Click here to Skip to main content
16,021,226 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
HI,

I am a beginner to CSHARP!
Please tell me if there is a way to save an .exe (Executable) file in a resource file or any where and run it any where?

Example
1. A file with name abcd.exe is to be added to a resource file or any location in the script (Please tell me how to add also)

2. The file abcd.exe is executed on clicking to a button.

3. It must be usable in any form as Form1 is a parent MDI.

Please help me . A good reply with better explanation would be useful .

Thank you for the people who replyed and to the people who tried to help!!
Posted
Comments
Sergey Alexandrovich Kryukov 16-Aug-11 23:34pm    
If you're a beginner, why doing such tricks? Learn some sanity of methodical development first.
--SA
Sergey Alexandrovich Kryukov 16-Aug-11 23:35pm    
Another advice: forget about MDI. Never remember. What's good about it? It's totally discouraged, even by Microsoft.
--SA

Your question is not clear, but I will have a go.

Yes, you can include any file as a resource in an assembly. (If using Visual Studio, add it to the project and Embad as Resource ... I think it's similar in #Develop and probably other IDEs. On the command line use /res:abcd.exe.)

To run a file, you must unpack it, save it to disk and Process.Start it.
void RunFileFromResource(string name){
 Stream s = Assembly.GetExecutingAssembly.GetManifestResourceStream(name);
 string tempPath = Path.GetTempPath();
 FileStream fs = File.Open(tempPath + "/" + name, FileMode.Write);
 s.CopyTo(fs);
 fs.Close();
 s.Close();
 
 Process.Start(tempPath + "/" + name);
}


Remember that if you're in VS you will get the irritating namespace prefix on all your resources (as far as I am aware there is no way to turn this off). Your assembly must have access to the file system and be allowed to start processes (which means full trust) for this to work, obviously.

Your point 3 does not make sense with the rest of the question. What you may mean is whether it is possible to link an assembly as a resource, and load forms from within it at runtime. While it is, that is a strange thing to want to do (if you want it as one file you can just combine all the classes into one assembly).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Aug-11 23:37pm    
Good answer to something making little to no sense, my 5. A better way of embedding resource would be creating a resource file (resx) and adding existing file to it (not yet added to the project, it will be done automatically with correct properties).
--SA
Why are you hiding an executable file in your program? Why not just build a setup project and install it correctly?
 
Share this answer
 
Not really sure about what you are doing or why. If the executables are plug-ins then you want to use a plug-in framework[^]

Can you provide more info on what you are trying to do?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900