开发者

Can I embed other files in a DLL?

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

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 app开发者_C百科lication to install.


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.


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


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.


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.


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"

0

精彩评论

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

关注公众号