How to Reference A Binary File in a C# ClickOnce Install Application - c#

I'm currently using C# to build an app using WebView2, however, I need to make it accessible for those who are currently not on Edge. I decided to use a fixed version runtime, as documented here.
I'm going to use "EnsureCoreWebView2Async" to specify my environment.
My question is how do I reference the binary files needed in the "browserExecutableFolder" parameter in "CoreWebView2Environment.CreateAsync" if I'm going to package the project using ClickOnce? I won't know where the user will install the file, so I don't know what directory to reference. Could I just reference the file based on the package directory, like below:
"...\\bin\\Release\\net5.0-windows\\programName.exe.Webview2\\EBWebView\\x64\\EmbeddedBrowserWebView.dll"
...or is there another file reference syntax I would need to use?
Sorry if this seems like a beginner's question, but I just recently started learning C#/.NET and I'm still trying to get the hang of programming altogether. :-)

You can package the binary file in the click once installer. When this gets extracted the binary file will be in the same (i recon) directory as the application installed.
Which click once installer will be used for installing the application?
Once you know where the binary file gets placed on installation and if it is in the same folder as the installed application, you can use the following code for getting to that path.
var exePath = System.Reflection.Assembly.GetEntryAssembly().Location;
Documentation for above
When using it with Path.Combine(exepath, "nameofbinaryfile");
You can get a reference to the binary file.

Related

Access files from inside class library

Background Info:
I needed to convert PDF files to images and have full control over how this is done, and noticed that this functionality does exist in NuGet packages, but only in paid packages. Because I am a cheap bastard I refused to pay for this as I can easily do this in python for free.
I created a way to call a python script from c# that converts the PDF I want to a jpg in the exact way I want, and used this for a while. Now I want to build on this project and thought about creating this into a class library that I can use in multiple projects.
The problem I am now facing is that for this python script to work I need a specific resource, a couple of files in a folder. When I add the existing project (the pdf to img converter) to another project it just adds the DLL, and ignores everything else. The files I need are still in a folder inside the class library.
When I try to access the necessary files the current path is inside the new project, and there those files don't exist.
Directory.GetCurrentDirectory(); tells me that the current path is inside the new project.
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); tells me where the DLL is located, but the files I need aren't there.
I have tried searching for a way to access files inside the class library but I came up with nothing useful.
The question:
Is there a way to access files inside of a project that you are referencing? or is there a way to ensure the files I need are brought over too, instead of just the DLL?
Just for completion, I intend to make this into my own private Nuget package, and Im sure I can get the files I need in the .nupkg, but the same problem will persist, How do I access those files.
If anyone has any experience with this kind of problem, I would be happy to hear from you. Thank you in advance.
I would try to use a c# solution in this case to convert pdf to jpeg.
May be something like this:
Convert pdf to jpeg using a free c# solution
or optimal solution: a nuget package, which you can define as dependency in your nuget package
but there are many other solutions instead a phyton script.
Reason:
If you like to provide a nuget package (even if its company intern) the library
should not have dependencies that are not transparent.
In your case ist the dependency to an available phyton installation in the envirement, where the package will be used.
The software that uses the package must have the rights to execute a phyton script
In additional, you will resolve the issue, that you must be able to store and execute a script, if you use a c# solution

C# reference that requires additional files

I'm having a problem with a third party C# class library:
The given .net dll requires additional files to be in the same directory than the .dll (config files etc.).
When I load the library as reference into asp.net project everything works smoothly until first call to the library. I get exceptions from the library saying that the required files are missing.
Now the question is where should I put those required files into? I've tried obj/debug/, bin/ and including the files in the project and selecting "Always copy to output directory", but none helped.
I guess it's something to do with IIS and its way of handling the reference/additional files?
Edit: It's working fine in Windows Forms application, but not in ASP.NET web application.
You can view source code of external class library with iLSpy or Reflector to find how exactly additional files are referenced.
It's can be an application execute path (so it must be IIS shadow copy directory) or GAC.
For that you need to read the documentation. The method throwing the error should be expecting the file in some other folder other than bin, debug or obj.

Possible to remove and add a reference to csproj programmatically via a batch file?

I am writing a short batch file to prepare a control library DLL with Examples project for deployment via sip file and have the following question.
Given a csproj file at known location and a DLL at known location, is it possible to programmatically update the csproj from the batch file (via third party command line exe, or other scripting) to add the new DLL?
My folder structure is
/Build
/SDK
/WPF
/4.0 : ControlLibrary.dll sits here
/Examples
/WPF
/4.0 : Examples.csproj sits here
Assuming the batch file is at the /Build level, is there any way to modify Examples.csproj to reference ControlLibrary.dll ?
Just to clarify, the reason why I have the structure like this is I wish to deploy an examples csproj to ship with my control library. Examples.csproj should reference the obfuscated control library under SDK/. Examples.csproj also exists on dev trunk (where it has been copied from) and in the development solution it references output of ControlLibrary.csproj on non obfuscated form.
Essentially what im creating here is a folder structure to zip up and ship the ControlLibrary plus examples, hence, the need to update the reference.
Update - Solved using Powershell
Please see this related question and answer on adding and removing references using Powershell
csproj files are XML files - you can easily write a command line application to manipulate these files (adding, removing nodes etc).
You can call such a command line application from your batch file.
You can find the schema for this file at:
%windir%\Microsoft.NET\Framework\[framework version]\Microsoft.Build.xsd
As described in this SO answer.
I don't understand why you would need to modify the csproj file in your case. Just make sure the library reference in the csproj file is relative, i.e. ..\..\..\SDK\WPF\4.0\ControlLibrary.dll and it will keep working fine even if you move the complete folder hierarchy to a new location.
If you're trying to simplify adding the library to new projects though, take a look at NuGet. It's the best way for distributing and deploying libraries.

Why does VS2010 allow for the concept of "include in project"?

I'm still learning the basics of how VS2010 sees the world. Apparently, you can optionally "include" a file in a project. I'm a bit confused by this: If a file is version-controlled, AND the file is within the project directory, shouldn't it implicitly be "included" in the project? If not, what's the use case where a version-controlled file in the project directory should NOT be included in the project?
=== Addition ===
Based on the answers I've gotten so far, maybe I should rephrased my question: What does it mean for a file to be "included" in a project?
A project needs to know about files in order for compilation and distribution to occur. Just because you have a file that's under source-control, doesn't mean that it will be compiled if the project is unaware of it.
Also, you may want to include files as part of a distribution package. We do this quite often for our web projects that we distribute using web app gallery.
Conversely, you could have documentation or sql scripts that you version control, but do not want them to be part of the project.
EDIT: In answer to your update, what it means for a file to be included in a project is that the file is actually added to the .csproj or .vbproj file and will be used during compilation and/or distribution. VS does differentiate if the file is Content or if it needs to Compile it. This can be seen by clicking on the file in Solution Explorer and looking at the Build Action property.
No, you don't want random files that happen to be in the project directory included in source control.
We do sometimes put documentation (pdfs) or drawings/schematics in the project folder and under version control but you don't need them inside the visual studio project (especially when they are not being distributed because they are for internal use only).
Excluding the file from your project can be useful if the file is related to the project but not necessarily needed in the solution.
Example
If I need some test XML for an application that i'm writing; that is designed to normally be pulling this from a WCF service, it can be useful to keep that file in the directory for a development environment where I use IO to get the XML for testing, but I don't necessarily want it in my solution which is source controlled.
When you exclude a file from a project is no longer compiled or embedded, then when you want to include it again you can do so without having lost your settings.
If you e.g. copy a file (containing a helpful class which want to have in your project) into a folder of your project, then you will see ... nothing. You have to check the option "Show all files" of the solution explorer and the copied file can be seen, but it is still "greyed out". No you can choose the menuitem Include in project and that file will be integrated in your project and a pending change (add) for your source control is added too. Visual Studio doesn't include all files it can find in the project folder automatically to the project - and that is a good feature.
One of my colleagues explained to me a scenario in which a version-controlled file should NOT be part of the project. Here's the idea:
A developer writes some new code.
The code is experimental, and not intended to be part of the normal build.
The easiest way to exclude the file from the build is to NOT include it in the project, but still version-control it.
This way, the file can be shared with other developers, but not break the build.

Possible to package all needed files in an EXE?

I wrote an EXE that uses a third party dll and a template excel document. Anytime someone uses it they have to copy all three files (which is a pain).
Is there any way I can package everything that is needed into the EXE so there is only one file to worry about?
This is something that ilmerge is used for, atleast for combining assemblies. There is more information here and here.
There are several known problems with this though.
Yes - drag it into your project resources (My Project > Resources tab) and from there you can access it using global::Resources.resourceFile (I think - that might not be the right syntax, an alternative is here) as a byte stream and write it locally from there.
You can add files to your project in visual studio and set Build Action to None and Copy to Output directory to Copy always.
or
You can add these files to assembly resources. Adding and Editing Resources (Visual C#)
I suggest doing the first.
Also, for 3rd party dll: set Copy Local to true for the reference.
After you have your project set like this. Create setup project in VS and it will make one exe as you want.
How to: Create or Add a Setup Project
Software installation is a pain, but I think you can get away with an xcopy style deployment by using the compression tool of your choice (like winzip) and creating a self extracting executable.

Categories

Resources