ClickOnce deployed application can't find external DLL - c#

I have this WinForms application that uses an external dll file (Winppla.dll) so I can print tags in my Argox printer. Since I cannot add the dll file as a reference to the project, I use the command [DllImport("Winppla.dll")] on my class.
When I run the project in Visual Studio, it works perfectly.
Then I published my application using ClickOnce and when I try to run the application, I get the message error saying that the Winppla.dll could not be found, even though I am running it on my machine.
I tried to:
copy the file to the bin folder of the project before publishing
copy the file to the packages folder of the application before publishing
copy the file to the latest version folder on the Application Files folder (on the publishing location of the application)
add the file as a resource on the project
add a folder with the file as a Reference Path on the project
but none of these worked.
This project also uses SAP CrystalReports, and it works just fine.
Any ideas about how to make it work?

Following #Bearcat9425 instructions in the comments section of the question, I finally solved the problem:
Copy the file to the project folder and include it on the project
(on Solution Explorer, click on 'Show All Files', then right-click in the file and select 'Include In Project')
Mark the file as one of the application files on my publish tab
(right-click on my project and select 'Properties', then go to Publish tab and click on 'Application Files' and make sure the Winppla.dll is marked as 'Include')
Copy the Winppla.dll file to a shared folder in our server (I placed it on the same folder where I placed the setup of my application)
On each DllImport command write the complete path to the file shared location: [DllImport(#"\\the-path-on-the-server\Winppla.dll")]
Publish!

Related

How to copy .NET Framework Console Application subfolders to the release folder?

I was wondering if someone could please help me with something.
The main idea is that I want to have a .NET Framework Console App, which is build and packaged into a single file (an archive or something similar, like a jar file in the java world, containing all the referenced .dll files, sources files and additional project files) that i can deploy on another machine.
I've build an Console app using .NET 4.7.2 which is receiving data from a remote server and it pushes it to RabbitMQ. I use a .p12 file to authenticate to RMQ, which i keep stored in a sub-folder in my app called "Others".
When I run it from Visual Studio 2019 it works, but when I try either to release it and run it from that folder or to install it on my windows 10 machine it does not work anymore. It gives me the following exception :
RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable ---> System.AggregateException: One or more errors occurred. ---> System.Security.Cryptography.CryptographicException: The system cannot find the path specified.
...
When I publish the app, via the publish window, I select an output folder and I get the following files and folders :
(folder) Application Files
(file) setup, type of file - Application.exe (.application)
(file) ConsoleApp, type of file - Application Manifest (.application)
The RabbitMQ .dll file, which contains the RabbitMQ Client is located inside the folder "Application Files\ConsoleApp_1_0_0_13" together with other .dll files and a subfolder ("XMLRequest") but does not contain the other subfolders of my project.
I don't have the possibility to add these subfolders, where my .p12 key is located, i.e. "Others", in the publish screen of the application.
The other case when it does not work is when I to copy the whole "Release" subfolder into another location and just run the ConsoleApp.exe from there. I get the same error message.
Does anyone have an idea ?
Thanks !
Try following:
Open the project in Visual Studio
Go to Solution Explorer --> Click on Project --> Click "Show All Files" menu in Solution Explorer Menu
All the folders in your project will appear with dotted border in your project files tree
Right Click your folder and click "Include In Project"
Navigate to your file by expanding your folder in Solution Explorer
Right click file and click "Properties"
In Properties windows select "Copy always" for "Copy to Output Directory"
Performing above steps will cause Visual Studio to copy your entire folder to output (Debug and Release) directory.
There is also another option to publish required files during build and release process. Right click your required files and click properties. In the properties window set appropriate value for Build Action property to the value that you need. Following documentation describes when to use which Build Action Values.
https://learn.microsoft.com/en-us/visualstudio/ide/build-actions?view=vs-2019
Packaging your app as a single .exe file
If you want your .NET Framework Console App, to be packaged into a single file, then I would take a look at the nuget package Costura.Fody. It will package up all the projects DLLs into a single .exe for you. I use it all the time.
All you need to do is add the nuget package to your project like this:
PM> Install-Package Fody
PM> Install-Package Costura.Fody
and out will pop a single .exe
Embedding Resource Files - Option #1
If you want to include files in your deployment, what I have done in the past is embed them in the exe themselves and then extract them when the app runs.
To do this, add the files to your project as normal:
Then right-click the file and select "properties" and set the build action to be an "embedded resource".
This will alow you extract the file later on, when the program is running. With this setup you could have any number of resource files setup in the app.
Then on startup of the app, you can extract the embedded resource to a file on disk using a function like this:
public static string GetEmbeddedResource(string resourceName)
{
string resourceContents = "";
try
{
string[] names = Assembly.GetEntryAssembly().GetManifestResourceNames();
string resource = "";
foreach (string str in names)
{
if (str.ToLower().Contains(resourceName.ToLower()) == true)
{
resource = str;
break;
}
}
if (string.IsNullOrEmpty(resource) == false)
{
using (StreamReader sreader = new StreamReader(Assembly.GetEntryAssembly().GetManifestResourceStream(resource), Encoding.Default))
{
resourceContents = sreader.ReadToEnd();
}
}
}
catch (Exception ex)
{
throw;
}
return resourceContents;
}
with the usage of the above function looking like this:
var resource = GetEmbeddedResource("SomeFile.txt");
and then you can write the file to anywhere you need it on disk.
Embedding Resource Files - Option #2
The other option would be to copy the embedded resource to the output folder but then that means you wont have a single .exe file if you are manually copying this from machine to machine.
Embedding Resource Files - Option #3
If you are using ClickOnce technology to deploy your app, which it looks like you are, then when you go to the publish tab, if you select the "Application Files"
you can then choose which files to include in the deployment as seen here.

wpf string files not found on release version but work in debug c#

I have finished a very basic application (wpf/c#). The solution is made of 3 projects:
The main project for the app
The Class Library Project to store app resources (images and txt files)
The Setup project which I use to create exe file for distribution to other
machines.
While the project works fine in debug mode when I deploy it using the Setup project and install on the computer I can access the image files from the Library Project (I can see there is a dll file for the library project in the application folder) but it fails to access the text files, complaining the path was not found. This is my very first time I completed the application and attempted to deploy it so am a bit at a loss why the setup does not provide correct references to the text files and yet it seems to work fine with image files which are located in the same library project.
Can someone point me in the right direction where to look at it to troubleshoot?
I have cleaned and rebuilt all projects in the solution. retested in debug mode (works fine). tried to search msdn and StackOverflow but I cannot find any guidance I could use or follow.
I would like to be able to display text from the text files in the released/installed application version the same way it works in debug mode. At the moment it fails to find the relevant txt files.
Finally, I have managed to crack it. Posting the answer for anyone having the same problem.
The issue here was not with the file path, even though I came up with a more clearer technique of building it, see my comments above. The problem was with the way Setup Project in VS2017 was creating a package. It is handling differently images and text files, even though both are in the same Library Project, essentially for text files I had to do the following to get it working:
Open File System in Setup Project
Create the 'Resources' Folder under 'Application Folder'
Set the 'Resource' folder 'AlwaysCopy' property to 'true'
In 'Resources' folder right-click and select Add> File...
Navigate to the folder with the files and select them all (make sure the files are setup as Resources or embedded resources)
Rebuild the Setup Project
.
So summarising I had to specifically tell Visual Studio to build the folder structure in the Application Folder during the install.
Now when I run the installer the text files are included in the package and created during standalone installation. Also included a screenshot below.

How to publish a folder containing some necessary files inside a project

I am going to publish a C# windows form application project.
I have a folder named "Reports" Containing some report file.
when I publish the project , my application cannot find the path of reports.
How can I publish my Reports inside the project?
From solution explorer, choose each file that you need to include in the publish and in the properties of the file set Copy to Output Directory to true. This way your files will be included in bin\debug folder of your application in their current folder names.
Then if you want to use Publish command, go to properties of the project, in publish tab, click on Application Files and check Show All Files and change the Publish Status of the files you need to Include.
Pay attention, if your file's Build Action is Embedded Resource then you don't need to do anything else to include in the publish because it's included (embedded) in resources of your application.

Visual C# missing app.config file after build

My solution has two exe apps inside. Console application (Console.exe) and wpf application (MyUI.exe). Wpf application is a startup project, and console one is a small installation tool. Both have app.config file which contains db info. I have problem with this config file after I build the solution. In bin folder I have my wpf app and config file with the same name, e.g.:
MyUI.exe and MyUI.exe.config.
But there is no config file for console application. Is there any setting I should set to make it right?
When I set build action to content and copy to output... to Copy always then I have "App.config" file in bin directory, but there is no "Console.exe.config".
There is no such thing as a solution output folder - each project has it's own output folder, in which the App.config will be placed (after it's renamed to [exe_name].config).

app.exe.config file not published with ClickOnce application

I have a clickonce app that needs the app.config file deployed.
In Visual Studio, if I right click on the app.config file and select properties then change the Build Action to Content, the app.config.deploy file gets created, but there is no app.exe.config.deploy file.
I really don't even need the app.exe.config.deploy file and I don't know why Visual Studio (2010) includes it.
At this point, I click on the properties for the clickonce .csproj file, click on the publish tab and click on Application Files. "app.config" and "app.exe.config is set to "Include (auto)". But I don't have the option to disable the app.exe.config.
The reason I need app.config deployed is because I'm using the following code:
var str = ConfigurationManager.AppSettings["oem"];
I'm deploying multiple versions of this with an external build script. The build script swaps out different versions of the app.config for each different published version of the code.
Bottom line question. Is there a way to either:
Get the app.exe.config to deploy with the rest of the publish
Only get the app.config file to deploy (without the app.exe.config)
If I add say a text file to the project and set it's deployment type to "content" as specified above it gets included.

Categories

Resources