问题
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