C#:文件/注册表/线程的操作
文件的操作:(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#:文件/注册表/线程的操作的更多相关文章
- C#高级编程笔记(22至25章节)文件\注册表\权限\事务
22安全(using System.Security.Principal;) AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.Wi ...
- Revo Uninstaller Pro - 真正彻底卸载软件不留垃圾的强大神器!(清理安装残留文件/注册表)
大家都知道 Windows 在卸载软件时总是不够彻底,系统C盘总会留下大量难以辨别和清理的垃圾文件和临时文件,时间长了注册表也会变得非常臃肿,不仅浪费硬盘空间,而且也会明显拖慢系统响应和启动速度. R ...
- C#注册表读写完整操作类
1.注册表基项静态域 /// <summary> /// 注册表基项静态域 ///1.Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT 主键 ///2.R ...
- DACL原理.控制文件的访问权限(文件,注册表.目录.等任何带有安全属性的对象.)
目录 一丶简介 1.DACL是什么. 2.如何创建一个自己控制的文件. 3.SDDL是个什么鬼. 二丶 编写SDDL 控制的文件 一丶简介 1.DACL是什么. DACL称为自主访问的控制列表.是应用 ...
- QSettings配置读写-win注册表操作-ini文件读写
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSettings配置读写-win注册表操作-ini文件读写 本文地址:http:// ...
- 【读书笔记】C#高级编程 第二十四章 文件和注册表操作
(一)文件和注册表 对于文件系统操作,相关的类几乎都在System.IO名称空间中,而注册表操作由System.Win32名称空间中的类来处理. (二)管理文件系统 System.MarshalByR ...
- delphi 注册表操作(读取、添加、删除、修改)完全手册
DELPHI VS PASCAL(87) 32位Delphi程序中可利用TRegistry对象来存取注册表文件中的信息. 一.创建和释放TRegistry对象 1.创建TRegistry对象.为了操 ...
- Delphi的注册表操作
转帖:Delphi的注册表操作 2009-12-21 11:12:52 分类: Delphi的注册表操作 32位Delphi程序中可利用TRegistry对象来存取注册表文件中的信息. 一.创 ...
- 【Visual Lisp】驱动器、目录、文件和注册表
;;驱动器.目录.文件.和注册表;;★★★01.获取并创建驱动器盘符组成的表★★★(setq Drives (vlax-get-property (vlax-create-object "S ...
随机推荐
- Spring学习(十七)----- Spring自动代理创建者
1. BeanNameAutoProxyCreator示例 在此之前,必须手动创建一个代理bean(ProxyFactryBean). <beans xmlns="http://www ...
- 【轮子狂魔】手把手教你自造Redis Client
为什么做Redis Client? Redis Client顾名思义,redis的客户端,主要是封装了一些对于Redis的操作. 而目前用的比较广泛的 ServiceStack.Redis 不学好,居 ...
- requestAnimationFrame优势何在?
大概半年前,无意中在网上看到一个新的js函数requestAnimationFrame,据说,此函数可以优化传统的js动画效果,似乎是未来js动画的新方向. 当时我所在的项目正好用到了和js动画有关的 ...
- 发现一个很N且免费的html5拓扑图 关系图 生成组件
传送门:http://visjs.org/ demo代码 <!doctype html> <html> <head> <title>vis.js new ...
- 树莓派UPS-18650,添加时钟
1.简介 UPS-18650 是一个专门为树莓派(以下简称 pi)所设计的 UPS 电源,采用两颗标准 的 18650 锂电池进行供电,支持外部电源插入检测,支持边充边放,既插上外部电源时, pi 由 ...
- AltiumDesigner 热焊盘铺铜
在layout中,引脚与大面积的铺铜完全连接容易造成过分散热而产生虚焊以及避免因过分散热而必须使用大功率焊接器,因此在大面积铺铜时,对于接地引脚,我们经常使用热焊盘.在AltiumDesigner 中 ...
- Oracle之带参存储过程(存储过程中for循环调用存储过程)
--带参存储过程create or replace procedure testdate(v in number) is i number; begin i:=v; insert into test_ ...
- 企业上云这四大要点,你 get 了吗?
本文由 Platform9(一家专注于云计算.专有云.混合云.OpenStack 以及容器技术的北美初创公司)技术产品营销经理 Akshai Parthasarathy 撰写,描述了企业在向云基础设施 ...
- alias命令详情
基础命令学习目录首页 原文链接:http://c.biancheng.net/view/938.html 给命令设置别名,你可以把它当作命令的"小名",但是这样做有什么意义呢? 比 ...
- 转载---LIBRARY_PATH和LD_LIBRARY_PATH环境变量的区别
总是分不太清楚LIBRARY_PATH和LD_LIBRARY_PATH环境变量的区别,每次都是现查一下,转载到这里,备忘... 转载自:https://www.cnblogs.com/panfeng4 ...