
ZJWCHC 第十三章 20/728 文件和注册表操作
ZJWCHC 文件和注册表操作 第十三章

目标 ◆了解System.Io命名空间 ◆掌握读写文本文件的方法 ◆掌握向文件读写二进制数据的方法 ◆掌握读写内存流的方法 ◆掌握注册表的操作方法 2
2 目标 了解System.IO 命名空间 掌握读写文本文件的方法 掌握向文件读写二进制数据的方法 掌握读写内存流的方法 掌握注册表的操作方法

System.I0命名空间3-1 File对象 静态方法 Move Delete Copy CreateText OpenText Open 0
3 System.IO 命名空间 3-1 File对象 静态方法 Move Delete Copy CreateText OpenText Open

System.I0命名空间3-2 试一试: 把C:\WinNTWin.INl文件拷贝到Cl下的代码,怎么写?
4 System.IO 命名空间 3-2 试一试: 把C:\WinNT\Win.INI文件拷贝到C:\下的代码,怎么写?

System.I0命名空间3-3 ◆FileInfo类和File类 。两者都提供对文件类似的操作 ·FileInfo不是静态对象 ·FileInfo提供了实例成员,因此不是线程安全 的,不会因为安全检查而降低效率 5
5 System.IO 命名空间 3-3 FileInfo类和File类 两者都提供对文件类似的操作 FileInfo不是静态对象 FileInfo提供了实例成员,因此不是线程安全 的,不会因为安全检查而降低效率

读写文本文件3-1 System.lIO命名空间 File类 静态方法 继承类 CreateText(string FilePath) OpenText(string FilePath) Open(string FilePath, FileMode) FileStream类 Create(string FilePath) OpenRead(string FilePath) AppendText(string FilePath)
6 读写文本文件 3-1 System.IO 命名空间 静态方法 继承类 CreateText(string FilePath) OpenText(string FilePath) Open(string FilePath, FileMode) Create(string FilePath) OpenRead(string FilePath) AppendText(string FilePath) FileStream 类 File 类

读写文本文件3-2 ◆FileStream构造函数 FileStream已重写构造函数 FileStream(string FilePath,FileMode) FileStream(string FilePath,FileMode, FileAccess) FileStream(string FilePath,FileMode, FileAccess,FileShare) 在构造函数中使用的FileMode、FileAccess和 FileShare参数都是enum类型
7 读写文本文件 3-2 FileStream 构造函数 FileStream 已重写构造函数 FileStream(string FilePath, FileMode) FileStream(string FilePath, FileMode, FileAccess) FileStream(string FilePath, FileMode, FileAccess, FileShare) 在构造函数中使用的 FileMode、FileAccess 和 FileShare 参数都是enum 类型

FileMode和FileShare FileStream fstream new FileStream("Test.cs", FileMode.OpenOrCreate, FileAccess.ReadWrite,FileShare.None); 。。。。。。 8
8 FileMode 和FileShare FileMode Append Create CreateNew Open OpenOrCreate Truncate FileShare None Read Write ReadWrite ………… FileStream fstream = new FileStream("Test.cs" , FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); ………

文件读写例子4-1 文本编器 文件名: 保存(9 9
9 文件读写例子 4-1

FileStream fs; try fs=File.Create(txtFileName.Text); catch 创建文件 MessageBox.Show("建立文件时出错。,"错误 System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxlcon.Warning); return; } byte[]content= ew UTF8Encoding(true)GetBytes(xtContent.Text); fs.Write(content,0,content.Length); fs.Flush0): 9丽将转换后的Be数组写入新建的文本文件 System.WVindw.Fomms.MessageBoxlcon.Infomation): System catch MessageBox..Show("写入文件时出错。","错误" System.Windows.Forms.MessageBoxButtons.OK System.Windows.Forms.MessageBoxlcon.Warning): finally fs.Close();
10 文件读写例子4-2 FileStream fs; try { fs = File.Create(txtFileName.Text); } catch { MessageBox.Show("建立文件时出错。","错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); return; } byte[] content = new UTF8Encoding(true).GetBytes(txtContent.Text); try { fs.Write(content, 0, content.Length); fs.Flush(); MessageBox.Show("保存成功", "保存", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); } catch { MessageBox.Show("写入文件时出错。","错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); } finally { fs.Close(); } } 创建文件 将转换后的Byte数组写入新建的文本文件