文件的操作:(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.ajax优缺点,工作流程

    1.ajax的优点 Ajax的给我们带来的好处大家基本上都深有体会,在这里我只简单的讲几点: 1.最大的一点是页面无刷新,在页面内与服务器通信,给用户的体验非常好.  2.使用异步方式与服务器通信,不 ...

  2. C++模板的实现(模板函数和模板类,附带模板实现顺序表和链表代码)

    文章链接:https://blog.csdn.net/qq_38646470/article/details/80209469

  3. parewise算法性能优化

    在<接口自动化测试框架-AIM>这篇博客中,提到了parewise算法. 这次对其进行性能优化,共3点. 一. 因为笛卡尔积和两两拆分,是有序的. 就保证了两两拆分后的每列都是相同位置的元 ...

  4. Ubuntu下配置Anaconda

    转自:https://blog.csdn.net/Horcham/article/details/57075388 安装Anaconda Ubuntu下似乎库中不自带Anaconda,是自带纯净的py ...

  5. 用 Delphi 7 实现基于 FFMS2 的视频转 GIF 工具 [原创]

    儿子经常要把自拍的视频(ts格式)转成表情包,下载了几个工具都不大好用,更多的还要收费.那就想自己写一个吧,没想到这一下断断续续地,居然 3 个月过去了.现在总算弄出个作品来了,结个贴吧.唉,天资愚钝 ...

  6. sqli-labs学习笔记 DAY6

    DAY 6 sqli-labs lesson 30 与上一题一样,需要用到HPP 查看源代码,参数两边加上了双引号,直接使用lesson 26a与lesson 27a的脚本即可 sqli-labs l ...

  7. 兼容所有浏览器的旋转效果-IE滤镜Matrix和CSS3transform

    在现代浏览器中使用CSS3的transform样式即可轻松搞定,但是对于国内IE浏览器(特别是7,8)还占有较大份额的情况下,兼容性还是必须要考虑的,所以也特意记录下IE旋转滤镜的使用. 在IE下的旋 ...

  8. Beta阶段基于NABCD评论作品

    组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶  刘佳瑞  公冶令鑫  杨磊  刘欣  张宇  卢帝同 一.拉格朗日2018--<飞词> 1.1.NABCD分析 N(Need,需求):该小 ...

  9. 探路者 FInal冲刺中间产物

    版本控制 https://git.coding.net/clairewyd/toReadSnake.git 版本控制报告 http://www.cnblogs.com/linym762/p/79976 ...

  10. 作业1MathExam

    自己取一个大气又可爱的标题 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 720 1000 ...