Execute file in resourses folder without copying it to disk - c#

I have an executable file in the resources folder. I am able to run that file by writing it to the disk then locating the file and use the Process.Start method to run the executable. Here is an example of how I do this:
How can I execute the file without writing it to the disk. Is there a way I can execute the file from memory? Or maybe execute it directly from the resources folder?

You can't. One of the very hard requirements of Windows, an executable must exist on disk. It is fundamental to its architecture. Slowing down the malware authors is a happy side-effect. Google "memory mapped file" if you want to learn more about it.

If it's a .NET exe try Assembly.Load(byte[]). Be wary of CAS though.

Related

Pack data files with an application in c#

So I want to create a C# application that can be transmitted to users which can then run an "exe" file copying the data files. Therefore, the application should contain at least to things:
The "exe" file
The files containing the data
The data in the application will be in most cases a directory with files and subfolders. My concern is how to store this data. I was thinking about storing the data in ".bin" files while being able to read the data with the exe file and place the files/folders in the correct structure, but I don't know exactly how would I do that with files and folders. Does anyone have any suggestions ? Is there a better way? Do you have any recommended reads ?
Thanks!
When you compile in either debug or release mode, there will be an .exe file in the bin/debug or bin/release folder for your project together with all the project files. You can move that to whatever machine with the .NET framework installed and run the .exe.
If you want to make an actualy executable, that's more difficult to do. Check out this post for more inspiration on that.
If your build is meant for distribution, better compile it on Release mode.
You will find the assemblies on your project_folder/bin/Release.
You may want to copy all contents in the release folder by compressing it, or better yet, using an installer compilation tool, like InnoSetup - http://www.jrsoftware.org/isinfo.php/, that is free and will do your application installer just fine.

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.

How to compile batch file with resources to exe and run it with its exe in C#?

I have batch file and a folder with resources which include one executable file. This file in its turn uses these resources. I want to compile these resources (batch file, executable file and folder with resources) to one executable one. And after launch of this exe, my batch file inside should run with all the ensuing consequences.
I'd like to make it on C#. What should I begin with?
If you want to do it exactly as stated then a reasonable approach would be to package all your resources into one zip file, embed it as a binary resource in your C# executable and have the executable extract the resource, unzip the file into a temporary directory and run it from there with Process.Create.
#Jon's answer is correct, but you can save yourself the coding entirely - that's what self extractors do. Check out the WinZip Self Extractor for instance. It can do exactly what you want.

Adding Files/Folders to Program Files x86 Folder with C#

I am trying to create a file directory (folder) in the 32 bit Program Files folder to store data that a user will be creating in the program. However, it keeps saying that access is denied when I try to do so. Is there anyway to give the program permission to access the 32 bit program files folder and add subsequent folder and files to it? Here is my code below which is producing a run-time error due to not having permission.
string mainDirectory=Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)+"\\UKC Trial Reporter Files";
if (!Directory.Exists(mainDirectory))
{
Directory.CreateDirectory(mainDirectory);
}
Store your data in Environment.SpecialFolder.ApplicationData, Environment.SpecialFolder.LocalApplicationData or Environment.SpecialFolder.CommonApplicationData (if data have to be accessible by any user).
Never store it in %ProgramFiles%.
%ProgramFiles% is intended for... program files: executables, dlls, app.config files.
In addition of security reasons, there's yet another reason to do this never: when uninstalling, your application's installer must sweep all of files and folders it had created. And this is impossible, when list of files in installdir was changed during application lifetime.
Fortunately, we have UAC since Windows Vista has released.
Don't do that.
Program FIles is exactly that; it should contain EXEs and static resources.
Instead, you need to write to the user's SpecialFolder.ApplicationData folder.

Saving a file in resource folder of project

Like following,
The Media.html file is located in the folder of project.
How can I save this file outer user's local disk?
EDITED
**Sorry for question insufficiently. I mean how copy a resource file like Media.html to disk programatically, in runtime, not VS2010 environment.
You may use C#'s file handling techniques, use File.Copy method. Please go here

Categories

Resources