location of external dlls in c# - c#

I am a newbie to c# and developed a small WPF application which depends on a external dll files. I have added it as reference in the visual studio and started using the methods in side it.
I started to face the problem when I am deploying this utility to other computers. It is mandating me to place the external dll file in the same location of WPF exe file. And I want to avoid this. Instead I want it to look a specific folder(local or remote).
Is it possible to do this way?
Thanks,
Sitaram

The best solution would be to set the LocalCopy Property of the Reference to true. That will copy it to your output exe.
A very bad solution would be to load the assembly manuall in code (Assembly.Load) and add it to your AppContext.

Related

Build same Project as Console and DLL

I've got an C# Project in Visual Studio, which has Console Application as Output Type.
But I also need a Class Library of this project for another solution.
Right now I have to switch the output type every time, but I wonder if it's possible to generate exe and dll at the same build-event?
Is there a post-build-event for this?
To my knowledge there is no possibility to change the output type after compilation. That being said, if would be possible to have two projects like Console and Library in your solution, which would use the same source code files but have different output types. That way you would have different outputs without any duplication of code.
it is generally possible to reference a .net exe assembly as it would be a class-library.
So you can just stick in creating an exe file and reference the exe (sounds strange, but works) in your other project.
This is the dialog for browsing for references. As you see you can select exe files.
But as commented it really depends on what your usecase is.
I don't recommend to ship an exe with an entry point to your customer hoping that the customer does not discover the exe. But what you could do about that is to conditionaly compile your entry point.
For example
class Program {
// This is the entry point of the EXE
public static void Main() {
#if DEBUG
// Start Debug Application
...
#else
// Shipped to client - Entry point disabled
return;
#endif
}
}
If there is a relevant reason to have a shipped exe and a shipped class library, I would refactor your solution like this:
(A) complete application (.sln)
(B) console-application (.csproj) which has a reference to (C)
(C) class library project (.csproj)
With that it is perfectly clear to others that there is an application that uses the library and the library itself.
Console Application is the type of your project. You can not change it.
What you can -and must- do is, carry your logic into a Class Library project and use your class library from any type of project you want.
You should compile your project to become a dll and then use this dll in a console application.
A possibility to achieve what you want is to manually run the msbuild on your post-build event of your project.
See: How do i build a solution programatically in C#?
or Building C# Solution in Release mode using MsBuild.exe
The usual solution for this is using a Solution with two projects:
a Class Library with all the code (which builds into a DLL)
an Console Application referencing the library whose Main just calls some function(s).
For more information, check the MSDN page on Solutions.
Codor suggested manually adding the files to the Console project, but one downside is that build settings are not shared between both versions, so you might get some inconsistency there.
I'm not really sure why people think it's not possible but it actually is.
The easiest way would be renaming the exe to dll Sounds stupid, I know. But it works in many cases. Also, as "Peter I" said a .NET exe can be imported as assembly in other projects. So you might not actually need a dll anyways.
Another way would be using C# command line as stated here: /out (C# Compiler Options)
You can use command command line options in Pre/Post build events Pre-build Event/Post-build Event Command Line Dialog Box
I have a similar requirement and couldn't find any definite answer in this post or anywhere. I currently have a class library and would like to create a console application project without copying any code. Ideally speaking there should be two projects, one for creating a console application and another for creating a class library. And this is what the visual studio also suggests. When I tried to run the class library, I got the below message.
It clearly asks us to add an executable project to the solution and add the reference to the library project.
Below are the steps to do this.
Right click solution -> Add new project -> Console App -> choose name -> ok.
Right click on the console project -> add reference -> In reference manager, click on the projects tab and select the other project(In my case this is the class library project, In case it is not listed just click on browse and select the .csproj file) -> ok.
Now to use the classes in the other project, simple do using LibraryProjectNameSpace
There we are. Bingo!!!!
Also as mentioned in the other answers it is not possible to have the same project generate both .exe and .dll. But you can have the same solution generate these two guys by having two projects. In this way there is no need to switch the output of the project every time.
FYI, I use visual studio 2017

What files are mandatory in release windows form?

I have files in ...bin/release where is my windows form application, I have used EEPlus library as well. What the files do I need to send to client to have application work correctly?
My files:
name.exe
name.exe.config
name.pdb
name.vshost.exe
name.vshost.exe.config
name.vshost.exe.manifest
EEPlus.dll
EEPlus.xml
I know that first two are mandatory, but what about all rest?
thanks in advance
name.exe //necessary, it is your main executable
name.exe.config //necessary, it is your application config file
name.pdb //not necessary, it contains code and debug symbols configuration of your assembly, but let it be there, it is useful when users encounter a bug or crash
name.vshost.exe //not necessary, it is the hosting process of visual studio for debugging purposes
name.vshost.exe.config //not necessary, config file of name.vshost.exe
name.vshost.exe.manifest //not necessary, manifest of name.vshost.exe
EEPlus.dll //necessary, it is one of your application dependencies
EEPlus.xml //not necessary, contains some information for EEPlus.dll
reference for xml, reference for vshost, reference for pdb
All except *.pdb and *vshost*.
Really you should look at the REFERENCES of your project.
Generically all the assemblies that don't make part of the NET Framework need to be redistributed.
In this case, it seems that you need to distribute EEPlus.dll only.
The other files are there just as a byproduct of the compilation.
Of course, you should also consider that a thirdy party library could need other files, but this should be explained in their documentation under the redistrubute page.
You could try this:
Delete all files except the name.exe, name.exe.config and EEPlus.DLL, then run your app outside VS directly in the BIN\RELEASE folder. However I recommend to have a clean virtual machine where you could test your app and be sure to not forget anything.
Always
name.exe
name.exe.config
Dependency dlls
Interop dlls
First we check this mandatory files before giving to client, because at client, when running application that will be crashed without displaying any errors...

Add C# code to exe file directly without using the build command from visual studio

I have an exe file for my application using visual c#.
Whenever I have to change, I go to visual studio, change or add the code and build the application.
So that my exe file will be updated.
Now what I want to do is I just want to add new changes c# script to exe directly.
I don't want to build the application from visual studio again and again whenever I have to change the code in application.
Because sometimes it's take too long to build for big application.
Is there any tools or way to do it?
Thanks.
Uhm no.
The most you can do is avoid visual studio, edit the source files using a lightweight editor, and re-compile the assembly using msbuild (e.g., msbuild.exe mysolution.sln).
If your project is large, building it can take some time. If you only need to change a small portion, you should seperate your project into smaller assemblies so your change in one assembly does not force a rebuild of your whole infrastructure.
If we're talking purely about building a project outside of visual studio then use an MSBuild scipt.
There is a decent tutorial here: http://goo.gl/MOseG
Note that you can reference static assemblies if you only want that project to be built, but be aware of stale references!
Now, let's have a look at the source of a your problem, large build that take a long time. I susggest you read here:
http://msdn.microsoft.com/en-us/library/ee817674.aspx
It may be wise to split you solution into several sub solutions.
Well you could seperate that file which you change often into an own assembly and just rebuild that assembly. Of course you need to make an reference to that assembly in your application.
Have you tried commandline compiling? It can be faster then Vs cause you dont have to open Vs (if you changed you code in notepad for example). Otherwise it nearly takes the same time (or longer cause you have to type in paths).
csc /out:YournewExe.exe YourFile.cs
Csc is the c# compiler and can be found at C:\Windows\Microsoft.NET\Framework\Version
Further information can be found here Click
Why don't you just save your project file everytime instead of building it? And when you finish working on it for today,just build the app then.
You don't have to test the app everytime through the .exe file,do you?

Visual Studio 2010 dll create error

I am new to C# programming and I have a problem with dll creation.
I opened a class library project and write public static methods in my classes. Clicked debug and copied dll and pdb files (under bin/debug/..) to my WPF application project.
I didn't get any reference problems also editor shows my methods normally, also when I use them it gives me no error or warning...
However, when I run my program, I saw that my methods calling dll methods are not working. In addition, debug mode also jumps my methods so I cant trace the code.
Where am I doing wrong? Is there any other way to create dll or am I missing a trick in here?
Thank you..
Rather than copying the DLLs into your WPF app's bin directory, you should either add a project reference to your class library from your WPF app, or add a reference to the output directory of the class library. Otherwise the build is probably copying over your hand-copied files. Basically, you should treat anything in bin as "controlled by Visual Studio" IMO - don't copy anything there manually. It helps if you use project references rather than referring to specific files, too - that way each build gets an appropriate configuration for its dependencies.

Configuration files with COM

I have an old PowerBuilder application that we are slowly phasing out. We are also moving to a more service orientated. So in order to facilitate this we are using C# COM wrappers to call WCF methods so old direct SQL calls can be slowly removed. We also use the C# COM wrappers when need functionality is needed in the power builder application.
Since we are using COM calls to DLL from PowerBuilder to C#, there is no need for an external executable. This means that a app.config file will not be loaded on its own. At least that is what I noticed. Example: Let's say the main DLL that has the wrapper methods is Wrapper.dll. If I had config named Wrapper.dll.config it would not get loaded when the make my call from PowerBuilder to C#.
The reason I would like to use a config file is because I would like to start using log4net in the C# dlls in order to make debugging easier because it is hard enough with PowerBuilder. There are other reasons that I would like to load configuration files but the easiest to explain is basically it is easier to set up some stuff using a config file.
So is there a way to load a configuration files into the Configuration manager for a COM call?
Thanks
Tony
Check out this code from Mike Woodring. I think it will enable you to do what you want.
Nice snippet JP.
By default, Runtime assemblies actually get their config settings from the calling executable's config file. Your snippet allows the loading of one associated with the actually library assembly.
Thanks for the answers, while helpful it was not what I was looking for. The "easiest" to do what I need is to name the config file after the calling applications exe. So if the application's name is test.exe and your C# dll is wrapper.dll, then you would name the config file test.exe.config. Since test.exe in this case is a PowerBuilder application, I can get away with this for now. If it were a .net app (and probably others) it would probably already have a config and thus get in the way.
Tony.

Categories

Resources