文件的操作:(file与fileinfo,前者是静态方法,执行安全检查,适合对一个的操作)

1.1.create:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.IO;    //这个命名空间包含了对文件操作的类

 namespace WindowsFormsApplication2
 {
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }

         private void Form1_Load(object sender, EventArgs e)
         {

         }

         private void button1_Click(object sender, EventArgs e)
         {
             //判断是否有输入的文件地址
             if (textBox1.Text == string.Empty)
             {
                 this.toolStripStatusLabel1.Text = "请在红色的小点旁边,输入要创建的文件。";
                 errorProvider1.SetError(textBox1, "输入要创建的文件");
             }
             else
             {
                 if (File.Exists(textBox1.Text.Trim()))  //判断是否有此文件
                 { toolStripStatusLabel1.Text = "已经存在此文件了。"; }
                 else
                 {
                     File.Create(textBox1.Text);   //静态方法创建文件
                     toolStripStatusLabel1.Text = "文件创建成功!" + textBox1.Text + "    ";
                 }

             }
         }

         private void button2_Click(object sender, EventArgs e)
         {
             if (textBox2.Text == string.Empty)
             {
                 //以下2句为提示:
                 this.toolStripStatusLabel1.Text = "请在红色的小点旁边,输入要创建的文件。";
                 errorProvider1.SetError(textBox1, "输入要创建的文件");
             }
             else
             {
                 FileInfo fi = new FileInfo(textBox2.Text.Trim());  //实例化
                 if (fi.Exists)   //同上也是判断
                 {
                     toolStripStatusLabel1.Text = "已经存在此文件了。";
                 }
                 else
                 { fi.Create(); }  //创建该文件
             }
         }
     }
 }

检验:

 

1.2.move:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.IO;

 namespace WindowsFormsApplication2
 {
     public partial class Form2 : Form
     {
         public Form2()
         {
             InitializeComponent();
         }

         private void button1_Click(object sender, EventArgs e)
         {
             if (textBox1.Text == string.Empty && textBox2.Text == string.Empty)
             {
                 this.toolStripStatusLabel1.Text = "请按照要求填写信息!";
             }
             else
             {
                 //存储源文件的地址名
                 StringBuilder sb_yuanlai = new StringBuilder(textBox1.Text.Trim());
                 //存储要移动到的文件的地址名
                 StringBuilder sb_yidong = new StringBuilder(textBox2.Text.Trim());

                 //实例化fileinfo
                 FileInfo fi = new FileInfo(sb_yuanlai .ToString());
                 fi.MoveTo(sb_yidong.ToString());
                 toolStripStatusLabel1.Text = "已经完成移动!";
             }

         }
     }
 }

运行:

1.3.delete:

    private void button2_Click(object sender, EventArgs e)
         {
             if (radioButton1.Checked == true)  //判断是否选择该功能
             {
                 File.Delete("c:\\2012\\vs.txt");
                 if (File.Exists("c:\\2012\\vs.txt"))   //执行删除操作
                 { MessageBox.Show("操作失败!"); }
                 else
                 { MessageBox.Show("已经删除,ok!"); }
             }
             else
             { MessageBox.Show("你想干吗?"); }
         }

实验:

2.文件夹,目录:(directory/directory info,同上)

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.IO;

 namespace WindowsFormsApplication2
 {
     public partial class Form3 : Form
     {
         public Form3()
         {
             InitializeComponent();
         }

         private void button1_Click(object sender, EventArgs e)
         {
             if (textBox1.Text == string.Empty)
             { MessageBox.Show("请输入要创建的文件夹:"); }
             else
             {
                 DirectoryInfo di = new DirectoryInfo(textBox1.Text.Trim());
                 if (di.Exists)
                 {

                     MessageBox.Show("文件已存在!");

                 }
                 else
                 {
                     di.Create();//创建目录
                     di.MoveTo("c:\\2012\\vs"); //移动
                     Directory.Delete("c:\\2012", true);//删除
                     toolStripStatusLabel1.Text = "创建+移动+删除 操作以全部完成!";
                 }
             }
         }
     }
 }

运行:

综合运用:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.IO;

 namespace WindowsFormsApplication2
 {
     public partial class Form4 : Form
     {
         public Form4()
         {
             InitializeComponent();
         }

         private void button1_Click(object sender, EventArgs e)
         {
             //选择浏览文件
             if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
             {
                 //记录选择的目录地址
                 textBox1.Text = folderBrowserDialog1.SelectedPath;

                 //实例化目录地址
                 DirectoryInfo di = new DirectoryInfo(textBox1.Text.Trim());
                 FileSystemInfo[] fsi = di.GetFileSystemInfos();

                 //遍历
                 foreach (FileSystemInfo ss in fsi)
                 {
                     //判断是不是文件夹,目录
                     if (ss is DirectoryInfo)
                     {
                         //实例化当前遍历的目录
                         DirectoryInfo di_ss = new DirectoryInfo(ss.FullName);

                         //把对应的信息变成字符串显示
                         listView1.Items.Add(di_ss.Name);
                         listView1.Items[listView1.Items.Count - ].SubItems.Add((di_ss.FullName.ToString()));
                         listView1.Items[listView1.Items.Count - ].SubItems.Add("");
                         listView1.Items[listView1.Items.Count - ].SubItems.Add(di_ss.CreationTime.ToLongDateString());

                     }
                         //否则就是文件
                     else
                         //下面同上
                     {
                         FileInfo fi_ss = new FileInfo(ss.FullName );
                         listView1.Items.Add(fi_ss.Name );
                         listView1.Items[listView1.Items.Count - ].SubItems.Add(fi_ss.FullName.ToString());
                         listView1.Items[listView1.Items.Count - ].SubItems.Add(fi_ss.Length.ToString());
                         listView1.Items[listView1.Items.Count - ].SubItems.Add(fi_ss.CreationTime.ToLongDateString ());
                     }

                 }
             }
             else
             { MessageBox.Show("告诉我你的选择!"); }
         }
     }
 }

运行:

3.二进制流:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.IO;

 namespace WindowsFormsApplication2
 {
     public partial class Form5 : Form
     {
         public Form5()
         {
             InitializeComponent();
         }

         private void Form5_Load(object sender, EventArgs e)
         {
             AcceptButton = button1;  //默认回车为确定读取

         }

         private void button1_Click(object sender, EventArgs e)
         {
             //打开的文件类型
             openFileDialog1.Filter = "新建文本文档(*.txt)|*.txt|二进制文件(*.dat)|*.dat";
             //如果确定选择的:
             if (openFileDialog1.ShowDialog() == DialogResult.OK)
             {
                 //显示获取地址
                 textBox1.Text = openFileDialog1.FileName;

                 //实例化文件流为读取
                 FileStream fst = new FileStream(textBox1.Text.Trim(),FileMode.Open,FileAccess.Read );
                 //实例化二进制流
                 BinaryReader br = new BinaryReader(fst );
                 //判断读取的位置
                 )
                 {
                     //显示转换后的信息哦
                     richTextBox1.Text = Convert.ToString(br.ReadString());
                 }
                 //关闭释放流
                 fst.Close(); br.Close();                   toolStripStatusLabel1.Text = "读取写入完成!";
             }
         }

         private void button2_Click(object sender, EventArgs e)
         {
             //结构同上
             if (richTextBox1.Text == string.Empty)
             { toolStripStatusLabel1.Text = "请输入信息!"; }
             else
             {
                 saveFileDialog1.Filter = "新建文本文档(*.txt)|*.txt|二进制文件(*.dat)|*.dat";
                 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                 {
                     //实例化
                     FileStream fs = new FileStream(saveFileDialog1.FileName ,FileMode.OpenOrCreate,FileAccess.ReadWrite );
                    //实例化以二进制形式写入:
                     BinaryWriter bw = new BinaryWriter(fs );
                     bw.Write(richTextBox1.Text.Trim());

                     fs.Close(); bw.Close();
                     toolStripStatusLabel1.Text = "写入完成!";
                 }
             }
         }
     }
 }

运行:

写入一句话:

“大家好,我是李晓峰,很高兴您能审阅我的博客,谢谢!
2013-8-30”

注册表:regedit:

1.1读取HKEY_LOCAL_MACHINE\SOFTWARE下的信息:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using Microsoft.Win32;  //对注册表的操作要包含此空间

 namespace WindowsFormsApplication2
 {
     public partial class Form6 : Form
     {
         public Form6()
         {
             InitializeComponent();
         }

         private void toolStripStatusLabel1_Click(object sender, EventArgs e)
         {

         }

         private void button1_Click(object sender, EventArgs e)
         {
             if (checkBox1.CheckState == CheckState.Checked)
             {
                 RegistryKey rk = Registry.LocalMachine;
                 RegistryKey rk2 = rk.OpenSubKey(@"SOFTWARE");

                 //读取SOFTWARE下的子项(目录)
                 foreach (string ss in rk2.GetSubKeyNames())
                 {
                     //写入显示
                     listBox1.Items.Add("子项 is:"+ss );

                     //继续读取遍历目录下的子项
                     RegistryKey rk3 = rk2.OpenSubKey(ss );
                     //遍历目录下的所有值
                     foreach (string sss in rk3.GetValueNames())
                     { listBox1.Items.Add("值:"+ss+rk3.GetValue(sss)); }
                 }
             }
         }
     }
 }

运行:

       

2.在HKEY_LOCAL_MACHINE\HARDWARE 目录下创建一个子目录li,在li下面创建xiao写入一个feng键值绑定数据“lifeng123”和lf键值绑定数据“123”;

  private void button2_Click(object sender, EventArgs e)
         {
             if (checkBox1.CheckState == CheckState.Checked)
             {
                 RegistryKey rk = Registry.LocalMachine;
                 //打开,“true”代表应许能被修改的:
                 RegistryKey rk2 = rk.OpenSubKey("HARDWARE",true );

                 //创建其下的子项目录:
                 RegistryKey rk3 = rk2.CreateSubKey("li"); RegistryKey rk4 = rk3.CreateSubKey("xiao");

                 //创建子键
                 rk4.SetValue("); rk4.SetValue("feng","feng123");

                 toolStripStatusLabel1.Text = "创建成功!";
             }
         }

提交:

2.修改:将创建的2个键值绑定的数据为“你好123”;

 private void button3_Click(object sender, EventArgs e)
         {
             try
             {
                 //下面打开旗目录子项
                 RegistryKey rk = Registry.LocalMachine;
                 RegistryKey rk2 = rk.OpenSubKey("HARDWARE",true );
                 RegistryKey rk3 = rk2.OpenSubKey("li", true); RegistryKey rk4 = rk3.OpenSubKey("xiao",true );

                 //修改:
                 rk4.SetValue("lf", "你好123"); rk4.SetValue("feng", "你好123");
                 toolStripStatusLabel1.Text = "修改ok!";
             }
             catch (Exception ex)
             { toolStripStatusLabel1.Text = ex.Message; }
         }

运行:

3.1.删除键值:

 private void button4_Click(object sender, EventArgs e)
         {
             try
             {
                 //下面打开旗目录子项
                 RegistryKey rk = Registry.LocalMachine;
                 RegistryKey rk2 = rk.OpenSubKey("HARDWARE", true);
                 RegistryKey rk3 = rk2.OpenSubKey("li", true); RegistryKey rk4 = rk3.OpenSubKey("xiao", true);

                 //删除:
                 rk4.DeleteValue("lf");
                 toolStripStatusLabel1.Text = "删除ok!";
             }
             catch (Exception ex)
             { toolStripStatusLabel1.Text = ex.Message; }
         }

运行:

3.2.删除目录:

   private void button5_Click(object sender, EventArgs e)
         {
             try
             {
                 //下面打开旗目录子项
                 RegistryKey rk = Registry.LocalMachine;
                 RegistryKey rk2 = rk.OpenSubKey("HARDWARE", true);
                 RegistryKey rk3 = rk2.OpenSubKey("li", true);

                 //删除:
                 rk3.DeleteSubKey("xiao",true );
                 toolStripStatusLabel1.Text = "删除ok!";
             }
             catch (Exception ex)
             { toolStripStatusLabel1.Text = ex.Message; }
         }

运行;

3.3.删除根目录;

 private void button6_Click(object sender, EventArgs e)
         {
             try
             {
                 //下面打开旗目录子项
                 RegistryKey rk = Registry.LocalMachine;
                 RegistryKey rk2 = rk.OpenSubKey("HARDWARE", true);

                 //删除:
                 rk2.DeleteSubKeyTree("li",true );
                 toolStripStatusLabel1.Text = "全部删除ok!";
             }
             catch (Exception ex)
             { toolStripStatusLabel1.Text = ex.Message; }
         }

运行:

线程:

1.1.创建一个线程:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.Threading;  //次空间是对线程进行操作的
 using System.Collections;

 namespace WindowsFormsApplication2
 {
     public partial class Form7 : Form
     {
         public Form7()
         {
             InitializeComponent();
         }

         public static void fangfa1()
         { MessageBox.Show("主线程已启动!"); }

         private void button1_Click(object sender, EventArgs e)
         {
             //创建主线程
             Thread th = new Thread(new ThreadStart(fangfa1 ));
             //启动主线程:
             th.Start(); th.Name = "lixiaofeng";

             //实例化一数组捕获消息:
             ArrayList al = new ArrayList();
             al.Add("是否为后台线程:"+th.IsBackground.ToString() + "\n"); al.Add("线程的优先级别:"+th.Priority.ToString() + "\n");
             al.Add("线程的当前连接状态:"+th.ThreadState.ToString() + "\n");
             al.Add("线程名称:"+th.Name + "\n"); al.Add("线程唯一标示符是:"+th.ManagedThreadId.ToString() + "\n");

             Thread.Sleep();

             //关闭释放
             th.Abort("退出主线程!");
             th.Join();               toolStripStatusLabel1.Text = "ok";

             //显示:
             foreach (string ss in al)
             { richTextBox1.Text += ss; }

         }
     }
 }

运行:

 

1.2.优先级:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Threading;

 namespace ConsoleApplication4
 {

     class Program
     {
         public static void fangfa1()
         { Console.WriteLine("线程1已启动!"); }

         public static void fangfa2()
         { Console.WriteLine("线程2已启动!"); }

         public static void fangfa3()
         { Console.WriteLine("线程3已启动!"); }

         static void Main(string[] args)
         {
             //实例化
             Program pr = new Program();
             Thread th1 = new Thread(new ThreadStart(fangfa1 ));
             Thread th2 = new Thread(new ThreadStart(fangfa2 ));
             Thread th3 = new Thread(new ThreadStart(fangfa3 ));

             //设置优先级:
             th2.Priority = ThreadPriority.Highest;
             th3.Priority = ThreadPriority.BelowNormal;
             th1.Priority = ThreadPriority.Lowest;

             //打开进程;
             th1.Start(); th2.Start(); th3.Start();

             Console.ReadLine();
             //关闭进程;
             th3.Abort(); th2.Abort(); th1.Abort();
         }
     }
 }

运行:

1.3.同步;

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Threading;

 namespace ConsoleApplication4
 {

     class Program
     {
         public  void fangfa1()
         {
             lock (this)
             { Console.WriteLine("线程1已启动!"); }
         }

         public void fangfa2()
         {
             Monitor.Enter(this);
             Console.WriteLine("线程2已启动!");
             Monitor.Exit(this );
         }

         public void fangfa3()
         {
             Mutex mu = new Mutex();
             mu.WaitOne();
             Console.WriteLine("线程3已启动!");
             mu.ReleaseMutex();
         }

         static void Main(string[] args)
         {
             Program pr = new Program();

             //调用其方法
             pr.fangfa1();
             pr.fangfa2();
             pr.fangfa3();

             Console.ReadLine();
         }
     }
 }

运行:

能力有限,谢谢!

C#:文件/注册表/线程的操作的更多相关文章

  1. C#高级编程笔记(22至25章节)文件\注册表\权限\事务

    22安全(using System.Security.Principal;) AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.Wi ...

  2. Revo Uninstaller Pro - 真正彻底卸载软件不留垃圾的强大神器!(清理安装残留文件/注册表)

    大家都知道 Windows 在卸载软件时总是不够彻底,系统C盘总会留下大量难以辨别和清理的垃圾文件和临时文件,时间长了注册表也会变得非常臃肿,不仅浪费硬盘空间,而且也会明显拖慢系统响应和启动速度. R ...

  3. C#注册表读写完整操作类

    1.注册表基项静态域 /// <summary> /// 注册表基项静态域 ///1.Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT 主键 ///2.R ...

  4. DACL原理.控制文件的访问权限(文件,注册表.目录.等任何带有安全属性的对象.)

    目录 一丶简介 1.DACL是什么. 2.如何创建一个自己控制的文件. 3.SDDL是个什么鬼. 二丶 编写SDDL 控制的文件 一丶简介 1.DACL是什么. DACL称为自主访问的控制列表.是应用 ...

  5. QSettings配置读写-win注册表操作-ini文件读写

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSettings配置读写-win注册表操作-ini文件读写     本文地址:http:// ...

  6. 【读书笔记】C#高级编程 第二十四章 文件和注册表操作

    (一)文件和注册表 对于文件系统操作,相关的类几乎都在System.IO名称空间中,而注册表操作由System.Win32名称空间中的类来处理. (二)管理文件系统 System.MarshalByR ...

  7. delphi 注册表操作(读取、添加、删除、修改)完全手册

    DELPHI VS PASCAL(87)  32位Delphi程序中可利用TRegistry对象来存取注册表文件中的信息. 一.创建和释放TRegistry对象 1.创建TRegistry对象.为了操 ...

  8. Delphi的注册表操作

    转帖:Delphi的注册表操作 2009-12-21 11:12:52 分类: Delphi的注册表操作 32位Delphi程序中可利用TRegistry对象来存取注册表文件中的信息.     一.创 ...

  9. 【Visual Lisp】驱动器、目录、文件和注册表

    ;;驱动器.目录.文件.和注册表;;★★★01.获取并创建驱动器盘符组成的表★★★(setq Drives (vlax-get-property (vlax-create-object "S ...

随机推荐

  1. 封装的一套简单轻量级JS 类库(RapidDevelopmentFramework.JS)

    1.最近好久没有更新自己的博客了,一直在考虑自己应该写一些什么.4.2日从苏州回到南京的路上感觉自己的内心些崩溃和失落,我就不多说了? 猛然之间我认为自己需要找一下内心的平衡.决定开发属于自己一套快速 ...

  2. IIS 配置网站

    IIS 配置网站最常见的问题 1 文件夹权限问题 (C盘 windows下temp文件夹权限问题)2 版本问题 (框架版本问题如4.0 在2.0下运行,4.5在4.0下运行) 3如果是局域网,考虑动态 ...

  3. 使用MUART0-P-1-2设置无线PM2.5感测环境

    信息搜集–> 处理分析–> 动作执行,这是IoT环境中最基本的组成要素,传感器搜集环境信息后,透过指定的通讯协议传送到至控制中枢,经过处理分析后再将命令送交各device端执行.要实现这样 ...

  4. Micro:bit篮球小游戏

    尝试用Micro:bit制作一款篮球游戏,材料是利用一些纸箱跟生活周遭可以取得的加上Micro;bit,打造出一个好玩的篮球游戏,制作过程也十分简单. 材料清单 先制作篮板. 接着制作篮球架体. 制作 ...

  5. 维诺图(Voronoi Diagram)分析与实现(转)

    一.问题描述1.Voronoi图的定义又叫泰森多边形或Dirichlet图,它是由一组由连接两邻点直线的垂直平分线组成的连续多边形组成. 2.Voronoi图的特点(1)每个V多边形内有一个生成元: ...

  6. FFmpeg简单转码程序--视频剪辑

    学习了雷神的文章,慕斯人分享精神,感其英年而逝,不胜唏嘘.他有分享一个转码程序<最简单的基于FFMPEG的转码程序>其中使用了filter(参考了ffmpeg.c中的流程),他曾说想再编写 ...

  7. D.王者荣耀交流协会——PSP Daily(测评人:贾男男)

    D.王者荣耀交流协会——PSP Daily(测评人:贾男男) 一.基于NABCD评论作品,及改进建议 每个小组评论其他小组beta发布的作品.1.根据(不限于)NABCD评论作品的选题;2.评论作品对 ...

  8. 20145214 《网络对抗技术》 MSF基础应用

    20145214 <网络对抗技术> MSF基础应用 1.实验后回答问题--用自己的话解释什么是exploit,payload,encode 如果把MSF比作一把枪的话,payload应该是 ...

  9. web 08 struts2入门 struts2配置 struts包

    电影网站:www.aikan66.com 项目网站:www.aikan66.com游戏网站:www.aikan66.com图片网站:www.aikan66.com书籍网站:www.aikan66.co ...

  10. 第二阶段Sprint1

    昨天:进行第二阶段第一次站立会议,讨论冲刺阶段,目标,任务认领 今天:实现视频录制,共享平台的视频下载和上传 遇到的问题:调手机摄像头没问题,共享平台怎么办