文件的操作:(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. 安装vs2017后,RDLC 报表定义具有无法升级的无效目标命名空间

    原先的RDLC报表定义用的命名空间是2008,用vs2017报表设计器重新保存后,会自动升级成2016,导致无法使用. 不想升级控件,太麻烦,所以就手动修改RDLC文件吧. 1.修改http://sc ...

  2. 使用Nmon_Analyzer excel 问题总结

    使用wps打开nmon的分析文件,出现  运行时错误13类型不匹配 查看具体代码,是这句出现错误Start = DateValue(Sheet1.Range("date")),进一 ...

  3. swap分区和内存

    1  查看swap 空间大小(总计):      # free -m          默认单位为k, -m 单位为M                total       used       fr ...

  4. AssertionError

    (1)p1 = multiprocessing.Process(test1)p2 = multiprocessing.Process(target=test2) 错误: p1缺少target,应为(t ...

  5. Kubernetes探索学习004--深入Kubernetes的Pod

    深入研究学习Pod 首先需要认识到Pod才是Kubernetes项目中最小的编排单位原子单位,凡是涉及到调度,网络,存储层面的,基本上都是Pod级别的!官方是用这样的语言来描述的: A Pod is ...

  6. SecureCRT SSH连接一直提示密码错误

    这是解决方法:  http://www.linuxidc.com/Linux/2016-09/134925.htm

  7. 2017-2018-1 Java演绎法 第九、十周 作业

    团队成员 [20162315 马军] [20162316 刘诚昊] [20162317 袁逸灏(组长)] [20162319 莫礼钟] [20162320 刘先润] [20162330 刘伟康] 项目 ...

  8. apm server

    目录 1.apm的tomcat启动失败解决方法 2.apm的mysql修改root密码的方法 内容: 1.apm的tomcat启动失败解决方法 APMServ5.2.6 无法启动Apache的一个问题 ...

  9. ThoughtWorks.QRCode类库

    ThoughtWorks.QRCode一个二维码生成类库.

  10. 【CSAPP笔记】10. 代码优化

    写程序的主要目标是使它在所有可能的情况下都能正确运行(bug free),一个运行得很快但有 bug 的程序是毫无用处的.在 bug free 的基础上,程序员必须写出清晰简洁的代码,这样做是为了今后 ...