C# 无法访问 memoryStream 的关闭 Stream,如何重新打开?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9508879/
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
Cannot access a closed Stream of a memoryStream, how to reopen?
提问by olidev
I have a memoryStream instance and it is closed.
我有一个 memoryStream 实例,它已关闭。
I already have tried:
我已经尝试过:
memoryStream.Flush();
memoryStream.Position=0;
To reopen the memory stream but it does not work. How can I reopen a closed memory stream?
重新打开内存流但它不起作用。如何重新打开关闭的内存流?
采纳答案by Reed Copsey
How can I reopen a closed memory stream?
如何重新打开关闭的内存流?
You can't reopen the stream. If you need to "reset" the stream, just assign it a new instance:
您无法重新打开流。如果您需要“重置”流,只需为其分配一个新实例:
memoryStream = new MemoryStream();
回答by Greg Biles
You can clone the original one and then use the clone, even when the original has been closed. Even though the original one is created with a capacity of 1000, ToArray() returns a 2 element array. ToBuffer() on the other hand gets you the entire buffer, which is what you don't want.
您可以克隆原始文件,然后使用该克隆文件,即使原始文件已关闭。即使最初创建的容量为 1000,ToArray() 也会返回一个 2 元素数组。另一方面, ToBuffer() 为您提供整个缓冲区,这是您不想要的。
MemoryStream original = new MemoryStream(1000);
original.WriteByte(4);
original.WriteByte(5);
MemoryStream dolly = new MemoryStream(original.ToArray());
dolly.Seek(0, SeekOrigin.Begin);
回答by Andrew Pope
try this:
尝试这个:
memoryStream = new MemoryStream(memoryStream.ToArray());
回答by RenniePet
This is an old, old question, but I'm reacting to the fact that the accepted answer isn't really useful and the highest-voted answer says that using .ToArray() is better than .GetBuffer(). I think that in many, perhaps most, situations using .GetBuffer() is significantly better than using .ToArray().
这是一个古老的问题,但我正在回应这样一个事实,即接受的答案并不是真的有用,并且投票最高的答案说使用 .ToArray() 比使用 .GetBuffer() 更好。我认为在很多情况下,也许是大多数情况下,使用 .GetBuffer() 明显优于使用 .ToArray()。
Here's an example of the problem of a memory stream getting closed despite your desire to continue using it:
这是一个尽管您希望继续使用内存流但仍然关闭的问题示例:
/// <summary>
/// Method that gets called by ManagedResource.WriteData() in project CodeAnalysis during code
/// emitting to get the data for an embedded resource file.
/// </summary>
/// <param name="resourceFullFilename">full path and filename for resource file to embed</param>
/// <returns>MemoryStream containing .resource file data - caller will dispose it</returns>
private static MemoryStream ProvideResourceData(string resourceFullFilename)
{
MemoryStream shortLivedBackingStream = new MemoryStream();
using (ResourceWriter resourceWriter = new ResourceWriter(shortLivedBackingStream))
{
using (ResXResourceReader resourceReader = new ResXResourceReader(resourceFullFilename))
{
IDictionaryEnumerator dictionaryEnumerator = resourceReader.GetEnumerator();
while (dictionaryEnumerator.MoveNext())
{
string resourceKey = dictionaryEnumerator.Key as string;
if (resourceKey != null) // Should not be possible
resourceWriter.AddResource(resourceKey, dictionaryEnumerator.Value);
}
}
}
return new MemoryStream(shortLivedBackingStream.GetBuffer());
}
The ResourceWriterclass needs a backing stream so I give it a newly-created memory stream. But when ResourceWriteris no longer needed it closes the backing stream. Then I create a new MemoryStreambased on the backing stream's buffer, which works fine, even though it is closed.
这个ResourceWriter类需要一个支持流,所以我给它一个新创建的内存流。但是当ResourceWriter不再需要时,它会关闭后备流。然后我MemoryStream基于后备流的缓冲区创建一个新的缓冲区,即使它已关闭,它也能正常工作。
In this situation I happen to know that the calling program is going to use the provided memory stream to copy the data to another buffer and then close the memory stream immediately. So there is no need to create a new byte array, and there is a performance advantage to notcreating a new byte array.
在这种情况下,我碰巧知道调用程序将使用提供的内存流将数据复制到另一个缓冲区,然后立即关闭内存流。所以,没有必要创建一个新的字节数组,并有性能优势不是建立一个新的字节数组。
Thanks to @JoshVarty for showing how to avoid the problem of MemoryStreamgetting closed here: https://github.com/dotnet/roslyn/issues/7791
感谢@JoshVarty 展示了如何避免在MemoryStream此处关闭的问题:https: //github.com/dotnet/roslyn/issues/7791

