C# 如何将 FileStream 更改为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15279455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:15:46  来源:igfitidea点击:

How to change a FileStream to a string?

c#filestream

提问by rotaercz

In have the following code how can I change the stream to taking in a string variable?

在以下代码中,如何将流更改为接收字符串变量?

        // open dictionary file
        FileStream fs = new FileStream(dictionaryPath, FileMode.Open, FileAccess.Read, FileShare.Read);
        StreamReader sr = new StreamReader(fs, Encoding.UTF8);

        // read line by line
        while (sr.Peek() >= 0) 
        {
            string tempLine = sr.ReadLine().Trim();
            if (tempLine.Length > 0)
            {
                // check for section flag
                switch (tempLine)
                {
                    case "[Copyright]" :
                    case "[Try]" : 
                    case "[Replace]" : 
                    case "[Prefix]" :

                    ...
                    ...
                    ...

采纳答案by Lee

It looks like you only need to call ReadLine()- in this case you can change the type of srto TextReader.

看起来您只需要调用ReadLine()- 在这种情况下,您可以将类型更改srTextReader

You can then replace your StreamReaderwith a StringReaderand pass in the string you want to use:

然后,您可以将您的替换StreamReaderStringReader并传入您要使用的字符串:

TextReader sr = new StringReader(inputString);

回答by Alexandre Vin?on

Do you mean StringReader? It creates a stream that read the content of a string.

你的意思是StringReader吗?它创建一个读取字符串内容的流。

回答by Steve

If you have a string and you want to read from it like it was a stream:

如果您有一个字符串,并且想像读取流一样读取它:

byte[] byteArray = Encoding.ASCII.GetBytes(theString);
MemoryStream stream = new MemoryStream(byteArray);

回答by T-moty

My suggestion.. "stay away of streams, if you can"

我的建议..“如果可以,请远离溪流”

In this case, you can

在这种情况下,您可以

1) Read All File in a string variable;

1) 读取字符串变量中的所有文件;

2) Split it in array of strings at the line-end chars (\r\n)

2) 在行尾字符 ( \r\n)处将其拆分为字符串数组

3) Do a simple foreachcycle and put your switch statement inside it

3)做一个简单的foreach循环,把你的switch语句放在里面

Little Example:

小例子:

string dictionaryPath = @"C:\MyFile.ext";

string dictionaryContent = string.empty;

try // intercept file not exists, protected, etc..
{
    dictionaryContent = File.ReadAllText(dictionaryPath);
}
catch (Exception exc)
{
    // write error in log, or prompt it to user
    return; // exit from method
}

string[] dictionary = dictionaryContent.Split(new[] { "\r\n" }, StringSplitOptions.None);

foreach (string entry in dictionary)
{
    switch (entry)
    {
        case "[Copyright]":
            break;

        case "[Try]":
            break;

        default:
            break;
    }
}

Hope this help!

希望这有帮助!