文件和注册表操作

学习目标

掌握文件和目标的类

掌握Path类

对System.IO命名空间类的应用

流概念

掌握读写文件注册表类

 

1.管理文件系统

   在计算机中,会对其中的文件和目录进行管理。如:读取文件信息、删除文件和读取目录文件等,这些功能的实现,主要是由System.IO命名空间下的类对文件和目录进行操作。

System.IO命名空间为目录和文件的操作提供了必要的类、方法和属性。

System.IO命名空间

File(用于创建、复制、删除、移动和打开文件的静态方法)

 

FileInfo(提供创建、赋值、删除、移动和打开文件的实例方法):不是静态对象,只有实例化后才可以使用其属性和方法。

通过对FileInfo类和File类的使用,开发人员就可以获得文件的相关信息。

同样,使用DirectoryDirectoryInfo类可以获得目录的相关信息。

DirectoryDirectoryInfo

在System.IO命名空间中,,提供了使用DirectoryDirectoryInfo类来进行目录的管理。使用这两个类可以完成对目录和子目录的创建、移动、浏览等,甚至可以定义隐藏目录和只读目录。

Directory:所有方法都为静态方法,无需创建对象就可调用,这些方法可以操纵和查询任何目录的信息。

作。并且实例化后,就可以获取目录的创建时间和最后修改时间。

DirectoryInfo:需要有实例成员才可以调用其方法,从而有效的对一个目录进行多种操

作。并且实例化后,就可以获取目录的创建时间和最后修改时间。

示例:Exist方法(判断指定目录是否存在)

View Code
 1 private void button1_Click(object sender, EventArgs e)
2 {
3 string stringPath = "C:\\TestDirectory";
4 if (Directory.Exists(stringPath))
5 {
6 label1.Text = "该目录存在,请继续往下操作";
7 }
8 else
9 {
10 label1.Text = "该目录不存在,请检验路径是否正确";
11 }
12 }


Path:是静态类,不能实例化

示例:文件浏览器

View Code
 1 private void button1_Click(object sender, EventArgs e)
2 {
3 if (!(folderBrowserDialog1.ShowDialog() == DialogResult.OK))
4 {
5 return;
6 }
7 textBox1.Text = folderBrowserDialog1.SelectedPath;
8 if (textBox1.Text != "")
9 {
10 listBox1.Items.Clear();
11 listBox2.Items.Clear();
12 comboBox1.Items.Clear();
13 string pathString = textBox1.Text;
14 DirectoryInfo folderInfo = new DirectoryInfo(pathString);
15 foreach (FileSystemInfo info in folderInfo.GetFileSystemInfos())
16 {
17 if (info is DirectoryInfo)
18 {
19 listBox1.Items.Add(info.Name);
20 comboBox1.Items.Add(info.Name);
21 }
22 else
23 {
24 listBox2.Items.Add(info.Name);
25 }
26 }
27 }
28 }
29 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
30 {
31 string pathString = textBox1.Text + "\\" + listBox1.SelectedItem.ToString();
32 DirectoryInfo info = new DirectoryInfo(pathString);
33 label5.Text = " 目录创建时间:" + info.CreationTime.ToString() + "\n";
34 label5.Text = " 目录完整路径:" + info.FullName.ToString() + "\n";
35 label5.Text = "最后一次访问时间:" + info.LastAccessTime.ToString() + "\n";
36 label5.Text = "最后一次修改时间:" + info.LastWriteTime.ToString() + "\n";
37 label5.Text = " 文件夹名称:" + info.Name + "\n";
38 label5.Text = " 其父目录为:" + info.Parent + "\n";
39 label5.Text = " 其根目录为:" + info.Root;
40 }
41 private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
42 {
43 string pathString = textBox1.Text + "\\" + listBox2.SelectedItem.ToString();
44 FileInfo info = new FileInfo(pathString);
45 label5.Text = " 文件创建时间:" + info.CreationTime.ToString() + "\n";
46 label5.Text = "最后一次访问时间:" + info.LastAccessTime.ToString() + "\n";
47 label5.Text = "最后一次修改时间:" + info.LastWriteTime.ToString() + "\n";
48 label5.Text = " 文件名称:" + info.Name + "\n";
49 label5.Text = " 文件完整路径为:" + info.FullName + "\n";
50 label5.Text = " 文件扩展名为:" + info.Extension + "\n";
51 label5.Text = " 其文件大小为:" + info.Length / 1024 + "KB\n";
52 label5.Text = "其文件是否为只读:" + info.IsReadOnly;
53 }
54 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
55 {
56 textBox1.Text += "\\" + comboBox1.SelectedItem.ToString();
57 if (textBox1.Text != "")
58 {
59 listBox1.Items.Clear();
60 listBox2.Items.Clear();
61 comboBox1.Items.Clear();
62 string pathString = textBox1.Text;
63 DirectoryInfo folderInfo = new DirectoryInfo(pathString);
64 foreach (FileSystemInfo info in folderInfo.GetFileSystemInfos())
65 {
66 if (info is DirectoryInfo)
67 {
68 listBox1.Items.Add(info.Name);
69 comboBox1.Items.Add(info.Name);
70 }
71 else
72 {
73 listBox2.Items.Add(info.Name);
74 }
75 }
76 }
77 }

运行结果:并在窗体中进行相关操作

 

2.读写文件(方法的罗列)

File.ReadAllText(FilePaht)

File.ReadAll(FillPath,Encoding)

File.ReadAllBytes()

 File.WriteAll(FilePath,content)

 File.WriteAll(FilePath,content,Endcoding)

File.WriteAllBytes()

3.流

流是用于传输数据的对象,数据的传输有两种,即读取流和写入流。

对于文件的读写,最常用的类是:

FileSteam(文件流):用于二进制文件中读写二进制数据,也可读写任何文件

StreamReader(流读取器):读取问件,还可以处理任何流信息。

SteamWriter(流写入器):写入文件

读取文件

StreamReader类的方法不是静态方法,在使用该类读取文件要先实例化该类,实例化时,要先读取文件的路径,实例化后,打开指定文件,调用该类的方法就可以读取文件数据,最后完成后调用StreamReader类的Colse方法关闭流。

示例:

View Code
 1 private void button1_Click(object sender, EventArgs e)
2 {
3 openFileDialog1.ShowDialog();
4 textBox1.Text = openFileDialog1.FileName;
5 if(textBox1.Text !="")
6 {
7 string pathString = textBox1.Text;
8 if (File.Exists(pathString))
9 {
10 FileStream fileStream = File.OpenRead(pathString);
11 try
12 {
13 //实例化StrReader类(避免乱码出现),提供一个字符集选择
14 StreamReader reader = new StreamReader(fileStream, System.Text.Encoding.Default);
15 while (!reader.EndOfStream)
16 {
17 textBox2.Text += reader.ReadLine() + "\r\n";
18 }
19 reader.Close();
20 }
21 catch (Exception ex)
22 {
23 MessageBox.Show(ex.Message);
24 }
25 }
26 else
27 {
28 MessageBox.Show("你要读取的文件不存在");
29 }
30 }
31 }

运行结果:

 

写入文件

SteamWriter类可以实现向文件中写入内容的功能,以一种特殊的编码向字节流中写入字符。

示例:

View Code
 1 private void Form1_Load(object sender, EventArgs e)
2 {
3 string pathString = "C:\\MyNewText.txt";
4 if(System.IO.File.Exists(pathString))
5 {
6 StreamWriter writer = new StreamWriter(pathString ,true,System.Text.Encoding.Default);
7 writer.WriteLine("02.生气是拿别人做错的事惩罚自己。");
8 writer.Flush();//清理当前编写器的所有缓存区,并将缓存区数据写入文件。
9 writer.Close();
10 try
11 {
12 FileStream fStream = File.OpenRead(pathString);
13 StreamReader reader = new StreamReader(fStream,System.Text.Encoding.Default);
14 while (!reader.EndOfStream)
15 {
16 label1.Text = reader.ReadLine();
17 }
18 reader.Close();//关闭与StreamReder相关的实例文件
19 }
20 catch(Exception ex)
21 {
22 MessageBox.Show(ex.Message);
23 }
24 }
25 }

运行结果:

 

读写二进制文件

读写二进制文件首先要实例化FileSteam,两种方法,一种是new,另一种是使用 FileSteam fs = file.OpenRead()或file.OpenWrite()或file.Creat()或file.Open() ,使用完以后要Close().

示例:

View Code
 1 private void button1_Click(object sender, EventArgs e)
2 {
3 string fileName = "MyNew.data";
4 if (File.Exists(fileName))
5 {
6 MessageBox.Show("当前文件已存在");
7 }
8 else
9 {
10 FileStream fs = new FileStream(fileName, FileMode.Create);
11 BinaryWriter writer = new BinaryWriter(fs);
12 writer.Write(textBox1.Text);
13 for (int i=0; i < 300; i++)
14 {
15 writer.Write(10 + i);
16 }
17 MessageBox.Show("写入文件成功");
18 textBox1.Text = "";
19 writer.Close();
20 fs.Close();
21 }
22 }
23 private void button2_Click(object sender, EventArgs e)
24 {
25 string fileName = "MyNew.data";
26 if (!(File.Exists(fileName)))
27 {
28 MessageBox.Show("当前文件不存在");
29 return;
30 }
31 string strData = "";
32 FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
33 BinaryReader reader = new BinaryReader(fs);
34 strData = reader.ReadString();
35 for (int i=0; i < 200; i++)
36 {
37 if (i == 0)
38 {
39 strData += reader.ReadInt32().ToString();
40 }
41 else
42 {
43 strData += " || " + reader.ReadInt32().ToString();
44 }
45 }
46 textBox2.Text = strData;
47 fs.Close();
48 reader.Close();
49 }

运行结果:






posted @ 2012-02-01 01:27  翼灵绝  阅读(297)  评论(0编辑  收藏  举报