从C#项目中的文件夹获取许可证文件 [英] Get license file from a folder in C# project

查看:734
本文介绍了从C#项目中的文件夹获取许可证文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个许可证文件,我需要在运行时访问,以创建PDF文件。在创建内存pdf后,我需要调用一个方法来设置许可证,如下所示:

  pdf.SetLicense( pathToLicenseFileHere); 

许可证文件位于与创建pdf的.cs文件相同的项目中,但在一个单独的文件夹中。我不能让这个简单的事情正确地行事,这让我有点难过,因为它真的不应该那么难。 :( b / b>

我尝试设置这样的路径:

  string path = @\Resources\File.lic; 

但它不是为我工作。



编辑:

更多信息我应该提到这是一个网页服务返回pdf:s作为byte [] .Web服务创建pdf:s,在创建过程中,我需要设置许可证文件,以便从pdf文档中删除水印.PDF本身不保存到光盘,只需创建一次即可访问许可证文件。

解决方案

所以文件在你的项目中,需要它在您的应用程序的文件夹中的相同的文件夹(结构)。



如果我是正确的有两种方法得到这个工作:


  1. 将文件标记为 Content ,以便VS将其复制到.exe文件的文件夹中

  2. 将其添加为re如果内存中的访问是足够的,那么源代码到您的应用程序。

    $ b

    对于解决方案1(作为内容文件)




    • 在解决方案资源管理器中标记文件
    • 右键单击文件并选择属性
    • Build Action 改为 Content



    现在文件位于 Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),license.txt)



    解决方案目录中的一个文件夹,并在那里移动想要的文件。在这种情况下,VS将创建您的解决方案中定义的整个结构来放置您的内容文件。在这种情况下,您可能需要将文件夹信息添加到上面的代码中。



    对于解决方案2(作为内部资源)




    • 添加需要的文件(如果还没有发生的话)到您的项目中(项目 - 添加现有项目)
    • Explorer on MyProject - Properties - Resources.resx

    • 点击添加资源按钮旁边的向下箭头,选择添加现有文件...

    • 在对话框中再次选择您喜欢的文件



    现在文件被添加为项目的内部资源。要从代码访问它只需使用 Properties.Resources.MyFileName 。根据文件的类型,你会在这里得到一个字符串(如果它是一个文本文件)或一个字节数组(如果它包含任何非文本)。

    此外,如果它是一个简单的文本文件,您可以在解决方案资源管理器中双击它,对其进行编辑,重新生成后,新的应用程序将包含已更改的文本(如果您的应用程序中需要某些SQL语句或XML文件, p>

    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.

    解决方案

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

    这篇关于从C#项目中的文件夹获取许可证文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆