c#中将字符串转换为文件流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17833080/
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 10:36:51  来源:igfitidea点击:

Convert string to filestream in c#

c#.netvisual-studio-2010.net-4.0filestream

提问by now he who must not be named.

Just started with writing unit tests and I am now, blocked with this situation:

刚开始编写单元测试,我现在被这种情况阻止了:

I have a method which has a FileStream object and I am trying to pass a "string" to it. So, I would like to convert my string to FileStream and I am doing this:

我有一个方法,它有一个 FileStream 对象,我试图向它传递一个“字符串”。所以,我想将我的字符串转换为 FileStream,我正在这样做:

File.WriteAllText(string.Concat(Environment.ExpandEnvironmentVariables("%temp%"),   
 @"/test.txt"), testFileContent); //writes my string to a temp file!


new FileStream(string.Concat(Environment.ExpandEnvironmentVariables("%temp%"),  
    @"/test.txt"), FileMode.Open) //open that temp file and uses it as a fileStream!

close the file then!

然后关闭文件!

But, I guess there must be some very simple alternative to convert a string to a fileStream.

但是,我想一定有一些非常简单的替代方法可以将字符串转换为 fileStream。

Suggestions are welcome! [Note there are other answers to this question in stackoverflow but none seems to be a straight forward solution to that]

欢迎提出建议![请注意,stackoverflow 中有此问题的其他答案,但似乎没有一个是直接的解决方案]

Thanks in advance!

提前致谢!

回答by Piotr Stapp

First of all change your method to allow Streaminstead of FileStream. FileStreamis an implementation which, as I remember, does not add any methods or properties, just implement abstract class Stream. And then using below code you can convert stringto Stream:

首先将您的方法更改为允许Stream而不是FileStream. FileStream是一个实现,我记得,它不添加任何方法或属性,只实现抽象类Stream。然后使用下面的代码,您可以转换stringStream

public Stream GenerateStreamFromString(string s)
{
    MemoryStream stream = new MemoryStream();
    StreamWriter writer = new StreamWriter(stream);
    writer.Write(s);
    writer.Flush();
    stream.Position = 0;
    return stream;
}

回答by Sheo Dayal Singh

As FileStream class provides a stream for a file and hence it's constructor requires the path of the file,mode, permission parameter etc. to read the file into stream and hence it is used to read the text from file into stream. If we need to convert string to stream first we need to convert string to bytes array as stream is a sequence of bytes. Below is the code.

由于 FileStream 类为文件提供流,因此它的构造函数需要文件的路径、模式、权限参数等才能将文件读入流,因此它用于将文件中的文本读入流。如果我们需要先将字符串转换为流,我们需要将字符串转换为字节数组,因为流是一个字节序列。下面是代码。

   //Stream is a base class it holds the reference of MemoryStream
            Stream stream = new MemoryStream();
            String strText = "This is a String that needs to beconvert in stream";
             byte[] byteArray = Encoding.UTF8.GetBytes(strText);
             stream.Write(byteArray, 0, byteArray.Length);
             //set the position at the beginning.
             stream.Position = 0;
             using (StreamReader sr = new StreamReader(stream))
                        {
                           string strData;
                           while ((strData= sr.ReadLine()) != null)
                            {
                                Console.WriteLine(strData);
                            }
                        }