How to run embedded batch file using process.start in c# [duplicate]

余生长醉 提交于 2020-01-05 04:39:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!