A long time ago I wrote a simple application using WPF (I will write about this utility in my next post) that I recently dug up from the lost archives and I wanted to use in my every day life (at work & at home).
I realized that this small application was made up of 3 projects, thus creating one exe file and 2 extra dll files.
Since I want to pack it light, and I truly see no reason to carry around 3 files that are all under 1MB, I looked for a way to compile all of this under one file.
After looking on the internet a little bit, I found a simple solution, which probably isn't the best way to do it but it works good enough for me...
First, in my main project, I created a folder called Assemblies, and put the other compiled dll's into this folder manually. Then, I defined these dlls' Build Action to be 'Embedded Resource' (this is found in the properties window of every file in your project - image attached).
This will cause the dll's to be compiled as resources embedded into the output exe file.
And now that the dll's are in the exe file, we can delete the dll's in our output folder and just run our exe file, right?
Wrong! The CLR will still look for these files in the folder (and also in the GAC) and when it doesn't find them it won't know what to do.
So all we need to do actually, is just tell the compiler where to look when it doesn't find the files in the original location.
This is done by overriding the method 'AssemblyResolve' in our current AppDomain object like this :
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { String resourceName = "MyProject.Assemblies." + new AssemblyName(args.Name).Name + ".dll"; using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { if (stream == null) return; byte[] assemblyData = new byte[stream.Length]; stream.Read(assemblyData, 0, assemblyData.Length); return Assembly.Load(assemblyData); } };
What we did here is just retrieve all the embedded resources and find the specific assembly the CLR was looking for.
Now, after compiling your project, you can delete the other dll files in your output folder and the exe file will work on its own.
While thinking of this solution, I didn't like the idea of having to copy the assemblies after every change into the 'Assemblies' folder I created, and then compiling again... but after thinking about it for a while, I think it's not that bad in my case if I consider the circumstances - I'm talking about a very small solution, only 3 projects, and all i need this packing into one file for is to move it around. I also don't believe I will be working on it that much more, so this means I just need to do this once. If it was a current project at work or even for fun, I wouldn't go with this option, and i would've found a better way around this...
An extra note : While running the application for the first time, it immediately crashed. I opened it in debug mode (which actually meant I needed to put a Thread.Sleep() in the initialization and then attach to process through VS since I wanted to delete the dll files before debugging). I realized that the application was looking for an assembly called XmlSerializers that I never created. I don't know exactly why, but it turns out that this was automatically generated while compiling. No worries though, this can be taken care of. Just go to the project properties > Build > Output > Generate serialization assembly = Off.
That's a cool approach, although I would look into automating the packing step into the build.
ReplyDeleteDid you know about ILMerge? I never used it, but supposedly it does exactly what you're trying to do here.
http://www.microsoft.com/download/en/details.aspx?id=17630
I am indeed familiar with ILMerge, but unfortunately it doesn't work with WPF because of the way WPF handles assembly resources.
ReplyDeleteI didn't state this in the post, but that is why I intentionally put 'WPF' in the title - so some people with the same problem might run into this post... :)
You can always make the dlls copy to your project automatically, using a pre-build event script in the Build Events of the main project. After all, if your project has references to the other projects, the others will be built first anyway. Heck, you can even use that to automatically remove the excess DLLs afterwards.
ReplyDeleteThis is how I did it:
copy /y "$(ProjectDir)..\EmbeddedProject\$(OutDir)EmbeddedProject.dll" "$(ProjectDir)Resources"
(with EmbeddedProject being the dll to embed, and the resource dlls in the main project being in a "Resources" subfolder).
And to delete the dll files:
if exist "$(ProjectDir)$(OutDir)EmbeddedProject.*" del "$(ProjectDir)$(OutDir)EmbeddedProject.*"
I saw a variant of this on StackOverflow where the file was specifically added to the project's Resources.resx file, which allowed it to be retrieved through the ResourceManager.
Hi, thanks for sharing. Everything is good, but I had an error with corrupted dll files. I don't know if it is only on my machine, but if you have the same try this website http://fix4dll.com/xinput1_3_dll. I replace the wrobg file with a good one and all worked well.
ReplyDeleteGreat Article. Thank you for sharing! Really an awesome post for every one.
ReplyDeleteIEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.
JavaScript Training in Chennai
JavaScript Training in Chennai
Appreciating the persistence you put into your blog and detailed information you provided.
ReplyDeleteThanks for sharing! Glad to read your posts. Thumbs up👍!!
online internship
online internships
watch internship online
online internship for students
the internship online
online internship with certificate
online internship certificate
python online internship
data science online internship
rastgele görüntülü konuşma - kredi hesaplama - instagram video indir - instagram takipçi satın al - instagram takipçi satın al - tiktok takipçi satın al - instagram takipçi satın al - instagram beğeni satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - instagram beğeni satın al - instagram beğeni satın al - polen filtresi - google haritalara yer ekleme - btcturk güvenilir mi - binance hesap açma - kuşadası kiralık villa - tiktok izlenme satın al - instagram takipçi satın al - sms onay - paribu sahibi - binance sahibi - btcturk sahibi - paribu ne zaman kuruldu - binance ne zaman kuruldu - btcturk ne zaman kuruldu - youtube izlenme satın al - torrent oyun - google haritalara yer ekleme - altyapısız internet - bedava internet - no deposit bonus forex - erkek spor ayakkabı - webturkey.net - karfiltre.com - tiktok jeton hilesi - tiktok beğeni satın al - microsoft word indir - misli indir
ReplyDeleteaşk kitapları
ReplyDeleteyoutube abone satın al
cami avizesi
cami avizeleri
avize cami
no deposit bonus forex 2021
takipçi satın al
takipçi satın al
takipçi satın al
takipcialdim.com/tiktok-takipci-satin-al/
instagram beğeni satın al
instagram beğeni satın al
btcturk
tiktok izlenme satın al
sms onay
youtube izlenme satın al
no deposit bonus forex 2021
tiktok jeton hilesi
tiktok beğeni satın al
binance
takipçi satın al
uc satın al
sms onay
sms onay
tiktok takipçi satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
instagram beğeni satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
takipcialdim.com/instagram-begeni-satin-al/
perde modelleri
instagram takipçi satın al
instagram takipçi satın al
takipçi satın al
instagram takipçi satın al
betboo
marsbahis
sultanbet
takipçi satın al
ReplyDeleteinstagram takipçi satın al
https://www.takipcikenti.com
marsbahis
ReplyDeletebetboo
sultanbet
marsbahis
betboo
sultanbet
www.escortsmate.com
ReplyDeleteescortsmate.com
https://www.escortsmate.com
tiktok jeton hilesi
ReplyDeletetiktok jeton hilesi
referans kimliği nedir
gate güvenilir mi
tiktok jeton hilesi
paribu
btcturk
bitcoin nasıl alınır
yurtdışı kargo
İnstagram takipçi satın al! İnstagram takipçi sitesi ile takipçi satın al sende sosyal medyada fenomen olmaya bir adım at. Sende hemen instagram takipçi satın almak istiyorsan tıkla:
ReplyDelete1- takipçi satın al
2- takipçi satın al
3- takipçi satın al
What may need taken hours, days, and even months to build can now be 3D printed using this know-how to create your distinctive designs shortly. Even although there are lots of 3d printing ideas for you to can} begin getting issues printed, 3D printing know-how is still incredibly new to many. So we’re going to reply a few of the the} most typical questions on 3D printing and turning issues to 3D. Now the corporate is prepping the release of its second-generation gadget, set to ship this April. The Metamáquina 2 features a build volume of seven.9 x 7.9 x 6 inches -- more than double that Hooded Puffer Jacket of its equally named predecessor. It will run R$3,300 ($1,614) when it hits in a couple of months.
ReplyDelete