C#入门文件操作

文件操作先引入命名空间;

1 //引入命名空间
2 using System.IO;

一、写入问本文件,总结为5个步骤;

 1   //写入文件步骤 5个
 2             //1、创建文件流
 3             FileStream fs = new FileStream("c:\\fs1.txt",FileMode.Create);
 4             //2、创建写入器;
 5             StreamWriter sw = new StreamWriter(fs);
 6             //3、以流的方式写入数据;
 7             sw.Write(this.rtbContent.Text.Trim());
 8             //4、关闭写入器;
 9             sw.Close();
10             //5、关闭文件流;
11             fs.Close();

二、读取文本文件,一样5个步骤

 1 //读取文件步骤 5个
 2             //1、创建文件流
 3             FileStream fs = new FileStream("c:\\fsSysLog.txt", FileMode.Open);
 4             //2、创建读取器;
 5             //StreamReader sr = new StreamReader(fs,Encoding.Default);//第二个参数是编码方式,非必要参数,碰到乱码可以使用;
 6             StreamReader sr = new StreamReader(fs);
 7             //3、以流的方式读取数据;
 8             this.rtbContent.Text = sr.ReadToEnd();
 9             //4、关闭写入器;
10             sr.Close();
11             //5、关闭文件流;
12             fs.Close();

三、模拟写入系统日志文件(即逐行写入)

 1 //写入文件步骤 5个 模拟写入系统日志
 2             //1、创建文件流
 3             FileStream fs = new FileStream("c:\\fsSysLog.txt", FileMode.Append);
 4             //2、创建写入器;
 5             StreamWriter sw = new StreamWriter(fs);
 6             //3、以流的"逐行"方式写入数据;
 7             sw.WriteLine(DateTime.Now.ToString() + "[正在写入,操作正常!]");
 8             //4、关闭写入器;
 9             sw.Close();
10             //5、关闭文件流;
11             fs.Close();

四、删除文件

1            File.Delete(this.txtSourcePath.Text);
2             //删除文件

五、复制文件

1  if (File.Exists(this.txtToFilePath.Text.Trim()))//判断目标文件是否存在
2             {
3                 File.Delete(this.txtToFilePath.Text);//如果存在先删除
4             }
5             File.Copy(this.txtSourcePath.Text, this.txtToFilePath.Text);//复制文件

六、移动文件

 1           if (File.Exists(this.txtToFilePath.Text.Trim()))//判断目标文件是否存在
 2             {
 3                 File.Delete(this.txtToFilePath.Text);//如果存在先删除
 4             }
 5             if (File.Exists(this.txtSourcePath.Text.Trim()))//判断源文件是否存在
 6             {
 7                 File.Move(this.txtSourcePath.Text, this.txtToFilePath.Text);//存在则移动,否则提示
 8             }
 9             else
10             {
11                 MessageBox.Show("文件不存在");
12             }

 七、目录操作-获取目录下所有文件

 1            FileStream fs = new FileStream("objStudent.obj", FileMode.Open);
 2             StreamReader sr = new StreamReader(fs);
 3             //逐行读取;
 4             Student objStudent = new Student()
 5             {
 6                 StudentName = sr.ReadLine(),
 7                 Age = Convert.ToInt32(sr.ReadLine()),
 8                 Birthday = Convert.ToDateTime(sr.ReadLine()),
 9                 Gender = sr.ReadLine()
10             };
11             sr.Close();
12             fs.Close();
13 
14             //显示数据
15             this.txtStudentName.Text = objStudent.StudentName;
16             this.txtAge.Text = objStudent.Age.ToString();
17             this.txtBirthday.Text = objStudent.Birthday.ToShortDateString();
18             this.txtGender.Text = objStudent.Gender;

八、目录操作-获取当前文件夹下的子目录

1            //获取当前文件夹下的子目录
2             string[] dirs = Directory.GetDirectories("c:\\");
3             this.rtbContent.Clear();
4             foreach (string  item in dirs)
5             {
6                 this.rtbContent.Text += item + "\r\n";
7             }

九、目录操作-创建目录

1            //创建目录
2             Directory.CreateDirectory("c:\\newDir");

十、目录操作-删除制定目录;

1             //删除指定目录
2             //1、这个方法要求目录里面必须为空,没有文件;
3             Directory.Delete("c:\\newDir");
4             //2、方法2,使用Directoryinfo对象,可以删除不为空的目录;
5             DirectoryInfo dirInfo = new DirectoryInfo("c:\\newDir");
6             dirInfo.Delete(true);//true表示确定删除里面的文件;

 

posted @ 2017-07-28 11:02  sadseal  阅读(215)  评论(0编辑  收藏  举报