我可以在 DLL 中嵌入其他文件吗?

发布于 2024-08-14 22:11:06 字数 144 浏览 1 评论 0原文

我正在通过 API 为另一个应用程序编写插件。这些插件以 DLL 的形式分发。是否可以在 DLL 文件中嵌入其他文件,例如 pdf、图像、chm 帮助文件等...我希望能够通过我的插件提供文档,但我仍然希望保留分发插件的能力-作为单个文件,用户只需拖放到应用程序上即可安装。

I'm writing a plug-in for another application through an API. The plug-ins are distributed a DLLs. Is it possible to embed other files in the DLL file like pdfs, images, chm help files etc... I want to be able to provide documentation with my plug-in but I'd still like to retain the ability to distribute the plug-in as a single file the user can just drag and drop onto the application to install.

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

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

发布评论

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

评论(5

风向决定发型 2024-08-21 22:11:06

当然,您可以在 DLL 中嵌入资源。然后在运行时您只需执行以下操作:

Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("com.stackoverflow.plugin.example.Foo.pdf");

这将为您提供嵌入 DLL 中的 Foo.pdf 文件的流。请注意,资源名称的范围必须由您调用该方法的类型的命名空间决定。

Sure, you can embed a resource in your DLL. Then at runtime you just do:

Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("com.stackoverflow.plugin.example.Foo.pdf");

This would give you a stream to the Foo.pdf file embedded in your DLL. Pay attention to the fact that the resource name has to be scoped by the namespace of the type from which you're invoking the method.

硪扪都還晓 2024-08-21 22:11:06

当然,只需将它们设置为 VS.NET 中的“嵌入式资源”即可(假设您正在使用它)。然后,您可以通过资源 API 或简单地使用 Assembly.GetManifestResourceStream()

Sure, just make them "Embedded Resource" in VS.NET (assuming you're using it). You can then read them via resource APIs or simply with Assembly.GetManifestResourceStream().

就此别过 2024-08-21 22:11:06

是的,你可以这样做。

将资源文件添加到您的项目中。在 Visual Studio 中打开资源文件,然后单击“插入资源”。您可以选择不同类型的资源,包括外部文件。

Visual Studio 将为您生成代码,以便您可以在运行时通过 Resources 标识符从文件名称中以字节数组形式检索文件。

Yes, you can do that.

Add a resource file to your project. Open the resource file in Visual Studio and click Insert Resource. You can select different types of resources, including external files.

Visual Studio will generate code for you so that you can retrieve the files as byte arrays at run time from their names through the Resources identifier.

ぶ宁プ宁ぶ 2024-08-21 22:11:06

作为替代选项,如果您需要在用户计算机上解压并保存文件(例如您希望在应用程序外部访问的 chm 文件),您也可以对 zip 文件执行相同的操作。

您说您希望将文件“拖动”到您的应用程序上。只需让 DDE 事件检查文件是否是 zip(甚至可能使用带有元数据的 jar 之类的东西)并解压必要的文件,包括实际的插件。

这与 openxml 文档的想法相同,它们实际上只是伪装的 zip。

As an alternate option, if you need to unpack and save the files on the users machine (say a chm file you want accessible outside your app) you can also do the same with zip files.

You said you wanted the file to be "Dragged" onto your app. Simply have your DDE events check to see if the file is a zip (maybe even using something like a jar with metadata) and unpack the necessary files, including the actual plugin.

This is the same idea as openxml docs, they are really just zips in disguise.

机场等船 2024-08-21 22:11:06
string htmlBody = "";
string assemblyActualPath = Assembly.GetExecutingAssembly().Location; // C:\MyProyect\bin\Debug\MyAssembly.exe
string assemblyActualDirectory = Path.GetDirectoryName(assemblyActualPath); // C:\MyProyect\bin\Debug\
string assemblyPath = Path.Combine(assemblyActualDirectory, "Library.dll"); // C:\MyProyect\bin\Debug\Library.dll
Assembly assembly = Assembly.LoadFrom(assemblyPath);
Stream stream = assembly.GetManifestResourceStream("LibraryNameSpace.Templates.Html.HTMLPage1.html");
using (StreamReader reader = new StreamReader(stream))
{
     htmlBody = reader.ReadToEnd();
}

其中 HTMLPage1.html 是嵌入式资源,并且具有“不在输出目录中复制”属性

string htmlBody = "";
string assemblyActualPath = Assembly.GetExecutingAssembly().Location; // C:\MyProyect\bin\Debug\MyAssembly.exe
string assemblyActualDirectory = Path.GetDirectoryName(assemblyActualPath); // C:\MyProyect\bin\Debug\
string assemblyPath = Path.Combine(assemblyActualDirectory, "Library.dll"); // C:\MyProyect\bin\Debug\Library.dll
Assembly assembly = Assembly.LoadFrom(assemblyPath);
Stream stream = assembly.GetManifestResourceStream("LibraryNameSpace.Templates.Html.HTMLPage1.html");
using (StreamReader reader = new StreamReader(stream))
{
     htmlBody = reader.ReadToEnd();
}

Where HTMLPage1.html is an Embedded Resource and with the property "not copy in output directory"

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