By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,996 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,996 IT Pros & Developers. It's quick & easy.

Visual C# - Including Resource Files when Publishing

P: 10
Hi,

I'm a bit lost as to how to actually include additional resource files in my project...

I have a .pdf file I would like the end user to be able to access as a help file, it all works ok, except when I go and publish it the application tries to find it in the original directory that I got it from. I see there are several property options available to me but I'm not sure what they do - 'Build Action' and 'Copy to Output Directory'

Could someone please point me in the right direction on how to include stand-alone files like this into a project so that when it gets published and distributed the file is part of the application?

Many Thanks,

Ben
Jan 29 '09 #1
Share this Question
Share on Google+
11 Replies


Frinavale
Expert Mod 5K+
P: 9,731
Are you developing a web application or desktop application?
Feb 4 '09 #2

P: 10
Hi,
It's a desktop app.
Thanks =)
Feb 4 '09 #3

Frinavale
Expert Mod 5K+
P: 9,731
You need to use a ResourceManager to retrieve embedded resources.

Are you currently doing so?
Feb 4 '09 #4

P: 10
Hmmm...
I've had a bit of a go, but must confess I got lost. Where should I place the file so that its within "ResourceNamespace"? And what is the assembly I should be using?

Sorry for confused questions! This is what i'm doing at the moment:
Expand|Select|Wrap|Line Numbers
  1. System.Resources.ResourceManager rm = new System.Resources.ResourceManager("Mypdf.pdf", System.Reflection.Assembly.GetExecutingAssembly());
  2. File.OpenRead(rm.GetString("Mypdf.pdf"));
But it just gives a MissingManifestResourceException at the moment - can't find the file.

Thanks for your help,

Ben
Feb 4 '09 #5

Frinavale
Expert Mod 5K+
P: 9,731
I think you should be using the GetObject method, not the GetString method....

Aside from that, when you get a get a MissingManifestResourceException, it means that no the ResourceManager was unable to find any resources for the assembly....

Your PDF isn't in a resx file....

I haven't actually used this stuff in a desktop application before, it's completely different when you want to retrieve embedded resource files in a web application.

Give me a sec to look into this further.
Feb 4 '09 #6

Frinavale
Expert Mod 5K+
P: 9,731
Ok I think I took you down the wrong path here, sorry.
It seems you should be looking into using the GetManifestResourceStream Method . Please note that the string parameter that this method accepts (the name of the resource) is case sensitive. Also, make sure that your PDF file is marked as an Embedded Resource.

Expand|Select|Wrap|Line Numbers
  1. System.Reflection.Assembly thisExe;
  2. thisExe = System.Reflection.Assembly.GetExecutingAssembly();
  3. System.IO.Stream file =  thisExe.GetManifestResourceStream("Mypdf.pdf");
Feb 4 '09 #7

P: 10
Thanks for that,
How does it locate the file? Currently the code runs but doesn't do anything - isn't that because it is waiting for me to begin to streamread the text of the file? Can I not use OpenRead()?

Before your reply I started down the path of adding the pdf as part of a .resx ("HelpFiles"). Having followed the wizard I then coded:
Expand|Select|Wrap|Line Numbers
  1. System.Resources.ResourceManager rm = new System.Resources.ResourceManager(rml_s350MW.HelpFiles.MyPdf, System.Reflection.Assembly.GetExecutingAssembly());
  2. File.OpenRead(rm.GetObject("MyPdf.pdf"));
However the ResourceManager is looking for a string, and so is the OpenRead


Anyway, I should probably keep trying along the lines of using the GetManifestResourceStream Method . even though it currently isn't working.

Thanks
Feb 4 '09 #8

P: 10
Ok! Getting somewhere, I can now access the file. (Well at least I know it finds it without giving an exception =)

Using the GetManifestResourceStream Method and a little help from this link:
http://support.microsoft.com/kb/319292
I have got this:
Expand|Select|Wrap|Line Numbers
  1. System.Reflection.Assembly thisExe;
  2. thisExe = System.Reflection.Assembly.GetExecutingAssembly(); 
  3. System.IO.Stream file = thisExe.GetManifestResourceInfo("MyNamespace.MyPdf.pdf");

Just one more thing, this adds the file to the Stream. I don't want to do that but rather open the pdf in a viewer. I can do it to a file at a fixed location, but how can I do it using the Stream with the embedded resource?

Normally I would just go:
Expand|Select|Wrap|Line Numbers
  1. File.OpenRead("c:\\folder\MyPdf.pdf");
Thanks for your help,
Ben
Feb 4 '09 #9

tlhintoq
Expert 2.5K+
P: 3,525
I include a .txt file with my installer that lists the updates of the new version.
The text file is in the program directory of installed application since this is an easy 'special' folder to locate.
I have InstallShield integrated with Visual Studio to build the installers, so it may be a bit different from the native install builder of Visual Studio, but it does allow you to pick files to be included with the build in addition to the project's primary output (the compiled application)
Feb 5 '09 #10

Frinavale
Expert Mod 5K+
P: 9,731
Tlhintoq, any idea on how to open the PDF file from the FileStream returned by the GetManifestResourceInfo method?

The only idea I have is to save this stream to a temporary physical file and then open it...delete it after your finished with it. I held back on this suggestion because I'm sure there's a better way to do this. Since I mainly work with web applications I don't have much experience with this.

PS.
Using resx files instead of simply embedding your PDF would be a good idea if your application supports multiple languages. You could store each translated PDF in its respective resx file to easily manage the resources required to support multiple languages.
Feb 5 '09 #11

tlhintoq
Expert 2.5K+
P: 3,525
I've only ever provided the update history I mentioned, and left the opening of it up to the default application on the user's PC. As I said, I have the installer put the update history text file in the application installation directory for convenience.

In this example, the user chooses the "Update history" option from the "Help" menu.

Line 3 Get the path to the application.
Line 4 Turns that path into the path for the history file.
Line 5 Opens the history (using the designated default application)

Expand|Select|Wrap|Line Numbers
  1. private void updateHistoryToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             string History = Application.ExecutablePath;
  4.             History = Path.GetDirectoryName(History).TrimEnd('\\') + @"\Update_History.txt";
  5.             if (File.Exists(History)) { System.Diagnostics.Process.Start(History); }
  6.         }
  7.  
Using resx files instead of simply embedding your PDF would be a good idea if your application supports multiple languages. You could store each translated PDF in its respective resx file to easily manage the resources required to support multiple languages.
Very true. I do need to get into multiple language support. Then again, I have 5 applications to build on my "to do" list. I'm sure learning new things will come in during my free time. As soon as I learn what 'free time' is. <laugh>

If I had to do it today, I would probably do it through brute force of having different help PDFs (for each language), place them all in a given directory, then pick one based on the PC language. Same PDF would go on the website for downloadable help. In my head I think I could then easily update the help files without having to send a new application: Just a new help file(s) installer.

Is there a way of doing something similar via resources or resx files that I haven't learned yet?
Feb 20 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.