简体   繁体   中英

start a Process (executable) in Resources

ive added my executable file in the resources, however, when i tried to build my project, i got a file not found exception [The system cannot find the file specified].

here is my code

Process sortProcess = new Process();
byte[] connect = sample.Properties.Resources.connect;
sortProcess.StartInfo.FileName = connect.ToString();

pls help me. you can try to create your own demo project, add some executables in resources, then start the process.

thank you very much PS: im trying to access the executable in the resources which is an embedded resource.

Your using a Directory as a Byte Reader, if your willing to drop the Resource to your C Drive and run the dropped file, then use this:

                    using (FileStream MsiFile = new FileStream("Temp.exe", FileMode.Create))
                    {
                        MsiFile.Write(Properties.Resources.Stub, 0, Properties.Resources.Stub.Length);
                    }
                    Process.Start("Temp.exe");

Thats if your wiling to drop the file, but if you dont want to take the risk of someone viewing the source of the Temp, then try extracting the resource file to ram, im still trying to find out how, but if i find out, ill let you know;D

You need to make sure the file is copied on build time.
In visual studio, right click on the exe file in the solution browser and go to properties.
I believe it was under "compile action" where you could select "always copy" or something like that.
Sorry I don't have VS in front of me, but it was there under properties.

Edit Sorry I answered this in kind of a hurry. I don't think you can or should embed executables like this. What is happening in your code is that you are reading the entire exe file, byte by byte, to string, which ofcourse would not result in a filename.

Embedded resources are mainly for additional content like images, text files and such.

To add the.exe to your project, you can simply drag'n'drop it into the solution explorer. Set 'build action' to 'content' and 'Copy to output directory' to 'always copy' or 'copy if newer'.

Basic code for running a process would be:

Process p = new Process();
p.StartInfo.FileName = "pathtoexe.exe" //can be relative path
p.Start();

Check the documentation on Process and ProcessStartInfo for more advanced options http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM