C#程序设计项目化教程-电子教案-郑广成章节10章

上传人:E**** 文档编号:89085840 上传时间:2019-05-17 格式:PPT 页数:28 大小:1.31MB
返回 下载 相关 举报
C#程序设计项目化教程-电子教案-郑广成章节10章_第1页
第1页 / 共28页
C#程序设计项目化教程-电子教案-郑广成章节10章_第2页
第2页 / 共28页
C#程序设计项目化教程-电子教案-郑广成章节10章_第3页
第3页 / 共28页
C#程序设计项目化教程-电子教案-郑广成章节10章_第4页
第4页 / 共28页
C#程序设计项目化教程-电子教案-郑广成章节10章_第5页
第5页 / 共28页
点击查看更多>>
资源描述

《C#程序设计项目化教程-电子教案-郑广成章节10章》由会员分享,可在线阅读,更多相关《C#程序设计项目化教程-电子教案-郑广成章节10章(28页珍藏版)》请在金锄头文库上搜索。

1、C#程序设计 单元10 文件操作,主讲教师:C#课程组 授课专业:软件技术,单元10 文件操作,本,章,要,点,文件操作有关的类 文件系统的管理 文本文件的读写 序列化和反序列化的概念和功能,技,能,目,标,能进行文件和文件夹操作 能读写文本文件 能使用序列化和反序列化保持 和恢复对象状态,单元10 文件操作,单元10 文件操作,10.2技术与知识准备 10.2.1文件操作的类 在Visual C# 2010中,有很多与文件操作有关的类,它们位于System.IO命名空间中,使用这些类时,必须先引入System.IO命名空间,代码如下: using System.IO; 表列出了这些与文件操作

2、有关的且经常使用的类,并给出了这些类的相关说明。具体的使用示例将在本章后面的内容中详细介绍。,单元10 文件操作,示例:使用文件操作类来完成编辑器。 【步骤1】设计程序界面,如图所示。,单元10 文件操作,【步骤2】引入命名空间,代码如下: using System.IO; 【步骤3】双击【创建文件夹】按钮,编写其单击事件,代码如下: private void btnCreate_Click(object sender, EventArgs e) string dir; try dir = txtDir.Text; if (!Directory.Exists(dir) /Directory.C

3、reateDirectory(dir); DirectoryInfo dire = new DirectoryInfo(dir); dire.Create(); MessageBox.Show(“创建成功!请查看“); else MessageBox.Show(“此文件夹已存在!“); catch MessageBox.Show(“请输入要创建的文件夹名!“); txtDir.Focus(); ,单元10 文件操作,【步骤4】双击【创建文件】按钮,编写其单击事件代码如下: private void btnCreate_Click(object sender, EventArgs e) stri

4、ng output = “文件信息:rn“; string file1; file1 = txtCreate.Text; if (!File.Exists(file1) /File.Create(file1); FileInfo fl = new FileInfo(file1); fl.Create(); MessageBox.Show(“创建成功!请查看“); output += “t文件名:“ + file1 + “rn“; output += “t创建时间:“ + File.GetCreationTime(file1) + “rn“; output += “t最后修改时间:“ + Fil

5、e.GetLastWriteTime(file1) + “rn“; output += “t最后访问时间:“ + File.GetLastAccessTime(file1) + “rn“; rtxtFileInfo.Text = output; else MessageBox.Show(“此文件已存在!“); ,单元10 文件操作,运行结果,单元10 文件操作,【步骤5】运行程序,单击【复制】按钮,编写其单击事件代码如下: private void btnCopy_Click(object sender, EventArgs e) try string source, destination;

6、 destination = txtDest.Text; source = txtSource.Text; /File.Copy(source, destination); File.Copy(source, destination, true); MessageBox.Show(“复制成功!请查看“); catch MessageBox.Show(“源文件夹或目标文件夹不存在,请查看!“); 运行结果,单元10 文件操作,【步骤6】运行程序,单击【移动】按钮,编写其单击事件代码如下: private void btnMove_Click(object sender, EventArgs e)

7、 string source1, destination1; destination1 = txtMoveDes.Text; source1 = txtMoveSour.Text; File.Move(source1, destination1); MessageBox.Show(“移动成功!请查看“); 【步骤7】运行程序,单击【写入内容】按钮,编写其单击事件代码如下: private void btnWriteFile_Click(object sender, EventArgs e) string path = txtWrFile.Text; string content = rtxtW

8、rite2.Text; if (path=“) MessageBox.Show(“文件路径不能为空“); return; try FileStream myfs = new FileStream(path, FileMode.Create); /创建写入器 StreamWriter mySw = new StreamWriter(myfs); /将录入的内容写入文件 mySw.Write(content); /关闭写入器 mySw.Close(); /关闭文件流 myfs.Close(); MessageBox.Show(“写入成功“); catch (Exception ex) Messag

9、eBox.Show(ex.Message); ,单元10 文件操作,【步骤8】运行程序,单击【读取】按钮,编写其单击事件代码如下: private void btnRead_Click(object sender, EventArgs e) string path = txtFile.Text; if (path=“) return; /检测是否是文本文件(以.txt结尾) string fileName = path.Substring(path.LastIndexOf(“.“); if (!fileName.Equals(“.txt“) MessageBox.Show(“请选择文本文件!“

10、, “提示“); return; string content; try /创建文件流 FileStream myfs = new FileStream(path, FileMode.Open); /创建读取器 StreamReader mySr = new StreamReader(myfs); /读取文件所有内容 content = mySr.ReadToEnd(); rtxtWrite.Text = content; /关闭读取器 mySr.Close(); /关闭文件流 myfs.Close(); catch (Exception ex) MessageBox.Show(ex.Mess

11、age); ,单元10 文件操作,10.2.2 序列化和反序列化 大家都知道,程序运行过程中所创建的对象都位于内存中,当程序运行结束,对象的生命周期就结束了。如果能将对象的信息保存下来,下次程序启动读取这些信息将还原这些对象,使它们保持与上次结束时相同的状态。典型的例子如单击游戏中常见的进度保存功能,本次游戏半途中止,只需要保存进度,下次启动还可以继续。我们可以通过序列化和反序列化的方式简单快捷的实现这种效果。 所谓序列化,就是将对象先转换为一种适当格式,然后再将其传输到目标位置的过程。所谓适当格式,有时候需要是二进制格式,有时候需要SOAP格式或者其他的XML格式,也可以是应用程序所特有的、

12、定制化的格式。因此,可以将序列化视为将对象的状态保存到流或缓冲区的方法。和序列化相反的就是反序列化,就是把对象或数据从序列化的状态恢复为其原始状态的过程。 .NET提供了三种预定义的格式化程序:BinaryFormatter、SoapFormatter、XmlSerializer,目前使用二进制方式对泛型支持的最好。,单元10 文件操作,示例:使用BinaryFormatter进行序列化和反序列化。 public class MyClass /序列化方法 public void Save(Student student) FileStream fs = new FileStream(“stud

13、ent.bin“, FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, student); fs.Close(); /反序列化方法 public Student Load() Student student; FileStream fs = new FileStream(“student.bin“, FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); student = (Student)bf.Deserialize(fs

14、); fs.Close(); return student; ,单元10 文件操作,通过10.2内容的学习,应该了解了文件操作的常用类,能实现文件和目录的常用操作。下面我们将回到10.1节中介绍的工作场景1中,完成工作任务。,10.3 回到工作场景1,【工作过程一】 【步骤1】新建一个名为“MyExplore”的Windows应用程序。 【步骤2】添加一个文件信息类(MyFile)用来存储文件信息。,单元10 文件操作,【步骤3】编写关键代码 public class MyFile /文件名 private string fileName; public string FileName get

15、 return fileName; set fileName = value; /文件类型 private string fileType; public string FileType get return fileType; set if (value.Length 1) fileType = value.Substring(1, value.Length - 1); /文件大小 private float fileLength; public float FileLength get return fileLength; set /将字节大小转换为KB为单位 fileLength = float.Parse(Math.Round(value/1024, 2).ToString(); /文件路径 private string filePath; public string FilePath get return filePath; set filePath = value; ,单元10 文件操作,【工作过程二】 【步骤1】设计程序界面,添加TreeView控件和ListView控件,单元10 文件操作,【步骤

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号