读取和写入文件的最简单方法

本文翻译自:Easiest way to read from and write to files

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楼

参考:https://stackoom.com/question/VlHE/读取和写入文件的最简单方法


#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 extension method . @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, Path

namespace Lib
{
    /// <summary>
    /// Handy string methods
    /// </summary>
    public static class Strings
    {
        /// <summary>
        /// Extension method to write the string Str to a file
        /// </summary>
        /// <param name="Str"></param>
        /// <param name="Filename"></param>
        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 string
namespace 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<string> myLines = new List<string>()
{
    "line1",
    "line2",
    "line3",
};

File.WriteAllLines("./foo", myLines);

#6楼

The easiest way to read from a file and write to a file: 从文件读取并写入文件的最简单方法:

//Read from a file
string something = File.ReadAllText("C:\\Rfile.txt");

//Write to a file
using (StreamWriter writer = new StreamWriter("Wfile.txt"))
{
    writer.WriteLine(something);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值