问题
I add a batch file. let's say "batch.cmd" in my WPF application. I right click my project and add existing item, add my batch file in the project. I selected my batch file and change Build Action to Embedded Resource. After I build it, I saw this file add in project.
Now I need to start my batch file in my code, I tried Process.Start("batch.cmd"), it doesn't work, I did like:
Process p = new Process();
p.StartInfo.FileName = "batch.cmd";
p.Start();
It doesn't work either. How can i let my code understand this batch is existing inside my project and somewhere in the \bin. I don;t want to hard code it because the Path will always change.
Thanks in advanced.
回答1:
You need to save file from the resource to disk at run time and than call it from that location (may need to adjust "current directory" if it expects to start from a particular folder).
Links:
- How to embed and access resources
- Path.GetTempFileName - to get location where to save file (don't forget to delete when done).
- ProcessStartInfo.WorkingDirectory
Unverified code below:
var tempFileName = Path.GetTempFileName();
Assembly.GetExecutingAssembly()
.GetManifestResourceStream("my.cmd")
.CopyTo(File.OpenWrite(tempFileName);
Process.Start(tempFileName);
File.Delete(tempFileName);
回答2:
you mean like this?
Process p = new Process();
p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "\\batch.cmd";
p.Start();
来源:https://stackoverflow.com/questions/15141010/how-to-run-embedded-batch-file-using-process-start-in-c-sharp