开发者

Get license file from a folder in C# project

开发者 https://www.devze.com 2022-12-27 06:18 出处:网络
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:

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("pathToLicense开发者_C百科FileHere");

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.


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).


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号