Make programs with required libraries - c#

Suppose that we have a program that uses only some of the libraries of .NET Frameworks, for example the I/O program that read and write file, so this program does not use as many as libraries like NET libraries and etc. So, i wanna make a program that include ONLY the libraries that is required to run! Not all of .NET Frameworks, can i do sth like this ? If yes, please explain your idea.
Thanks for your attention :)

Your program does not "include" the required libraries. They are loaded as required, so you don't need to worry about your program loading libraries it does not actually use.
If your actual worry is that your users need to install the complete .NET framework, you could have a look at the .NET Framework Client Profile. That is a smaller installer and does not install the complete framework.

When you install the .net framework it includes all the required libraries. .net libraries that you reference in your program are not included with it when you distribute it.

Your application does not include the entire .NET Framework. This is why users are required to install the .NET Framework before they c an run your application.

I think you are asking if you can write a program that will run on a machine without the .Net framework installed? And then to only include the dll files that are needed for that specific application.
I think that the .Net framework must be installed on the machine as the .exe you compile using the CLR's compiler automatically checks that the framework is installed when you execute it.
Even the smallest .exe you create will be in a PE32 format, and the ones emitted by csc.exe contain a sort of bootstrap at the top that kicks off the CLR's Just in Time compilation etc.
So you need to have the framework installed for this functionality regardless of what libraries you call.
This is as I understand it.
For what you want to do you probably need to write non-managed or unsafe code using c++ and calling Win32 DLL's directly.

Create a setup project that installs the required .NET Framework when they install your application.

Related

Programatically check for version .NET Framework - Should I?

I got at task to extend en existing WinForm application to make a check weather or not the required .NET Framework (fx. 3.5) is installed.
Well the issues is that - if there is no .NET Framework installed, the winform program is not able to run at all... I assume.
I could (maybe) do like suggested here: (and make a c++ program that should start first, make the check and then launch the application)
Check on .Net framework version from WinForms app
But I would rather not go into c++.
Another way seems to be this solution:
Why isn't an exception thrown when the right .NET framework version is not present?
... where you configure your application in app.config. But I doubt that will work if there i no .NET framework installed.
<startup>
<supportedRuntime version="v3.5" />
</startup>
So my question is what is Best Practice in this area? Should I make the check one way or the other, or should I just make it a pre-requisite, that fx. .NET Framework version 3.5 is demanded?
If the required framework is not installed, your application won't run, so checking if the framework is installed from within your app is checking something you already know to be true.
If you want to check that the framework is installed, you really need to do this from within a a bootstrapper exe either written in a .NET version you know will exist (like .NET 2 as it gets installed on the machine with the OS) or some other language like C++.
You can check in the registry (HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP) to see what frameworks are installed. This can easily be done in C# or any other language.
C# code something like this:
var baseKeyName = #"SOFTWARE\Microsoft\NET Framework Setup\NDP";
var installedFrameworkVersions = Registry.LocalMachine.OpenSubKey(baseKeyName);
var versionNames = installedFrameworkVersions.GetSubKeyNames();
If you are deploying often for limited set of users, you should check out ClickOnce (http://msdn.microsoft.com/en-us/library/t71a733d(v=vs.80).ASPX)
It is a bit like Windows Installer, but simplified a lot. From the user perspective application will look like desktop icon. When you click on the icon, it will automatically check all the requirements and install the latest version of your software if it has been updated. This way you can ensure that users always has the required framework installed, as well users always will use the latest version.
To Check .net frame work version follow http://support.microsoft.com/kb/318785
I got at task to extend en existing WinForm application to make a check weather or not the required .NET Framework (fx. 3.5) is installed
This doesn't makes sense, because it will not run if required component is missing.
As already mentioned, typically installer is responsible for the job to check for components, interrupt installation, download and install them.
But in any case, nothing prevents you to make bootstrapper, which will be able run in majority of times (aka required less/none components) and which will run your .net application. It can be organized in multiple ways: exe + exe (often renamed), exe + dll, exe + embedded resource (extract and run), etc...
If you think to use MS Visual C++ to make bootstrapper, then here is a bad new for you: it required component to be installed (yes, that's stupid, but it needs Microsoft Visual C++ 200x redistributable). So it would be again a job for installer, or you will have to learn how to write C/C++ pure WinAPI software, which is a pain, but doable.
To example, this is a check for .Net Framework 4.0
// check if net framework 4.0 or higher is installed
HKEY hkey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Net Framework Setup\\NDP\\v4", 0, KEY_READ, &hkey) != ERROR_SUCCESS)
; // some error
RegCloseKey(hkey);
Doesn't looks complicated? Then go ahead!
You are right about the program not running if the required .NET version is not installed. You could use a bootstrapper, as others have said. You could simply let the application fail at runtime with the default message box that appears whining about the missing dependency.
Another valid approach would be hosting the CLR yourself. Here:
Hosting the Common Language Runtime
CLR Hosting - Customizing the CLR
It's mostly like a bootstrapper too, but instead of launching the second (.NET-dependent) application it just loads it into the same process (if available).
You could create a batch (.bat) file to verify the existence of the .NET Framework folders:
IF EXIST C:\WINDOWS\Microsoft.NET\Framework[framework version here] (goto launchprogram) else goto errormessage

Why the .NET framework takes so long to install? Why is it complex to make it portable?

I am asking this question primarly to learn. Let's say I want to send a very small console application (50 lines of code Also I am using the System.Text.RegularExpresion namespace.) to a friend writen on c# on .net framework 4.0 . I will like to make the application portable therefore I just send him the output of the bin directory.
Why does he has to install the .net framework 4.0 client which it takes quite a while. Also it will be nice to include only the dll libraries that I plan on using in this case system.dll (that library contains system.text.regularexpressions namespace).
In other words why is it that I cannot include system.dll in my console application in order to make it portable and for it to work on a computer that does not have the .net framework installed.
Why is the installation of .net framework 4.0 so complex? What would happen if windows where to place all .net libraries on C:\Program Files\.net framework 4.0\ and then on the installation write a key to the registry of the path where that framework was installed so that programs are able to find the nessesary dlls.
Why are installations so complex on general?
I tried to decomplile system.dll with reflector then include that on my project and that did not worked
Edit
I guess my question should have been why .net framework 4.0 takes so long to instal? Why is it not posible to run the .net framework 4.0 if windows where to place the necessary dlls on program files and then write to the registry the path where those dlls are located. That process would have been much faster. Why not do it that way?
So in conclusion
Thanks for the help I understand now how important is the CLR. I guess the only part that I am missing to understand is why installations take so long. I understand that there are thousands of dlls. Unziping those dlls to program files and writing 10000 keys on the registry should be much more quicker.
Your question seems to boil down to "Why do I need to install the entire .NET Framework, instead of including just the required DLL's?"
The answer is that .NET Framework consists of more than just DLL's. The other major component of the framework is the CLR, which is in charge of executing and managing .NET code. The .NET Framework consists of many other smaller things (such as compilers) which are not necessary to run code, but nevertheless included with the framework.
The CLR is more important to .NET than the DLL's themselves. It is analogous to the CPU on a computer. Without it, nothing can be done, and the executable programs you have are just garbage data. The CLR takes care of JIT compiling your code to a native executable, memory management, etc. It is very similar in concept to the JVM for Java applications.
Even the DLL's are more complex than it would seem. Although you could in theory (disregarding the CLR for a minute) deploy just the dependency DLL's with your application, remember that all those DLL's (with the exception of mscorlib) have dependencies on more DLL's, and so on, including a vast number of dependencies for a simple application.
The C# programming language requires the .Net framework be installed on the target computer first, before running the target program. VB.NET and F# have the same requirement. The .net framework is a very large set of libraries, requiring more than just a couple of .DLL files, but also access to the system registry. There is a fairly deep level of integration, most of it through COM, but going deep into Win32 (at least for WinForms).
Now, Microsoft could have make C# compile directly to native code, but that is not what they decided to do. These programs require the framework to be installed, by design. As it is now, the .Net framework is required. This was a bigger deal in 2001 when C# and .Net was first introduced, because everybody had to install it! Today, Windows 7 (and Vista) come with it pre-installed, making it easier to the user. For server-side (web apps), it is also not that big of a deal, because it is not a matter of installing it on many client computers
One way of looking at it would be that each program would require all of the libraries, making it more difficult to maintain bug fixes, if every program had their own collection of .Net libraries they used. With having one installation of the framework on a computer, when a bug is found, Microsoft can patch the one version of the framework, rather then the multiple locations the file(s) could be if each program had their own set of library files.
As for portability, you can use Mono to run these same .Net (C#) binaries on Linux and Mac. Of course, on those other platforms, you will still need an installation of Mono to make it work.

Running C# application with .NET - MONO

I have a simple application made using C#.
Now how do I make it , such that it runs on all systems.
If a PC does not have .NET framework installed - it shouldsiliently install it with only the bare minimum requirements that the program needs.
Installing .NET framework - too big in size compared to many program , which is just a few kilobytes. Also is shoulf be silent and only if required.
Basically the application should be light and capable to run in all Windows systems.
Not interested in getting to Linux users.
Should I use Mono Project.
Else is there a way to get the bare minim .NET framework selectively pre-installed.
Please advise.
Thanks
Have a look at mkbundle. It will create a standalone executable, with no other dependencies. In particular it does not need neither the Mono runtime nor .NET to be installed in order to execute.
The size might still be a problem (it will likely be several megabytes, even compressed), so there is another tool to strip out everything you don't need from the assemblies: the monolinker.
Note that the size will likely not reach the kilobyte range even after doing all this.
You can do this with a lot of work and the help of the Mono framework. See Embedding Mono for more information.
All that considered, it would be much easier to use a boostrapper to get a version of the .NET Client Framework installed. But you're going to lose the ability to install silently or be in the "kilobytes" footprint.
Unfortunately you cant run a .NET program on a machine that does not have the .NET framework installed and the installer of the program could be made to download the framework automatically but not in .NET .
To run .net applications you need the .net framework installed, that should be either the full version or the limited client profile edition.
The easiest way is to create a setup project from VS and require the .net version you want... the installer should be able to install the .net framework from the internet so you are not required to ship it with the app, which you can do by the way from the installer.
Mono won't be different since it still needs to be installed on the system. Mono however has full AOT support, but I don't have any idea whether that would help you or not... it is still a huge overkill anyway.
If you need your app to be small and run on ANY windows without any dependencies, you should do in c/c++ or vb6 whose runtime ships with most windows versions.

C# - Install .Net framework4

I want to ask about .net framework4 . it take along time through installing, is there a way to shorten the time of installing .
for example, can I make program to install it manually by installing only dlls that I use in my application?
Short answer: no.
If you are writing a client-side application, then you have the option of targeting the .NET Framework 4 Client Profile if you want to. This would be a smaller install (if implemented correctly).
However, the best solution in my opinion would be to require .NET Framework 4 as a prerequisite for your app, rather than attempting to bundle it. It's much easier, quicker and more reliable for an end-user or system administrator to obtain .NET themselves from Windows Update or other trusted source, then install your app later.
The quick answer is no; you cannot pick and choose which .NET DLL files to include as part of your code in order to reduce the size of the install.
However, having said that, there are some tools out there which will merge assemblies together into one file. But I don't know if that scales to including all required .NET assemblies. I doubt it very much.
If you're willing to use .NET 2, see Salamander linker. Disclaimer: not for all projects.

How do I create a portable app (runs without installing)

How do I create an app that is:
lightweight: I am guessing don't require .NET frameworks maybe?
portable: runs without installing and saves data in the app directory, so i can just move the folder or maybe even the exe?
this is just a personal experiment: i want to try create a simple todo list app that has the above attributes
I am thinking:
C#/WPF (but requires .NET framework, I can explore client profile thogh)
Appcelerator Titanium (i think this will be lightweight & good enough? I do not know if I can have a portable titanium app though)
It is almost portable if the target machines have .NET Framework installed.
NDepend is such a product, which is built against .NET 2.0 and runs fine on Windows Vista, Windows 7, and other Windows if you manually install .NET 2.0 before.
Personally, it is not hard to write an application launcher in native languages such as C++/Delphi to detect whether the target machine has .NET. If .NET is not yet installed, this launcher can display a warning or help install the framework automatically. (Even some installers allow you to do this.)
If you want to write it in C#, you either need the .NET framework or the Mono framework. Either way you need it. Thankfully .NET 2.0 is pretty ubiquitous.
By default, .NET uses xcopy deployment, so you can just copy the executable and any necessary DLLs around in a directory. It doesn't need to be "installed" unless you explicitly create external dependencies.
#jiewmeng, here i leave a few keys to build an portable application
If the application need save additional data like configuration files o data files , must be saved to the same folder of the exe application or an child folder of the application.
The application should not read/write configuration data to the Windows registry or in the %Appdata% folder.
avoid the use of external dependencies like ocx o dll files, that need to register in the system.
try to use an language wich makes native applications without framerworks dependence a good recomendation is use Delphi.
If you want to use .Net language, choose a version of .Net framework, that is in common use in most of the systems like the Microsoft .Net 2.0
Use Delphi, it's always portable and smaller

Categories