博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读取和写入文件的最简单方法
阅读量:3581 次
发布时间:2019-05-20

本文共 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。


#1楼

参考:


#2楼

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方法。


#3楼

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");    }}

#4楼

@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! 玩得开心!


#5楼

Or, if you are really about lines: 或者,如果您确实是关于行的:

System.IO.File also contains a static method WriteAllLines , so you could do: System.IO.File还包含一个静态方法WriteAllLines ,因此您可以执行以下操作:

IList
myLines = new List
(){ "line1", "line2", "line3",};File.WriteAllLines("./foo", myLines);

#6楼

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/

你可能感兴趣的文章
GOF(一) 代理模式(Proxy pattern)
查看>>
web面试题整理
查看>>
MyBatis+Spring+SpringMVC框架面试题整理(一)
查看>>
MyBatis+Spring+SpringMVC框架面试题整理(二)
查看>>
MyBatis面试题 如何构建一个线程安全的SqlSession
查看>>
Java 面试题整理(一)
查看>>
Java 面试题整理(二)
查看>>
实现:JDK动态代理和CGLIB动态代理
查看>>
Java 面试题整理(三)
查看>>
MyBatis+Spring+SpringMVC框架面试题整理(三)
查看>>
Java 面试题整理(四)
查看>>
MySQL面试题
查看>>
Windows10 安装DB2数据库
查看>>
db2 解决load数据时报row found on wrong partition
查看>>
Spring Boot 使用 druid-spring-boot-starter 整合druid
查看>>
牛客 - Java专项练习题知识点整理(一)
查看>>
牛客 - Java专项练习题知识点整理(二)
查看>>
牛客 - Java专项练习题知识点整理(三)
查看>>
牛客 - Java专项练习题知识点整理(四)
查看>>
牛客 - Java专项练习题知识点整理(五)
查看>>