How to get file path of content in class library - c#

I have an Excel file that has a Build Action of content in a class library. The library is referenced by both an ASP.NET WebForms application and a console application, and the Excel file is used by both. Is there a way I can programmatically get the file path of this file?
I know in ASP I can do this:
HttpContext.Current.Server.MapPath(#"bin")
And in the console app I can do this:
Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
Those work, but I'm wondering if there's better, consolidated way to find the file from the assembly and return the path.

The second approach works fine in all environments you just should change it so that it does not look for the entry assembly but the specific assembly.
Easiest way to do this is:
typeof(Some.Type.From.That.Assembly).Assembly.Location
An alternative would be to use embedded resources so that you can load the file directly from the assembly. Of course, this would not work if the file has to be writable by the user.

Related

Access to an embeded resource file doesn't work on Xamarin but on Windows App

I added an excel File (template.xlsx) to the project resources as an embedded resource. In my code I want to save that file to disk.
On Windows it works well with:
File.WriteAllBytes("test.xlsx", Properties.Resources.template);
But in my Xamarin.Forms project exactly the same line does not work because "Properties" is unknown in the current context.
I searched half a day in the net but all hints I got don't work either or confuse me even more. It seems I am missing an essential piece of base knowledge here. Could somebody tell me if I can easily access an embeded File in Xamarin similar like I do on Windows?
The difference I can see in the two test projects is:
In the project explorer of the windows project I can see Properties->Resources.resx. However, the template file is not located there but in Resources->template.xlsx
In the project explorer of the Xamarin project there is no Resources.resx in the Properties folder.
My file is again located under Resources->template.xlsx
You have to read that file in your code and then use it to write that file to file storage (application sandbox) of the platform. You can save files to that location, although you may not easily be able to access that file outside the scope of the app (say from another application).

How to merge a file to a precompiled exe application

I have designed and created two seperate windows application ( say winapp1 and winapp2). Winapp1 creates a file(say file.txt) in the disk containing some text. And I need to merge this file.txt with precompiled version of winapp2 ie winapp2.exe so that the winapp2 can display it.
Winapp1 creates a file with different contents each execution time. And for each file there will be a copy of winapp2 that needs to be merged.
Is there a way to this? If there is, help me.
And my English is not so good. Thanks for reading this.
The easiest solution, (assuming that releasing the source code for winapp2 to the system that runs winapp1 doesn't present problems) is to have the entire source for the winapp2 project accessible to winapp1. The source should include a blank copy of the file as an embedded resource. Winapp1 can then modify the source file and use MSBuild to build a fresh copy of Winapp2.
To modify the text file, you should be able to just use the normal file manipulation .NET methods and to call MSBuild, you may be able to do it via a .NET class, but you can certainly just call the msbuild executable from the relevant .NET framework folder in %windir%\Microsoft.NET\Framework\ and pass in the full path of the project file.

Use external existing file as resource file in .NET

I'm writing some integration tests for some SQL scripts that live in a folder separate from the project. Since the setup of the machine I'm writing on the tests on differs from the machine they will be run on, I would like to include them as resource files rather than hard coding any paths. However the default behavior for adding an existing file as a resource file simply copies the file, which is not what I want in case any of the SQL scripts get updated.
Does anyone know the best way to get the resource file to actually reference the SQL scripts that are in a folder separate from the project, or to somehow copy them into the assembly at compile time so I don't have to load them via absolute/relative paths?
Edit: For clarity, I'm trying to get the resource file to act as a symlink to the original file at compile time. Adding an existing file to a resource in file in Visual Studio simply makes a copy of the file, which means any changes to the original are not propagated to the resource file.
After asking on IRC, someone guided me to what I was looking for. You are able to add an existing file to a project as a link (there is an arrow on the Add box when in the file dialog), and set it's Build Action property to Embedded Resource
https://support.microsoft.com/en-us/kb/306234
Daniel posted a link on how to actually read an embedded resource:
How to read embedded resource text file

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.

Include and Reference Resource File from C# class

I have an image that is used in some PDF files that my C# application generates. I know how to reference the image file when it is located in my workspace, but when I compile the program, I don't see the image anywhere in the compiled directory.
Can someone tell me what happened to that file, or do I have to manually package the file along with my program when I send the program to the users? I added the image to the workspace by drag-drop to the resource directory of one of my namespaces.
Check the file's properties in Visual Studio. Have you set the CopyToOutputDirectory property to something besides Do not copy?
Hmm... I'm not sure about the whole drag-drop business, but if it's all working the resource will have been embedded into your assembly.
I suggest you have a look with Reflector - you can see the resources embedded within assemblies using that.
Have a look at the build properties for the item in Solution Explorer - in particular, if the build action is Embedded Resource then it will indeed be built into the assembly.

Categories

Resources