在C#中进行字符串IO操作,可以使用以下代码示例:
写入字符串到文件:string text = "Hello, World!";string path = "test.txt";// 写入文件File.WriteAllText(path, text);从文件中读取字符串:string path = "test.txt";// 读取文件string text = File.ReadAllText(path);Console.WriteLine(text);使用StreamWriter写入字符串到文件:string text = "Hello, World!";string path = "test.txt";// 使用StreamWriter写入文件using (StreamWriter writer = new StreamWriter(path)){ writer.Write(text);}使用StreamReader从文件中读取字符串:string path = "test.txt";// 使用StreamReader读取文件using (StreamReader reader = new StreamReader(path)){ string text = reader.ReadToEnd(); Console.WriteLine(text);}这些示例演示了如何在C#中进行字符串的IO操作,包括写入和读取文件中的字符串。您可以根据实际需求选择适合的方法来操作字符串数据。




