using System;
using System.IO;
class Program
{
static void Main()
{
string textToWrite = "这是要写入文件的文本";
string filePath = @"C:\example\test.txt";
try
{
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.Write(textToWrite);
Console.WriteLine("文本已成功写入文件。");
}
}
catch (FileNotFoundException ex)
{
Console.WriteLine($"文件未找到异常: {ex.Message}");
}
catch (IOException ex)
{
Console.WriteLine($"IO 异常: {ex.Message}");
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine($"权限不足异常: {ex.Message}");
}
}
}