I want to know how I can compress my dll files in my program, then upon running of the program the files are extracted from the program so that the program may use them, and when the program is closed, the files are compressed back in.
Asked
Active
Viewed 754 times
1
-
4What is the point of this? Are the DLL files really *too large*? – Cody Gray - on strike Jul 07 '13 at 10:30
-
1do you need to compress the DLLs because of size, or you just want to hide the DLLs? – Mahmoud Darwish Jul 07 '13 at 10:34
-
@CodyGray There are 4 dll's about 4mb's a piece. I want it this way just for the sake of not using a folder, or too many files on the desktop. – John M. Jul 07 '13 at 10:40
-
@MEYWD There are 4 dll's about 4mb's a piece. I want it this way just for the sake of not using a folder, or too many files on the desktop. – John M. Jul 07 '13 at 10:40
-
check this http://stackoverflow.com/questions/476993/can-i-include-dll-in-exe-in-visual-studio – Mahmoud Darwish Jul 07 '13 at 10:43
-
1So I think we're misunderstanding your use of the term "compression", which implies shrinking something down to make it smaller, rather than "merge" or "combine" or "include" or "encapsulate" or etc. In that case, ILMerge is what you're looking for. There have been [lots of questions](http://stackoverflow.com/questions/126611/net-windows-application-can-it-be-compressed-into-a-single-exe) asked [about that here already](http://stackoverflow.com/questions/476993/can-i-include-dll-in-exe-in-visual-studio). – Cody Gray - on strike Jul 07 '13 at 10:46
-
1Classic XY question. Where the inevitable X problem is that you *still* have DLLs on the desktop while the program runs. You solve X by putting programs where they belong, in the c:\program files directory. Now you just don't care about Y anymore. – Hans Passant Jul 07 '13 at 13:14
3 Answers
0
If i understand this correctly, you want ro hide your DLLs.
First way:
1) Include the DLL as resource in the project (exe)
2) Implement a method like this:
public static void ResourceToFile(string fileName, string dir, byte[] resource) {
Directory.CreateDirectory(dir);
string path = Path.Combine(dir, fileName);
File.WriteAllBytes(path, resource);
}
3) Call like this: (at start of the application, before the dll is used)
ResourceToFile("xyz.dll", Application.StartupPath, Resources.xyzLib);
Second way:
(no files written, only possible for assemblies)
You may use the AppDomain.AssemblyResolve
event, that fires when a DLL annot be found.
Use the above method to load the assembly from the applications resources.

joe
- 8,344
- 9
- 54
- 80
0
Taken from Jeffrey Richters blog. This will load assemblies from your programs resources so there is only the executable needed to run.
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
String resourceName = "AssemblyLoadingAndReflection." +
new AssemblyName(args.Name).Name + ".dll";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};

Ceres
- 3,524
- 3
- 18
- 25