views:

210

answers:

2

I have a license file that I need to access at runtime in order to create pdf-files. After I have created the in memory pdf, I need to call a method on that pdf to set the license, like this:

pdf.SetLicense("pathToLicenseFileHere");

The license file is located in the same project as the.cs-file that creates the pdf, but is in a separate folder. I cannot get this simple thing to behave correctly, which makes me a bit sad, since it really shouldn't be that hard. :(

I try to set the path like this:

string path = @"\Resources\File.lic";

But it just isn't working out for me.

Edit:

A bit more info. I should mention that this is a web service which returns pdf:s as byte[]. The web service creates the pdf:s, and during this creation process I need to set the license file so as to remove any watermarks from the pdf document. The pdf itself is not saved to disc, and only needs to access the license file once when it is created.

+1  A: 

Paths need to be relative to the location of the executable, not to the location of the .cs files.

psychotik
Indeed, but how do I achive that. I know I can use the Assembly.GetExecutingAssembly, but then what? Do I need to set the license file's build action to anything specific in order to get at it with GetManifestResourceStream()? Currently set to none. Thanks.
daft
@daft: the executable doesn't know where the .cs file is located. So you need to recalculate your path relative to the executable location.
Vlad
@daft: please note that your relative location should be defined for user's computer, not for your development environment. There chances that on the user's machine your .pdf is going to reside in the same folder as your executable.
Vlad
@Vlad: Thanks. This was a real confidence hit. I never thought I was a good developer, but I didn't think I was useless either. Now I'm certainly leaning more towards the latter option, which is sort of a bummer... Could you perhaps check my additional info in the original question, and maybe provide me with an example?
daft
@daft: sorry, did read your question. `path = @"\Resources\File.lic"` shouldn't work, as this refers to the Resources subfolder in the root folder of the current drive. You should better use `Application.ExecutablePath`, as @Oliver suggested. Starting from `Application.ExecutablePath` you can descend into Resources folder if your pdf is located there w.r.t. your executable. Even better idea could be to embed the pdf into the executable, so you won't need to care about the paths. Beware however that embedding is going to make update to the pdf more complicated :)
Vlad
Thank you Vlad! Most helpfull from both you and Oliver. :)
daft
+2  A: 

So the file is in your project and you need it in the same folder(structure) within your folder where your application is.

If i'm correct there are two approaches to get this to work:

  1. Mark the file as Content, so that VS will copy it into the folder of your .exe file
  2. Add it as resource to your app if in-memory access is sufficent

For solution 1 (as Content file)

  • Mark the file in the Solution Explorer
  • Right click the file and select properties
  • Change the Build Action to Content

Now the file is located in Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "license.txt").

Maybe you added a folder in your solution directory and moved the wanted file there. In this case VS will create the whole structure as defined in your solution to lay down your content file. In that case you'll probably need to add the folder information also to the code above.

For solution 2 (as internal resource)

  • Add the wanted file (if not already happened) to your project (Project - Add Existing Item)
  • Double click within the Solution Explorer on MyProject - Properties - Resources.resx
  • Click on the little down arrow next to the Add Resource button and select Add Existing File ...
  • Select the file you like again in the dialog

Now the file is added as a internal resource to your project. To access it from code just use Properties.Resources.MyFileName. Depending of the kind of file you'll get here a string (if it is a text only file) or a byte array (if it contains any non text).

Also if it is a simple text file you can double click it in your Solution Explorer, edit it and after a rebuilt the new application contains the altered text (very handy if you need some SQL statements or XML files within your app).

Oliver
Thank you for the enlightening answer Oliver. I will give your suggestion a try and come back to mark as accepted if I get it to work. :)
daft