1

I'm building a WinForm application to manage some very simple data (on an embedded .txt file) that I want to access and modify. Using the StreamReader works to access the data, but when it comes modifying it, that code throws the "Stream was not writable" exception:

System.Reflection.Assembly thisExe; //The first 3 lines are from msdn
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
Stream str = thisExe.GetManifestResourceStream("a.b.txt");
StreamWriter sw = new StreamWriter(str);

I tried the Stream.CanWrite property but it is read only.

Is there a way to obtain the CanWrite permission on that textfile?

user3311142
  • 345
  • 1
  • 4
  • 13
  • You cannot modify an EXE or DLL on disk. Protected by the lock that the operating system puts on them when they are loaded into memory. And by UAC. And the typical anti-virus program. So of course the stream isn't writable. Embedded resources are read-only. If you need to write them then you need a plain file that you store in AppData. – Hans Passant Sep 20 '14 at 08:19

1 Answers1

2

The Stream you get as a return value from GetManifestResourceStream cannot be writable. The embedded resources are compiled into your exe/dll and are not meant to be modified.

A manifest resource is a resource (such as an image file) that is embedded in the assembly at compile time.

Use Settings instead or copy the resource Stream to another Stream, see Write file from assembly resource stream to disk.

File.WriteAllText("C:\a.b.txt", Resources.ResourceName);
Community
  • 1
  • 1
Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105