简体   繁体   English

C#:将文本文件内容与字符串变量进行比较

[英]C#: Compare text file contents to a string variable

I have an application that dumps text to a text file. 我有一个将文本转储到文本文件的应用程序。 I think there might be an issue with the text not containing the proper carriage returns, so I'm in the process of writing a test that will compare the contents of of this file to a string variable that I declare in the code. 我认为文本可能存在一个问题,即不包含正确的回车符,所以我正在编写一个测试,将该文件的内容与我在代码中声明的字符串变量进行比较。

Ex: 例如:

1) Code creates a text file that contains the text: 1)代码创建一个包含文本的文本文件:

This is line 1
This is line 2
This is line 3

2) I have the following string that I want to compare it to: 2)我有以下字符串,我想比较它:

string testString = "This is line 1\nThis is line 2\nThis is line3"

I understand that I could open a file stream reader and read the text file line by line and store that in a mutable string variable while appending "\\n" after each line, but wondering if this is re-inventing the wheel (other words, .NET has a built in class for something like this). 据我所知,我可以打开文件流阅读器并逐行读取文本文件并将其存储在可变字符串变量中,同时在每行后附加“\\ n”,但想知道这是否重新发明了轮子(换句话说, .NET有一个类似于此的内置类)。 Thanks in advance. 提前致谢。

you can either use StreamReader's ReadToEnd() method to read contents in a single string like 你可以使用StreamReader的ReadToEnd()方法来读取单个字符串中的内容

    using System.IO;

    using(StreamReader streamReader = new StreamReader(filePath))
    {
       string text = streamReader.ReadToEnd();
    }

Note: you have to make sure that you release the resources (above code uses "using" to do that) and ReadToEnd() method assumes that stream knows when it has reached an end. 注意:您必须确保释放资源(上面的代码使用“using”来执行此操作)并且ReadToEnd()方法假定流知道它何时到达结束。 For interactive protocols in which the server sends data only when you ask for it and does not close the connection, ReadToEnd might block indefinitely because it does not reach an end, and should be avoided and also you should take care that current position in the string should be at the start. 对于服务器仅在您请求数据时发送数据并且不关闭连接的交互式协议,ReadToEnd可能会无限期地阻塞,因为它没有达到目的,应该避免,并且您应该注意字符串中的当前位置应该在一开始。

You can also use ReadAllText like 您也可以使用ReadAllText

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

which is simple it opens a file, reads all lines and takes care of closing as well. 这很简单,它打开一个文件,读取所有行,并负责关闭。

No, there is nothing built in for this. 不,没有内置任何东西。 The easiest way, assuming that your file is small, is to just read the whole thing and compare them: 假设您的文件很小,最简单的方法就是阅读整个内容并进行比较:

var fileContents = File.ReadAllText(fileName);
return testString == filecontents;

If the file is fairly long, you may want to compare the file line by line, since finding a difference early on would allow you to reduce IO. 如果文件相当长,您可能希望逐行比较文件,因为尽早找到差异可以减少IO。

A faster way to implement reading all the text in a file is System.IO.File.ReadAllText() 实现读取文件中所有文本的更快方法是System.IO.File.ReadAllText()

but theres no way to do the string level comparison shorter 但是没有办法把字符串级别的比较缩短

if(System.IO.File.ReadAllText(filename) == "This is line 1\nThis is line 2\nThis is line3") {
   // it matches
}

This should work: 这应该工作:

StreamReader streamReader = new StreamReader(filePath);
string originalString = streamReader.ReadToEnd();
streamReader.Close();

I don't think there is a quicker way of doing it in C#. 我不认为在C#中有更快的方法。

You can read the entire file into a string variable this way: 您可以通过以下方式将整个文件读入字符串变量:

        FileStream stream;
        StreamReader reader;
        stream = new FileStream(yourFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
        reader = new StreamReader(stream);
        string stringContainingFilesContent = reader.ReadToEnd();
        // and check for your condition
        if (testString.Equals(stringContainingFilesContent, StringComparison.OrdinalIgnoreCase))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM