如何写入嵌入式资源? [英] How To Write To An Embedded Resource?

查看:74
本文介绍了如何写入嵌入式资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试执行以下代码时,我都会不断收到错误流不可写。我了解到内存中仍然有对流的引用,但我不知道如何解决该问题。这两个代码块按顺序调用。我认为第二个可能是函数调用,也可能是调用堆栈中更深的两个函数,但是我认为这并不重要,因为我在第一个块中使用了 using语句,可以自动清除流。我确定这是C#中的常见任务,我只是不知道该怎么做...

I keep getting the error "Stream was not writable" whenever I try to execute the following code. I understand that there's still a reference to the stream in memory, but I don't know how to solve the problem. The two blocks of code are called in sequential order. I think the second one might be a function call or two deeper in the call stack, but I don't think this should matter, since I have "using" statements in the first block that should clean up the streams automatically. I'm sure this is a common task in C#, I just have no idea how to do it...

string s = "";

using (Stream manifestResourceStream =
    Assembly.GetExecutingAssembly().GetManifestResourceStream("Datafile.txt"))
{
    using (StreamReader sr = new StreamReader(manifestResourceStream))
    {
        s = sr.ReadToEnd();
    }
}

...

string s2 = "some text";

using (Stream manifestResourceStream =
    Assembly.GetExecutingAssembly().GetManifestResourceStream("Datafile.txt"))
{
    using (StreamWriter sw = new StreamWriter(manifestResourceStream))
    {
        sw.Write(s2);
    }
}

任何帮助将不胜感激。谢谢!

Any help will be very much appreciated. Thanks!

Andrew

推荐答案

嵌入式资源已编译到您的程序集中,则无法对其进行编辑。

Embedded resources are compiled into your assembly, you can't edit them.

这篇关于如何写入嵌入式资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆