开发者

start a Process (executable) in Resources

开发者 https://www.devze.com 2023-03-17 04:04 出处:网络
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].

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();

p开发者_如何学Gols 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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号