掌握C#文件操作:50个实用代码示例,轻松实现文件读写与管理

1. 创建文件
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
File.Create(filePath);
Console.WriteLine(“文件已创建。”);
}
}
2. 检查文件是否存在
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
if (File.Exists(filePath))
{
Console.WriteLine(“文件存在。”);
}
else
{
Console.WriteLine(“文件不存在。”);
}
}
}
3. 读取文件内容
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
string content = File.ReadAllText(filePath);
Console.WriteLine(“文件内容:\n” + content);
}
}
4. 写入文件内容
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
string content = “Hello, World!”;
File.WriteAllText(filePath, content);
Console.WriteLine(“内容已写入文件。”);
}
}
5. 追加文件内容
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
string content = “\nThis is a new line.”;
File.AppendAllText(filePath, content);
Console.WriteLine(“内容已追加到文件。”);
}
}
6. 读取文件内容到字符串数组
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
string[] lines = File.ReadAllLines(filePath);
foreach (string line in lines)
{
Console.WriteLine(line);
}
}
}
7. 写入字符串数组到文件
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
string[] lines = { “Hello”, “World”, “!” };
File.WriteAllLines(filePath, lines);
Console.WriteLine(“字符串数组已写入文件。”);
}
}
8. 读取文件大小
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
long length = new FileInfo(filePath).Length;
Console.WriteLine(“文件大小:” + length + ” bytes”);
}
}
9. 检查文件是否可读
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
if (File.Exists(filePath) && File.Exists(filePath))
{
Console.WriteLine(“文件可读。”);
}
else
{
Console.WriteLine(“文件不可读。”);
}
}
}
10. 检查文件是否可写
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
if (File.Exists(filePath) && File.Exists(filePath))
{
Console.WriteLine(“文件可写。”);
}
else
{
Console.WriteLine(“文件不可写。”);
}
}
}
11. 删除文件
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
File.Delete(filePath);
Console.WriteLine(“文件已删除。”);
}
}
12. 重命名文件
using System;
using System.IO;

class Program
{
static void Main()
{
string oldPath = @”C:\example\example.txt”;
string newPath = @”C:\example\newExample.txt”;
File.Move(oldPath, newPath);
Console.WriteLine(“文件已重命名。”);
}
}
13. 创建目录
using System;
using System.IO;

class Program
{
static void Main()
{
string directoryPath = @”C:\example”;
Directory.CreateDirectory(directoryPath);
Console.WriteLine(“目录已创建。”);
}
}
14. 检查目录是否存在
using System;
using System.IO;

class Program
{
static void Main()
{
string directoryPath = @”C:\example”;
if (Directory.Exists(directoryPath))
{
Console.WriteLine(“目录存在。”);
}
else
{
Console.WriteLine(“目录不存在。”);
}
}
}
15. 列出目录下的文件和子目录
using System;
using System.IO;

class Program
{
static void Main()
{
string directoryPath = @”C:\example”;
string[] files = Directory.GetFiles(directoryPath);
string[] directories = Directory.GetDirectories(directoryPath);
Console.WriteLine(“文件:”);
foreach (string file in files)
{
Console.WriteLine(file);
}
Console.WriteLine(“子目录:”);
foreach (string directory in directories)
{
Console.WriteLine(directory);
}
}
}
16. 创建临时文件
using System;
using System.IO;

class Program
{
static void Main()
{
string tempPath = Path.GetTempFileName();
Console.WriteLine(“临时文件路径:” + tempPath);
}
}
17. 创建临时目录
using System;
using System.IO;

class Program
{
static void Main()
{
string tempPath = Path.GetTempPath();
string tempDirectoryPath = Path.Combine(tempPath, “tempDir”);
Directory.CreateDirectory(tempDirectoryPath);
Console.WriteLine(“临时目录路径:” + tempDirectoryPath);
}
}
18. 读取文件内容到字节数组
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
byte[] content = File.ReadAllBytes(filePath);
Console.WriteLine(“文件内容字节数组:”);
foreach (byte b in content)
{
Console.WriteLine(b);
}
}
}
19. 写入字节数组到文件
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
byte[] content = { 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33 };
File.WriteAllBytes(filePath, content);
Console.WriteLine(“字节数组已写入文件。”);
}
}
20. 读取文件内容到流
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (StreamReader sr = new StreamReader(fs))
{
string content = sr.ReadToEnd();
Console.WriteLine(“文件内容:\n” + content);
}
}
}
}
21. 写入文件内容到流
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
string content = “Hello, World!”;
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(content);
}
}
Console.WriteLine(“内容已写入文件。”);
}
}
22. 读取文件内容到二进制流
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] content = br.ReadBytes((int)fs.Length);
Console.WriteLine(“文件内容二进制流:”);
foreach (byte b in content)
{
Console.WriteLine(b);
}
}
}
}
}
23. 写入文件内容到二进制流
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
byte[] content = { 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33 };
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
bw.Write(content);
}
}
Console.WriteLine(“内容已写入文件。”);
}
}
24. 读取文件内容到文本流
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (StreamReader sr = new StreamReader(fs))
{
string content = sr.ReadToEnd();
Console.WriteLine(“文件内容:\n” + content);
}
}
}
}
25. 写入文件内容到文本流
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
string content = “Hello, World!”;
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(content);
}
}
Console.WriteLine(“内容已写入文件。”);
}
}
26. 读取文件内容到二进制流(缓冲区)
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
{
Console.WriteLine(“文件内容二进制流(缓冲区):”);
foreach (byte b in buffer)
{
Console.WriteLine(b);
}
}
}
}
}
27. 写入文件内容到二进制流(缓冲区)
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
byte[] content = { 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33 };
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
byte[] buffer = new byte[content.Length];
int bytesWritten;
for (int i = 0; i < content.Length; i += buffer.Length) { bytesWritten = fs.Write(content, i, Math.Min(content.Length - i, buffer.Length)); } } Console.WriteLine("内容已写入文件。"); } } 28. 读取文件内容到文本流(缓冲区) using System; using System.IO; class Program { static void Main() { string filePath = @"C:\example\example.txt"; using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
{
Console.WriteLine(“文件内容文本流(缓冲区):”);
Console.WriteLine(System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead));
}
}
}
}
29. 写入文件内容到文本流(缓冲区)
using System;
using System.IO;

class Program
{
static void Main()
{
string filePath = @”C:\example\example.txt”;
string content = “Hello, World!”;
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
byte[] buffer = new byte[content.Length];
int bytesWritten;
for (int i = 0; i < content.Length; i += buffer.Length) { bytesWritten = fs.Write(System.Text.Encoding.UTF8.GetBytes(content), i, Math.Min(content.Length - i, buffer.Length)); } } Console.WriteLine("内容已写入文件。"); } } 30. 读取文件内容到内存 using System; using System.IO; class Program { static void Main() { string filePath = @"C:\example\example.txt"; byte[] content = File.ReadAllBytes(filePath); Console.WriteLine("文件内容内存:"); foreach (byte b in content) { Console.WriteLine(b); } } } 31. 写入文件内容到内存 using System; using System.IO; class Program { static void Main() { string filePath = @"C:\example\example.txt"; byte[] content = { 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33 }; using (MemoryStream ms = new MemoryStream()) { ms.Write(content, 0, content.Length); ms.Position = 0; byte[] buffer = new byte[content.Length]; int bytesRead = ms.Read(buffer, 0, buffer.Length); File.WriteAllBytes(filePath, buffer); } Console.WriteLine("内容已写入文件。"); } } 32. 读取文件内容到字符串 using System; using System.IO; class Program { static void Main() { string filePath = @"C:\example\example.txt"; string content = File.ReadAllText(filePath); Console.WriteLine("文件内容字符串:\n" + content); } } 33. 写入文件内容到字符串 using System; using System.IO; class Program { static void Main() { string filePath = @"C:\example\example.txt"; string content = "Hello, World!"; File.WriteAllText(filePath, content); Console.WriteLine("内容已写入文件。"); } } 34. 读取文件内容到字符串数组 using System; using System.IO; class Program { static void Main() { string filePath = @"C:\example\example.txt"; string[] lines = File.ReadAllLines(filePath); Console.WriteLine("文件内容字符串数组:"); foreach (string line in lines) { Console.WriteLine(line); } } } 35. 写入文件内容到字符串数组 using System; using System.IO; class Program { static void Main() { string filePath = @"C:\example\example.txt"; string[] lines = { "Hello", "World", "!" }; File.WriteAllLines(filePath, lines); Console.WriteLine("字符串数组已写入文件。"); } } 36. 读取文件内容到字节 using System; using System.IO; class Program { static void Main() { string filePath = @"C:\example\example.txt"; byte[] content = File.ReadAllBytes(filePath); Console.WriteLine("文件内容字节:"); foreach (byte b in content) { Console.WriteLine(b); } } } 37. 写入文件内容到字节 using System; using System.IO; class Program { static void Main() { string filePath = @"C:\example\example.txt"; byte[] content = { 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33 }; File.WriteAllBytes(filePath, content); Console.WriteLine("内容已写入文件。"); } } 38. 读取文件内容到流 using System; using System.IO; class Program { static void Main() { string filePath = @"C:\example\example.txt"; using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { using (StreamReader sr = new StreamReader(fs)) { string content = sr.ReadToEnd(); Console.WriteLine("文件内容流:\n" + content); } } } } 39. 写入文件内容到流 using System; using System.IO; class Program { static void Main() { string filePath = @"C:\example\example.txt"; string content = "Hello, World!"; using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(content); } } Console.WriteLine("内容已写入文件。"); } } 40. 读取文件内容到二进制流 using System; using System.IO; class Program { static void Main() { string filePath = @"C:\example\example.txt"; using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { using (BinaryReader br = new BinaryReader(fs)) { byte[] content = br.ReadBytes((int)fs.Length); Console.WriteLine("文件内容二进制流:"); foreach (byte b in content) { Console.WriteLine(b); } } } } } 41. 写入文件内容到二进制流 ”`csharp using System; using System.IO; class Program { static void Main() { string filePath = @"C:\example\example.txt"; byte[] content = { 72, 101, 108, 108, 111, 44, 32, 87, 111