关于File.ReadAllText Method (String)

Posted wolf96

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于File.ReadAllText Method (String)相关的知识,希望对你有一定的参考价值。

File.ReadAllText Method (String)

用在读取电量了,但是发现他没有关闭流,查了一下

https://msdn.microsoft.com/en-us/library/ms143368(v=vs.110).aspx

Remarks

This method opens a file, reads each line of the file, and then adds each line as an element of a string. It then closes the file. A line is defined as a sequence of characters followed by a carriage return ('\\r'), a line feed ('\\n'), or a carriage return immediately followed by a line feed. The resulting string does not contain the terminating carriage return and/or line feed.


读取全部行的信息,之后就自己关闭了,mark一下


using System;
using System.IO;
using System.Text;

class Test

    public static void Main()
    
        string path = @"c:\\temp\\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        
            // Create a file to write to.
            string createText = "Hello and Welcome" + Environment.NewLine;
            File.WriteAllText(path, createText);
        

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText);

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



by wolf96 2017/7/13




以上是关于关于File.ReadAllText Method (String)的主要内容,如果未能解决你的问题,请参考以下文章

为啥 File.ReadAllBytes 结果与使用 File.ReadAllText 时不同?

File.ReadAllText 中的字符无效

文件操作

C#中,怎么使用富文本框控件显示一个文件的内容

Xamarin 读取共享项目中的文件

ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”