在c#中有很多不同的方法来读写文件(文本文件,而不是二进制文件)。

我只需要一些简单的东西,使用最少的代码,因为我将在我的项目中大量使用文件。我只需要一些字符串,因为我所需要的是读写字符串。


当前回答

@AlexeiLevenkov指出了另一种“最简单的方法”,即扩展方法。它只需要一点点编码,然后提供绝对最简单的读/写方式,再加上它提供了根据个人需求创建变化的灵活性。下面是一个完整的例子:

这定义了字符串类型的扩展方法。注意,唯一真正重要的是带有额外关键字this的函数参数,这使得它指向方法所附加的对象。类名无关紧要;类和方法必须声明为静态的。

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

这是如何使用字符串扩展方法,注意它自动引用类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

我自己永远不会发现这个,但它很棒,所以我想分享这个。玩得开心!

其他回答

您正在寻找File、StreamWriter和StreamReader类。

简单:

String inputText = "Hello World!";

File.WriteAllText("yourfile.ext",inputText); //writing

var outputText = File.ReadAllText("yourfile.ext"); //reading
using (var file = File.Create("pricequote.txt"))
{
    ...........                        
}

using (var file = File.OpenRead("pricequote.txt"))
{
    ..........
}

简单,容易,也处置/清理对象一旦你完成它。

使用文件。ReadAllText和File.WriteAllText。

MSDN示例节选:

// Create a file to write to.
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);

...

// Open the file to read from.
string readText = File.ReadAllText(path);

@AlexeiLevenkov指出了另一种“最简单的方法”,即扩展方法。它只需要一点点编码,然后提供绝对最简单的读/写方式,再加上它提供了根据个人需求创建变化的灵活性。下面是一个完整的例子:

这定义了字符串类型的扩展方法。注意,唯一真正重要的是带有额外关键字this的函数参数,这使得它指向方法所附加的对象。类名无关紧要;类和方法必须声明为静态的。

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

这是如何使用字符串扩展方法,注意它自动引用类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

我自己永远不会发现这个,但它很棒,所以我想分享这个。玩得开心!