c# application runs fine through visual studio but standalone exe fails - c#

My C# application runs fine (in both Debug and Release mode) when I run it through Visual Studio 2012 or executing the .exe through Debug/Release folder, but it fails when I copy that .exe to some other location and run it. The error message that I receive is:
Could not load file or assembly 'bms.Common, Version=5.0.0.1006, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
I am copying the .exe from Debug/Release folder to some folder. Am I doing it wrong?

In Debug folder exists only .exe file? No other dlls? You should copy all .dll files from Debug folder to target directory along with .exe file.
If you want package all libraries into one executable assembly then look at ILMerge utility http://www.microsoft.com/en-us/download/details.aspx?id=17630
usage:
C:\Program Files (x86)\Microsoft\ILMerge>ilmerge C:\my.exe C:\my.dll /out:C:\merged.exe

Generally speaking, you should be deploying the DLLs in the same folder as the executable. With .NET we don't usually compile everything into one big .exe the same way you might link a c program.
There are certain exceptions: for example, you could put the DLLs in the GAC or put them somewhere else and provide what is known as a "probing path." Those are advanced approaches which should not be used unless you know what you're doing.
For complete information on how .NET resolves DLL references, I suggest you read here.

Related

Visual studio: Build action=Content causes error for dll file

I am trying to build a project in C# which uses some unmanaged dll files (MSVCRT.DLL, DFORRT.DLL, ...). There is no direct reference to the MSVCRT.DLL file in the project(this file is referenced by another dll and not directly by my program)
I want this dlls to be copied to bin directory. if i set build action to Content i get this error on run time:
The procedure entry point _wcstoui64 could not be located in dynamic link library msvcrt.dll.
Which is weird. I cant use other build actions because i want my dlls to be published too(Build action=None wont cause any error but doesn't let me to publish my dlls)
If i completely remove all references to all the dlls and treat them like data files, or pictures(files that are not used in code but should be copied to the output) again i get the same error.
What is causing the problem? how can i publish my dlls?

How to include SharePoint assemblies in EXE after publishing

I have developed a simple .EXE in Visual Studio 2015, which adds an item to the specified SharePoint site and List when the user runs some process.
The file is saved in a shared network drive, and everything seems to work perfectly except for one detail.
I can't seem to work out how to run the EXE without having the following .dll files saved in the same folder:
Microsoft.SharePoint.Client.Runtime.dll
Microsoft.SharePoint.Client.dll
If I move these from the folder in which the EXE is saved in, I get this error message when running the exe:
Unhandled Exception: System.IO.FileNotFoundException: Couldt not load file or assembly 'Microsoft.SharePoint.Client, Version=15.0.0.0, Culture=neutral ... Or one of its dependencies ...
I (think) I have set up 'strong named assembly' by following these instructions in VS for the app, and I was able to add the SharePoint dlls to the GAC by using the VS command line and gacutil.exe (with a successful confirmation message), but I am still unable to run the EXE without having the 2 .dll files present.
Note: I developed the app to target .NET-4, but the users will be accessing the files from a system with .NET-3.5 installed, however the server on which the EXE is saved on does have the .NET-4 environment.
I was able to achieve this by using a tool called ILMerge which can be found here and is available as a NuGet package.
ILMerge allows you to merge dlls and exe files into a single exe

Copy some files to projectDir when someone compile their project using my DLL

I'm not sure if this question has been asked or not.
I have created a DLL in C#. My DLL depends on other DLLs. When someone compile their project using my DLL, the other DLLs will get copy to the projectDir.
However, the other DLLs depend on some text files and some other executable files. The problem is that the text files and executable files don't get copy to the projectDir. Unfortunate, that will cause the project to crash when running.
My question is, how can I load those text files and executable files to the projectDir whenever my DLLs is compiled?
Thanks.
Depending on how you are sharing it you could also look at using Nuget to publish out your DLL. With Nuget you are able to specify and bring across dependent files and libraries that will be added to your any project just by including them as content in the DLL project.

Could not load file or assembly, newtonsoft.json

I have a c# console application that is using JSON and it works fine when run on my development machine. When i try to move the application to another machine to run it i get the following exception when it comes to the part of my code which requires json:
Could not load file or assembly, newtonsoft.json 4.5.0.0
I presume i need to download/install this on the machine i want to run the application on. How would i go about doing that?
Download the Newtonsoft.Json.dll from http://json.codeplex.com/releases/view/107620 and place it in application's BIN folder.
Locate the missing assembly in the solution explorer (under references), right-click and select properties, then select
copy local: true
and the dll will be copied to your output folder and should be distributed with your program.

How is it that I could compile an exe in C# with no error but execution fails with dll not found?

I have created a console app referencing http://www.antlr.org/download/CSharp
using System;
using Antlr.StringTemplate;
using Antlr.StringTemplate.Language;
That I want to compile by command line with
csc /r:c\antlr\antlr.runtime.dll;c\antlr\StringTemplate.dll myprog.cs
It created the exe but when executing it says it cannot find StringTemplate or dependencies why ? I have all dll package in same directory.
Update: the same program build under visual studio works but I need to do this by command line.
Update 2: even copied to exe path it still can't find them weird !
Update 3: ok finally works I confused visual studio bin output and my csc output dir.
That's because you are not using the friendly IDE that takes care of these details for you. Since you're doing this by hand, it is up to you to copy the DLLs yourself.
csc /r:c\antlr\antlr.runtime.dll;c\antlr\StringTemplate.dll myprog.cs
copy c:\antlr\antlr.runtime.dll .
copy c:\antlr\stringtemplate.dll .
The DLLs are now in the EXE's 'probing path', it won't have any trouble finding them.
The dlls have to be in the same directory of the exe unless they are in the GAC.
DLLs are search for on the PATH env variable.
Unless you specially put ;.; in your path, CWD is not searched.
Could be a number of things. For me, the most common cause of an error like this -- assuming that all the DLLs are indeed where they should be (next to the running executable) -- is that your application is compiling as Any CPU, but you are running it on a x64 machine (so the CLR loads it as x64) but one of the assemblies you reference has to interop to native code or otherwise makes a reference to a x86 (32-but) unmanaged DLL. Since you can't load a 32-bit DLL into a 64-bit process, the assembly load fails.

Categories

Resources