Changing content in Embedded Resources file

谁都会走 提交于 2021-01-08 02:40:28

问题


I want to change the content of a file loaded as a Stream from an embedded resource.

The following code gets the file:

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

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

The above code throws the System.ArgumentException.

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

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


回答1:


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.

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.



来源:https://stackoverflow.com/questions/12941957/changing-content-in-embedded-resources-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!