Access files from inside class library - c#

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

Related

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

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.

Edit App-V archive file

I'm trying to replace a file in a .appv archive file.
I know you can just rename the file to .zip extract and replace the file.
But when I zip it again and rename it back to .appv the file size is different and i get the following error when opening.
Im trying to change the AppxManifest.xml file or a setting within that without having to open via the UI(cmd, c# or powershell).
Thanks
Open up your sequencer and choose "Modify"
"Update"
Then I am skipping the steps where you upload your .appv file and installer (if you don't need the files from installer, you can always ignore it)
"Continue to modify"
Close the last page and it will automatically jumps to this page
Now, you can open up your package, expand the folder and add files if you want
The best option is to use one of the application packaging tools that supports editing of App-V package. Apart from Advanced Installer that has been already mentioned, take a look at AdminStudio and PACE Suite - both support editing App-V. There may be even more than those two I know.
You cannot zip a package back and expect to work, this is not supported by Microsoft.
Starting with App-V 5.1 the Sequencer from Microsoft can export and import the manifest file from the package, so you can modify it, but from what I know you cannot script this, it must be done from the GUI. The list of powershell cmdlets for the sequencer is quite short.
Also, Advanced Installer can also create and edit App-V packages much easily and faster than the sequencer, but again only from the GUI.
You can also try a free tool, called TweakApp-V, here's an example of you can use it. It has predefined commands to add/delete files and registry.

Embed exe file in a Class library

Is it possible to embed an exe file in a class library (dll file)?
I want to use this exe without the necessity of copying it manually to my workstation. In other words, if I want to use my C# class library in another app, I won't need to copy these exe files in a folder and pass the path of this folder in my app.
If there is a way how to do this, it would be great.
sorry but what I mean is just, I made a library for screenshots using Selenium Webdriver and when creating a new webdriver object, I need to pass the exe file of the webdriver.
what I need the most is that I don't want to copy the exe file if I will use this library to another workstation for example, I want everything to be packaged as one file
Thank you
Well, technically you can embed a binary file as a resource within a DLL (by adding the file as a binary resource through the project properties), but you'll still need to save the file to disk in order to execute it (which is assume what you're trying to do) and will possibly have security issues unless your application is fully trusted.
If the binary file is a resource you can extract the bytes from the static Properties class:
byte[] exe = Properties.Resources.MyExe;
and save it to disk like any other byte array.
If you own the code for the EXE then it would be a lot better for you to turn your EXE code into a library. Then you'll be able to refer to that library from anywhere and call any public functionnality it has. It's a far, far, FAR better approach.
Should you still need to run that code as a standalone process, nothing prevents you from making a new EXE front that will refer to that same library.
Now if you do not have the code, then depending on your deployment strategy you may prefer creating a reusable deployment component / module that can be attached to other application setups.

Share a file for C# project at home and at office

I have formed a number of source code files as my library. For example, I wrote LinqExtension.cs providing Median() function.
Now I'm working on a project which needs LinqExtension.cs. As usually, I link the file to the project. As introduced here. The reason that I link files rather than copying them is to keep the files at a single location. If I modify a file, all dependent projects get affected.
I also add the project to Subversion and upload to and download from Google Code. The linked file is not under version control.
I work on the project at home as well as at office. I hate copying the linked file to my office, which makes the file not single.
I figure out a solution that add <Compile Include="http://www.example.com/LinqExtension.cs"/> to csproj file so that the file only exists on the Internet. Once I upload a new verison of the file, all dependent projects get affected. Unfortunately the solution doesn't work.
Any other suggesions or better practice?
A better way would be to share your core library at the binary level, rather than at source code. You could set up a private Nuget repository to make this easier.
If it is absolutely necessary to share files, you can use pre-build actions in your project to copy the file from a common location, or even download them from google code. It's not clean, but if you don't want to use source control for it then I don't think you will find a clean way.
I like to keep a library folder of binaries in my Dropbox. That way Common libraries that I use can be accessed from my home and work project workspaces and the service keeps the version up to date.

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.

Categories

Resources