从 C# 项目中的文件夹获取许可证文件
我有一个许可证文件,需要在运行时访问它才能创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,该文件位于您的项目中,并且您需要将其位于应用程序所在文件夹的同一文件夹(结构)中。
如果我是正确的,有两种方法可以使其工作:
对于解决方案 1(作为内容文件)
现在文件位于
Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "license.txt")
中。也许您在解决方案目录中添加了一个文件夹并将所需的文件移到了那里。在这种情况下,VS 将创建解决方案中定义的整个结构来放置内容文件。在这种情况下,您可能还需要将文件夹信息添加到上面的代码中。
对于解决方案 2(作为内部资源)
现在该文件已作为内部资源添加到您的项目中。要从代码访问它,只需使用
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:
For solution 1 (as Content file)
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)
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).
路径需要相对于可执行文件的位置,而不是相对于 .cs 文件的位置。
Paths need to be relative to the location of the executable, not to the location of the .cs files.