C#读写文本文件。
我想写个单击butten1把文本文件里的内容读到textbox1中,再单击butten2,把textbox1中的文件写到另一个新建文本里,谢谢了,初学者,不会写,哪位高手能指点下,最好能详细点或给出代码,谢谢了,
[解决办法]
1.你可以直接用File.Move()方法或者FileInfo 的MoveTo()方法直接移动文件
System.IO操作
- C# code
try { // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. using (StreamReader sr = new StreamReader("TestFile.txt")) { String line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); }
[解决办法]
读:TextBox1.Text=File.ReadAllText("d:/b.txt",Encoding.Default);
写:File.WriteAllText("d:/a.txt", TextBox1.Text, Encoding.Default);
[解决办法]
追加:File.AppendAllText("d:/a.txt", TextBox1.Text, Encoding.Default);