本文共 3606 字,大约阅读时间需要 12 分钟。
本文翻译自:
There are a lot of different ways to read and write files ( text files , not binary) in C#. 在C#中,有许多不同的方式来读写文件( 文本文件 ,而不是二进制文件)。
I just need something that is easy and uses the least amount of code, because I am going to be working with files a lot in my project. 我只需要一些简单且使用最少代码的东西,因为我将在项目中处理很多文件。 I only need something for string
since all I need is to read and write string
s. 我只需要一些string
因为我需要的是读写string
s。
参考:
It's good when reading to use the OpenFileDialog control to browse to any file you want to read. 读取时最好使用OpenFileDialog控件浏览到要读取的任何文件。 Find the code below: 查找下面的代码:
Don't forget to add the following using
statement to read files: using System.IO;
不要忘记添加以下using
语句来读取文件: using System.IO;
private void button1_Click(object sender, EventArgs e){ if (openFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = File.ReadAllText(openFileDialog1.FileName); }}
To write files you can use the method File.WriteAllText
. 要写入文件,可以使用File.WriteAllText
方法。
FileStream fs = new FileStream(txtSourcePath.Text,FileMode.Open, FileAccess.Read);using(StreamReader sr = new StreamReader(fs)){ using (StreamWriter sw = new StreamWriter(Destination)) { sw.writeline("Your text"); }}
@AlexeiLevenkov pointed me at another "easiest way" namely the . @AlexeiLevenkov向我指出了另一种“最简单的方法”,即 。 It takes just a little coding, then provides the absolute easiest way to read/write, plus it offers the flexibility to create variations according to your personal needs. 它只需要一点编码,然后提供绝对最简单的读/写方式,此外,它还提供了根据您的个人需求创建变体的灵活性。 Here is a complete example: 这是一个完整的示例:
This defines the extension method on the string
type. 这定义了string
类型的扩展方法。 Note that the only thing that really matters is the function argument with extra keyword this
, that makes it refer to the object that the method is attached to. 请注意,唯一真正重要的是带有额外关键字this
的function参数,这使其引用该方法所附加的对象。 The class name does not matter; 类名无关紧要; the class and method must be declared static
. 该类和方法必须声明为static
。
using System.IO;//File, Directory, Pathnamespace Lib{ ////// Handy string methods /// public static class Strings { ////// Extension method to write the string Str to a file /// /// /// public static void WriteToFile(this string Str, string Filename) { File.WriteAllText(Filename, Str); return; } // of course you could add other useful string methods... }//end class}//end ns
This is how to use the string extension method
, note that it refers automagically to the class Strings
: 这是使用string extension method
,请注意,它自动引用了class Strings
:
using Lib;//(extension) method(s) for stringnamespace ConsoleApp_Sandbox{ class Program { static void Main(string[] args) { "Hello World!".WriteToFile(@"c:\temp\helloworld.txt"); return; } }//end class}//end ns
I would never have found this myself, but it works great, so I wanted to share this. 我自己永远不会找到这个,但是它很好用,所以我想分享一下。 Have fun! 玩得开心!
Or, if you are really about lines: 或者,如果您确实是关于行的:
System.IO.File also contains a static method WriteAllLines , so you could do: System.IO.File还包含一个静态方法WriteAllLines ,因此您可以执行以下操作:
IListmyLines = new List (){ "line1", "line2", "line3",};File.WriteAllLines("./foo", myLines);
The easiest way to read from a file and write to a file: 从文件读取并写入文件的最简单方法:
//Read from a filestring something = File.ReadAllText("C:\\Rfile.txt");//Write to a fileusing (StreamWriter writer = new StreamWriter("Wfile.txt")){ writer.WriteLine(something);}
转载地址:http://xmlgj.baihongyu.com/