从 C# 项目中的文件夹获取许可证文件

发布于 2024-08-29 04:31:19 字数 484 浏览 1 评论 0原文

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

pdf.SetLicense("pathToLicenseFileHere");

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

我尝试像这样设置路径:

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

但这对我来说不起作用。

编辑:

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

長街聽風 2024-09-05 04:31:19

因此,该文件位于您的项目中,并且您需要将其位于应用程序所在文件夹的同一文件夹(结构)中。

如果我是正确的,有两种方法可以使其工作:

  1. 将文件标记为 Content,以便 VS 会将其复制到您的 .exe 文件的文件夹中
  2. 将其作为资源添加到您的应用程序中,如果内存访问就足够了

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

  • 在解决方案资源管理器中标记该文件
  • 右键单击​​该文件并选择属性
  • 构建操作更改为内容

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

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

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

  • 将所需文件(如果尚未发生)添加到您的项目中(项目 - 添加现有项目)
  • 在解决方案资源管理器中双击 MyProject - 属性 - Resources.resx
  • 单击旁边的小向下箭头单击“添加资源”按钮并选择“添加现有文件...”
  • 在对话框中再次选择您喜欢的文件

现在该文件已作为内部资源添加到您的项目中。要从代码访问它,只需使用 Properties.Resources.MyFileName。根据文件的类型,您将在此处获得一个字符串(如果它是纯文本文件)或一个字节数组(如果它包含任何非文本)。

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

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

嘴硬脾气大 2024-09-05 04:31:19

路径需要相对于可执行文件的位置,而不是相对于 .cs 文件的位置。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文