如何写入嵌入式资源?

发布于 2024-09-19 21:23:27 字数 790 浏览 2 评论 0 原文

每当我尝试执行以下代码时,我总是收到错误“Stream is not writable”。我知道内存中仍然存在对流的引用,但我不知道如何解决该问题。这两个代码块按顺序调用。我认为第二个可能是调用堆栈中的一个或两个更深的函数调用,但我认为这并不重要,因为我在第一个块中有“using”语句,应该自动清理流。我确信这是 C# 中的一项常见任务,我只是不知道如何去做......

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);
    }
}

帮助将非常感激。谢谢!

安德鲁

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丿*梦醉红颜 2024-09-26 21:23:27

嵌入式资源被编译到您的程序集中,您无法编辑它们。

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

〆凄凉。 2024-09-26 21:23:27

如上所述,嵌入式资源是只读的。我的建议是,如果适用(例如,您的嵌入式资源是数据库文件、XML、CSV 等),则将空白资源提取到与程序相同的位置,然后读取/写入提取的资源。

示例伪代码:

if(!Exists(new PhysicalResource())) //Check to see if a physical resource exists.
{
    PhysicalResource.Create(); //Extract embedded resource to disk.
}

PhysicalResource pr = new PhysicalResource(); //Create physical resource instance.

pr.Read(); //Read from physical resource.

pr.Write(); //Write to physical resource.

希望这有帮助。

附加:

您的嵌入资源可能完全空白,包含数据结构和/或默认值。

As stated above, embedded resources are read only. My recommendation, should this be applicable, (say for example your embedded resource was a database file, XML, CSV etc.) would be to extract a blank resource to the same location as the program, and read/write to the extracted resource.

Example Pseudo Code:

if(!Exists(new PhysicalResource())) //Check to see if a physical resource exists.
{
    PhysicalResource.Create(); //Extract embedded resource to disk.
}

PhysicalResource pr = new PhysicalResource(); //Create physical resource instance.

pr.Read(); //Read from physical resource.

pr.Write(); //Write to physical resource.

Hope this helps.

Additional:

Your embedded resource may be entirely blank, contain data structure and / or default values.

断桥再见 2024-09-26 21:23:27

有点晚了,但为了后代=)
关于嵌入的.txt:

是的,在运行时您无法编辑嵌入的文件,因为它是嵌入的。您可以使用反汇编程序进行一些操作,但只能使用外部程序集,您将在当前上下文中加载它们。

如果您想在程序启动之前向资源写入一些实际信息,并且不将数据保存在单独的文件中,则有一个技巧。

我曾经使用过 winCE 和紧凑型 .Net,其中不允许使用 ResourceManager 在运行时存储字符串。我需要一些动态信息,以便在 dllNotFoundException 在启动时实际抛出之前捕获它。
所以我制作了嵌入式 txt 文件,我在预构建活动中填写了该文件。

像这样:

cd $(ProjectDir)
dir ..\bin\Debug /a-d /b> assemblylist.txt

这里我在调试文件夹中获取文件

并阅读:

using (var f = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("Market_invent.assemblylist.txt")))
        {
            str = f.ReadToEnd();
        }  

因此您可以在预构建事件运行一些exes中继续执行所有操作。

享受!它对于存储一些重要信息非常有用,有助于避免冗余操作。

A bit late, but for descendants=)
About embedded .txt:

Yep, on runtime you couldnt edit embedded because its embedded. You could play a bit with disassembler, but only with outter assemblies, which you gonna load in current context.

There is a hack if you wanna to write to a resource some actual information, before programm starts, and to not keep the data in a separate file.

I used to worked a bit with winCE and compact .Net, where you couldnt allow to store strings at runtime with ResourceManager. I needed some dynamic information, in order to catch dllNotFoundException before it actually throws on start.
So I made embedded txt file, which I filled at the pre-build event.

like this:

cd $(ProjectDir)
dir ..\bin\Debug /a-d /b> assemblylist.txt

here i get files in debug folder

and the reading:

using (var f = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("Market_invent.assemblylist.txt")))
        {
            str = f.ReadToEnd();
        }  

So you could proceed all your actions in pre-build event run some exes.

Enjoy! Its very usefull to store some important information and helps avoid redundant actions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文