当前位置:   article > 正文

C# 文件操作(复制、移动、重命名、创建、打开、删除)_c# 创建文件

c# 创建文件

目录

一、简介

二、创建文件

三、写入文件

四、读取文件

五、复制文件

六、移动文件

七、重命名文件

八、删除文件

结束


一、简介

C#中的IO(Input/Output)操作包括读取和写入文件、读取和写入流、以及操作目录和文件夹等。这些操作都可以通过System.IO命名空间中的类实现。下面对C# IO相关的类和操作进行介绍。

文件操作
File类
File类提供了对文件的创建、读取、写入、复制、移动、重命名和删除等操作。

StreamReader和StreamWriter类
StreamReader和StreamWriter类用于读取和写入文本文件。

目录和文件夹操作
Directory和DirectoryInfo类
Directory和DirectoryInfo类提供了对目录和文件夹的创建、移动、重命名和删除等操作。

二、创建文件

要创建一个文件,可以使用System.IO.File.Create()方法。该方法接受一个文件路径和一个可选的缓冲区大小作为参数,然后返回一个FileStream对象,该对象可用于写入文件。

代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace 文件操作
  8. {
  9. internal class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. try
  14. {
  15. string filePath = @"E:\myFile.txt";
  16. FileStream fileStream = File.Create(filePath);
  17. fileStream.Close();
  18. }
  19. catch (Exception ex)
  20. {
  21. Console.WriteLine("创建文件失败:" + ex.Message);
  22. }
  23. Console.WriteLine("创建文件成功!");
  24. Console.ReadKey();
  25. }
  26. }
  27. }

打开E盘,文件是创建成功了,只是这个 txt 文件里没有任何内容

三、写入文件

写入文件可以使用下面方式:
1. File.WriteAllText(FilePath,String)
2. File.WriteAllText(FilePath, String,Encoding) ----指定编码
3. File.WriteAllLines(FilePath,String[])
4. File.WriteAllLines(FilePath,String[],Encoding) ----指定编码

前面两种写入的是一个字符串,后面两种写入的是一个字符串数组。

使用 File.WriteAllText 或 File.WriteAllLines 方法时,如果指定的文件路径不存在,会创建一个新文件;如果文件已经存在,则会覆盖原文件。

第一种:字符串的写入

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace 文件操作
  8. {
  9. internal class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string filePath = @"E:\myFile.txt";
  14. string content = "读取读取读取读取读取读取";
  15. //第一种
  16. //File.WriteAllText(filePath, content);
  17. //第二种
  18. File.WriteAllText(filePath, content, Encoding.UTF8);
  19. Console.ReadKey();
  20. }
  21. }
  22. }

打开刚创建的 myFile.txt 文件:

第二种:多行字符串的写入

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace 文件操作
  8. {
  9. internal class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string filePath = @"E:\myFile.txt";
  14. string[] contentArr = { "dddd", "eeeeee" };
  15. //第一种
  16. //File.WriteAllLines(filePath, contentArr);
  17. //第二种
  18. File.WriteAllLines(filePath, contentArr, Encoding.UTF8);
  19. Console.ReadKey();
  20. }
  21. }
  22. }

上面写入的字符串现在已经被覆盖了

上面写入字符串都会覆盖原有的内容,下面就介绍如何追加字符串。

第三种:追加字符串

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Test1
  8. {
  9. internal class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string filePath = @"E:\myFile.txt";
  14. string content = "423423423423";
  15. File.AppendAllText(filePath, content);
  16. Console.ReadKey();
  17. }
  18. }
  19. }

filePath 路径这个文件如果没有,C# 会自动生成这个文件,运行后找到这个文件,就可以看到添加的内容

下面我将内容做一下更改,再次运行

  1. namespace Test1
  2. {
  3. internal class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. string filePath = @"E:\myFile.txt";
  8. string content = "dffsddgaadfasdfa";
  9. File.AppendAllText(filePath, content);
  10. Console.ReadKey();
  11. }
  12. }
  13. }

我们重写打开 myFile.txt 这个文件,发现文字是追加上去了,但是并没有换行

其实换行也非常的简单,直接在字符串里加转义字符 \n 就行了

  1. namespace Test1
  2. {
  3. internal class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. string filePath = @"E:\myFile.txt";
  8. string content = "\n哇哈哈哈哈";
  9. File.AppendAllText(filePath, content);
  10. Console.ReadKey();
  11. }
  12. }
  13. }

效果:

第四种:追加多行字符串

按上面的套路,添加多行当然也是可以的,不过用的方法名要换一个了

使用 File.AppendAllLines 来添加多行文字。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Test1
  8. {
  9. internal class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string filePath = @"E:\myFile.txt";
  14. string[] list = { "\n恭喜", "发财", "红包", "拿来" };
  15. File.AppendAllLines(filePath, list, Encoding.UTF8);
  16. Console.ReadKey();
  17. }
  18. }
  19. }

运行后:

四、读取文件

读取文件可以使用下面方式:

1.File.ReadAllText(FilePath)
2.File.ReadAllText(FilePath, Encoding) ----指定编码
3.File.ReadAllLines(FilePath)
4.File.ReadAllLines(FilePath, Encoding) ----指定编码

以字符串接收方式

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace 文件操作
  8. {
  9. internal class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string path = @"E:\myFile.txt";
  14. //string content = File.ReadAllText(path);
  15. string content = File.ReadAllText(path,Encoding.UTF8);
  16. Console.WriteLine(content);
  17. Console.ReadKey();
  18. }
  19. }
  20. }

以字符串数组接收的方式

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace 文件操作
  8. {
  9. internal class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string path = @"E:\myFile.txt";
  14. //string[] content = File.ReadAllLines(path);
  15. string[] content = File.ReadAllLines(path, Encoding.UTF8);
  16. for (int i = 0; i < content.Length; i++)
  17. {
  18. Console.WriteLine(content[i]);
  19. }
  20. Console.ReadKey();
  21. }
  22. }
  23. }

采用流(Stream)的方式来读取内容

1.StreamReader(FilePath)
2.StreamReader(FilePath, Encoding)
3.StreamReader(FileStream)
4.StreamReader(FileStream, Encoding)
5.File.OpenText(FilePath)
6.FileInfo.OpenText()

基于StreamReader,一行一行读取

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace 文件操作
  8. {
  9. internal class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string path = @"E:\myFile.txt";
  14. //StreamReader sr1 = new StreamReader(path);
  15. //StreamReader sr2 = new StreamReader(path, Encoding.UTF8);
  16. //初始化FileStream
  17. FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
  18. //StreamReader sr3 = new StreamReader(fs);
  19. StreamReader sr4 = new StreamReader(fs, Encoding.UTF8);
  20. string line = string.Empty;
  21. while((line = sr4.ReadLine()) != null)
  22. {
  23. Console.WriteLine(line);
  24. }
  25. sr4.Close();
  26. Console.ReadKey();
  27. }
  28. }
  29. }

一次性读完

  1. string path = @"E:\myFile.txt";
  2. StreamReader sr = new StreamReader(path, Encoding.UTF8);
  3. string content = sr.ReadToEnd();
  4. Console.WriteLine(content);
  5. sr.Close();

五、复制文件

要复制文件,可以使用System.IO.File.Copy()方法。该方法接受源文件路径和目标文件路径作为参数,并将源文件复制到目标文件。如果目标文件已存在,则将被覆盖。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace 文件操作
  8. {
  9. internal class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string sourceFilePath = @"E:\myFile.txt";
  14. string destinationFilePath = @"E:\myFile_copy.txt";
  15. File.Copy(sourceFilePath, destinationFilePath);
  16. Console.ReadKey();
  17. }
  18. }
  19. }

打开对应的目录,可以看到拷贝成功了,而且字节数也是一样的

六、移动文件

要移动文件,可以使用System.IO.File.Move()方法。该方法接受源文件路径和目标文件路径作为参数,并将源文件移动到目标文件。如果目标文件已存在,则将被覆盖。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace 文件操作
  8. {
  9. internal class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string sourceFilePath = @"E:\myFile.txt";
  14. string destinationFilePath = @"D:\myFile.txt";
  15. File.Move(sourceFilePath, destinationFilePath);
  16. Console.ReadKey();
  17. }
  18. }
  19. }

七、重命名文件

要重命名文件,可以使用System.IO.File.Move()方法。该方法接受源文件路径和目标文件路径作为参数,并将源文件重命名为目标文件。如果目标文件已存在,则将被覆盖。

在上一节 移动文件 中,我们将 myFile.txt 文件移动到 D 盘了,执行下面操作之前,要先剪切回来再执行操作哦

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace 文件操作
  8. {
  9. internal class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string filePath = @"E:\myFile.txt";
  14. string newFilePath = @"E:\myFile_new.txt";
  15. File.Move(filePath, newFilePath);
  16. Console.ReadKey();
  17. }
  18. }
  19. }

运行后,可以看到效果了

 

八、删除文件

要删除文件,可以使用System.IO.File.Delete()方法。该方法接受文件路径作为参数,并将该文件从磁盘上删除。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace 文件操作
  8. {
  9. internal class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string newFilePath = @"E:\myFile_new.txt";
  14. File.Delete(newFilePath);
  15. Console.ReadKey();
  16. }
  17. }
  18. }

结束

如果这个帖子对你有所帮助,欢迎 关注 + 点赞 + 留言

end

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/913799
推荐阅读
相关标签
  

闽ICP备14008679号