《C#入门经典(中文第四版)》第18章:图形图像程序设计
---------------------------------------------------------------------------------------------------------
System.IO 命名空间
A1 ………… File 类
举例:通过时间创建文件夹及文件,并写入内容!
A2 ………… Directory 类
A3 ………… Path 类
A4 ………… StreamReader 类
A5 ………… StreamWriter 类
A6 ………… FileWriter 类
A7 ………… FileStream 类
A8 ………… FileInfo 类
A9 ………… DirectoryInfo 类
G1 ………… FileSystemInfo 类
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A1个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 提供用于创建、复制、删除、移动和打开文件的静态方法,并协助创建 FileStream 对象。
2. File 方法(s):
using (FileStream fs = File.Create(path, 1024))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
using (FileStream fs = File.OpenRead(path))
{
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(ofd.FileName, textBox1.Text); //写入到文件
}
}
1 string s = textBox1.Text;
2 File.WriteAllText(@"E:\Desktop\222.txt", s);
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = File.ReadAllText(ofd.FileName); //从文件读取
}
}
1 string s = File.ReadAllText(@"E:\Desktop\1.txt", System.Text.Encoding.Default);
2 textBox1.Text = s;
1 string[] lines = File.ReadAllLines(@"E:\Desktop\1.txt",System.Text.Encoding.Default);
2 StringBuilder sb = new StringBuilder();
3 foreach (string line in lines)
4 {
5 sb.AppendLine(line);
6 }
7 textBox1.Text = sb.ToString();
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
using (StreamWriter streamWriter = File.AppendText(ofd.FileName))
{
streamWriter.WriteLine(textBox1.Text); //追加到文件的末尾!
}
}
}
1 using System;
2 using System.IO;
3
4 class Test
5 {
6 public static void Main()
7 {
8 string path = @"E:\Desktop\2.txt";
9 if (!File.Exists(path))
10 {
11
12 using (StreamWriter sw = File.CreateText(path))
13 {
14 sw.WriteLine("Hello");
15 sw.WriteLine("And");
16 sw.WriteLine("Welcome");
17 }
18
19
20
21 }
22
23
24 using (StreamWriter sw = File.AppendText(path))
25 {
26 sw.WriteLine("This");
27 sw.WriteLine("is Extra");
28 sw.WriteLine("Text");
29 }
30
31 using (StreamReader sr = File.OpenText(path))
32 {
33 string s = "";
34 while ((s = sr.ReadLine()) != null)
35 {
36 Console.WriteLine(s);
37 }
38 }
39 }
40 }
string folder = DateTime.Now.Month + "月" + DateTime.Now.Day + "日"; //通过当天的日期定义文件夹的名称 string file = DateTime.Now.Hour + "时" + DateTime.Now.Minute + "分" + DateTime.Now.Second + "秒" + ".txt";
//通过当时的时间定义文件的名称 string folderPath = Path.Combine(@"F:\Desktop\长江水文", folder); //定义文件夹的路径 string filePath = Path.Combine(@"F:\Desktop\长江水文", folder, file); //定义文件的路径 if (!Directory.Exists(filePath)) //判断文件夹是否存在,不存在,则先建立文件夹路径 { Directory.CreateDirectory(folderPath); //创建文件夹 } File.WriteAllText(filePath, webBrowser1.Document.Body.InnerText); //写入文件,不存在,则直接创建!
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A2个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 公开用于创建、移动和枚举通过目录和子目录的静态方法。
2. Directory 方法(s):
string text = null;
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() != DialogResult.OK)
return;
string[] files = Directory.GetFiles(fbd.SelectedPath, "*.txt"); //选择 文本文件!
foreach (string file in files)
{
text += file + "\r\n";
}
MessageBox.Show(text);
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A3个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 对包含文件或目录路径信息的 String 实例执行操作。 这些操作是以跨平台的方式执行的。
2. Path 方法:
richTextBox1.Text = Path.Combine(@"d:\Files");
richTextBox1.Text = Path.Combine(@"d:\Files", "23");
richTextBox1.Text = Path.Combine(@"d:\Files", "2008", "23");
richTextBox1.Text = Path.Combine(@"d:\Files", "2008", "01", "23");
richTextBox1.Text = Path.Combine(@"d:\Files", "2008", "01", "02", "23");
richTextBox1.Text = Path.Combine(@"d:\Files", "2008", "01", "02", "23", "23");
richTextBox1.Text = Path.Combine(@"d:\Files", "2008", "01", "02", "23", "01", "23");
richTextBox1.Text = Path.Combine(@"d:\Files", "2008", "01", "02", "23", "01", "02", "23");
//不受参数多少的影响
//d:\Files\2008\01\02\23\01\02\23
Path.GetDirectoryName(Application.ExecutablePath) //执行文件所在的目录
显示:E:\360data\重要数据\我的文档\Visual Studio 2010\Projects\倒计时\钢琴\bin\Debug
Path.GetFileName(Application.ExecutablePath) //执行文件的名称
显示:钢琴.EXE
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A4个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 实现一个 TextReader,使其以一种特定的编码从字节流中读取字符。
2. StreamReader 构造函数:
using (FileStream fileStream = File.OpenRead(openFileDialog1.FileName))
{
using (StreamReader streamReader = new StreamReader(fileStream, System.Text.Encoding.Default))
{
//有些编码格式没办法显示正常的中文,添加 System.Text.Encoding.Default 就OK了
}
}
using (StreamReader streamReader = new StreamReader(Path))
{
}
1 using (StreamReader sr = new StreamReader(@"E:\Desktop\1.txt", System.Text.Encoding.Default))
2 {
3 string s = "";
4 StringBuilder sb = new StringBuilder();
5 while ((s = sr.ReadLine()) != null)
6 {
7 sb.AppendLine(s);
8 }
9 textBox1.Text = sb.ToString();
10 }
1 using (StreamReader sr = new StreamReader(@"E:\Desktop\1.txt", System.Text.Encoding.Default))
2 {
3 textBox1.Text = sr.ReadToEnd().ToString();
4 }
3. StreamReader 方法:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"F:\Desktop";
ofd.Filter = "text|*.txt";
if (ofd.ShowDialog() == DialogResult.OK)
{
using (StreamReader streamReader = new StreamReader(ofd.FileName))
{
string str;
while ((str = streamReader.ReadLine()) != null) //没读到最后
{
textBox1.Text += str + "\r\n";
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"F:\Desktop";
ofd.Filter = "text|*.txt";
if (ofd.ShowDialog() == DialogResult.OK)
{
using (StreamReader streamReader = new StreamReader(ofd.FileName))
{
string r = null;
r = streamReader.ReadToEnd();
textBox1.Text = r;
}
}
}
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A5个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 实现一个 TextWriter,使其以一种特定的编码向流中写入字符。
2. StreamWriter 构造函数:
3. StreamWriter 方法:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"F:\Desktop";
ofd.Filter = "text|*.txt";
if (ofd.ShowDialog() == DialogResult.OK)
{
using (StreamWriter streamWriter = new StreamWriter(ofd.FileName))
{
streamWriter.WriteLine(textBox1.Text);
streamWriter.WriteLine(textBox1.Text);
}
}
}
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A6个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 公开以文件为主的 Stream,既支持同步读写操作,也支持异步读写操作。
2. FileStream 属性:
3. FileStream 方法:
using(FileStream fs = File.Create(path,1024))
{
fs.WriteByte(Convert.ToByte('U'));
}
fs.WriteByte(85)
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A7个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 公开以文件为主的 Stream,既支持同步读写操作,也支持异步读写操作。
2. FileStream 构造函数:
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A8个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 提供创建、复制、删除、移动和打开文件的属性和实例方法,并且帮助创建 FileStream 对象。 此类不能被继承。
2. FileInfo 属性:
3. FileInfo 方法:
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A9个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 公开用于创建、移动和枚举目录和子目录的实例方法。 此类不能被继承。
2. DirectoryInfo 属性:
3. DirectoryInfo 方法:
foreach (DirectoryInfo d in directory.GetDirectories("*", SearchOption.AllDirectories))
{
richTextBox1.Text += string.Format("{0:00} ", count) + d.FullName + "\n";
count++;
}
foreach (var d in directory.EnumerateDirectories("*", SearchOption.AllDirectories))
{
richTextBox1.Text += string.Format("{0:00} ", count) + d.FullName + "\n"; //很强大的 var!
count++;
}
foreach (var d in directory.GetFileSystemInfos("*", SearchOption.AllDirectories))
{
if (d.Extension == "")
{
richTextBox1.Text += string.Format("{0:00} ", count) + "文件夹\n" + d.FullName + "\n"; //扩展名为""的是文件夹
}
else
{
richTextBox1.Text += string.Format("{0:00} ", count) + "文件\n" + d.FullName + "\n";
}
count++;
}
foreach (var d in directory.GetFileSystemInfos("*", SearchOption.AllDirectories))
{
if (d.GetType() == typeof(DirectoryInfo)) //判断实例的类型
{
richTextBox1.Text += string.Format("{0:00} ", count) + "文件夹\n" + d.FullName + "\n";
}
else
{
richTextBox1.Text += string.Format("{0:00} ", count) + "文件\n" + d.FullName + "\n";
}
count++;
}
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第G1个 ╠══════════════════════════════════════════════════╣
╚════════╝
1. 为 FileInfo 和 DirectoryInfo 对象提供基类。