更改嵌入式资源文件中的内容 [英] Changing content in Embedded Resources file

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

问题描述

我想更改作为 加载的文件的内容来自嵌入资源的流.

以下代码获取文件:

Stream theFile = Assembly.GetExecutingAssembly().GetManifestResourceStream("_3LinksFourmTool.Resources.fourmlinks.txt");

我创建了一个方法,该方法采用 Stream 提供.使用新内容将字符串重写为 Stream.

I created a method that takes a string of text that is present in the Stream provided. The string is rewritten to the Stream with the new content.

public static void WriteNewTextToFile(string text, Stream theFile)
{

    string fileText = GetAllTextFromFile(theFile);
    ArrayList fileLIst = populateListFromText(fileText);

    using (StreamWriter fileWriter = new StreamWriter(theFile))
    {
        fileWriter.Write("");
        for (int i = 0; i < fileLIst.Count; i++)
        {
            fileWriter.WriteLine(fileLIst[i].ToString());        
        }
    }
}

以上代码抛出系统.ArgumentException.

此异常与文本文件是 Embedded Resource 有什么关系吗?

Does this exception have anything to do with the text file being an Embedded Resource?

如何在没有 System.ArgumentException 被抛出?

How can I modify this file without the System.ArgumentException being thrown?

推荐答案

资源无法在运行时修改,部分原因是可执行文件当时正在执行,并且无法对其进行写入.您可以使用外部二进制编辑器来修改它们,如果您只有程序集,或者您可以使用更改后的文件重新编译您的项目.在大多数应用程序平台中,为了打开一个资源进行写入或执行,您必须首先从二进制文件中提取文件,然后对其执行操作.但是你不能把它放回去.

Resources cannot be modified at runtime, in part because the executable is executing at the time, and no writes could be made to it. you can use an external binary editor to modify them, if all you have is the assembly, or you can recompile your project with the altered file. In most app platforms, in order to open a resource for write or execute, you must extract the file from the binary first, and then perform your ops on it. you can;t put it back however.

这里有一些关于如何在设计/编译时操作资源的链接:http://msdn.microsoft.com/en-us/library/7k989cfy%28v=vs.90%29.aspxhttp://msdn.microsoft.com/en-us/library/cd818wbk%28v=vs.100%29.aspx

here are some links on how to manipluate resources at design/compile time: http://msdn.microsoft.com/en-us/library/7k989cfy%28v=vs.90%29.aspx http://msdn.microsoft.com/en-us/library/cd818wbk%28v=vs.100%29.aspx

在这种情况下,您是否考虑过使用设置而不是资源?它们可以在运行时保存,所以如果你只需要文本值的东西,那应该很适合你.

in this case, have you considered using Settings, instead of resources? They can be saved at runtime, so if you just need text value stuff, that should work well for you.

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

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