views:

161

answers:

1

Hi everyone, I'm having some trouble using resource files.

In my project I'm using a bunch of bitmap files and two txt files. Bitmaps are used as backgrounds for controls, but .txt files I want to open in Notepad or WordPad. Till now I was accesing them from a file path on my machine:

ControlName.backgroundimage = New Bitmap(direcorypath & "/Map.bmp")

Process.Start(direcorypath & "/Instrukcje.txt")

And it worked fine. Now I added these files to my resources (it's practical for me to have them in executable file) And replaced those lines with:

ControlName.backgroundimage = New Bitmap(MyProject.My.Resources.Map)

Process.Start(MyProject.My.Resources.Instrukcje)

For bitmap it works fine, but for txt file an error occurs saying that a file cannot be found. Interestin thing is: when I type "MyProject.My.Resources.Instrukcje" in "Watch" it shows the content of the file. So it is there, only not as a file? I think I am missing something here
I searched everywhere whats the thing, but couldn't find anything helpful. I will be gratefil for some clues. (And sorry if somethings unclear, my programming skills are much worse than my not-so-good English) Thanks:)

A: 

Resources are embedded in your .dll or .exe file and therefore cannot be accessed with the Process class. You could write the .txt resource to a temp-directory and use Process to start the temp file there.

Additional info: I've added a .txt file to a Resource.resx file to test your question, and plain text is stored as a string in the resource file. Therefore, if you access the Resources.(InsertYourTextFilesName) property, the text within the textfile is returned.

In case you might want to know: binary files (e.g. non-plain-text files) are stored as byte array in the resource.resx and interned within the compiled assembly as such.

Webleeuw
Thanks, Webleeuw, a temp file seems like a great idea, i'll try to do that:)
Agata
All right! I just used the resource as if it was an ordinary string and wrote it into a temporary file. And everything workes fine, thank you again :)
Agata