如何运行嵌入式批处理文件使用process.start在c# [英] How to run embedded batch file using process.start in c#

查看:424
本文介绍了如何运行嵌入式批处理文件使用process.start在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个批处理文件。让我们在我的WPF应用程序中说batch.cmd。我右键单击我的项目并添加现有项目,在项目中添加我的批处理文件。我选择了我的批处理文件并将Build Action更改为Embedded Resource。

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.

现在我需要在我的代码中启动我的批处理文件,我尝试了 Process。开始(batch.cmd),它不工作,我喜欢:

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

它也不工作。如何让我的代码理解这个批处理是存在于我的项目和\bin中的某处。

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.

感谢高级。

推荐答案

您需要在运行时将文件从资源保存到磁盘,而不是从该位置调用它(如果希望从特定文件夹开始,可能需要调整当前目录)。

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).

链接:

  • How to embed and access resources
  • Path.GetTempFileName - to get location where to save file (don't forget to delete when done).
  • ProcessStartInfo.WorkingDirectory

以下未验证的代码:

  var tempFileName = Path.GetTempFileName();

  Assembly.GetExecutingAssembly()
    .GetManifestResourceStream("my.cmd")
    .CopyTo(File.OpenWrite(tempFileName);

  Process.Start(tempFileName);
  File.Delete(tempFileName);

这篇关于如何运行嵌入式批处理文件使用process.start在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆