从C#控制台应用程序执行批处理文件. [英] Executing batch file from C# console application.

查看:157
本文介绍了从C#控制台应用程序执行批处理文件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试运行一个包含命令的批处理文件,以命中nunit控制台运行程序可执行文件.我正在使用system.diagnostics.process方法运行批处理文件

这是代码:

方法中使用的路径:
C:\ Users \ Victor \ workstride自动测试\ DextapAutomation \ SeleniumDemo \ bin \ Debug \ ../../Batchfiles/

批处理文件名称:RandomBatchfile.bat


ProcessStartInfo startInfo =新的ProcessStartInfo();
startInfo.WorkingDirectory =上面指定的目录
startInfo.CreateNoWindow = false;
startInfo.FileName = RandomBatchfile.bat
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments ="C-Sharp控制台应用程序";

试试
{
//使用我们指定的信息开始该过程.
//调用WaitForExit,然后using语句将关闭.
使用(Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch(异常e)
{
Console.WriteLine(
@尝试运行测试时出了点问题,
这是例外:{0},e.ToString());
}

我尝试过的事情:

当我运行程序时,它说找不到指定的文件,并且它引用的目录是解决方案的本机目录,而不是我在上面指定的目录.我检查了该过程是否开始之前是否已设置目录,因此,我很困惑.请帮忙!

Hello Everyone,

I am trying to run a batchfile that contains commands to hit a nunit console runner executable. I am using the system.diagnostics.process method to run the batch file

Here is the code:

Path used in method:
C:\Users\Victor\workstride-automated-tests\DextapAutomation\SeleniumDemo\bin\Debug\../../Batchfiles/

Batchfile Name: RandomBatchfile.bat


ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = Directory specified above
startInfo.CreateNoWindow = false;
startInfo.FileName = RandomBatchfile.bat
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "C-Sharp Console application";

try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch (Exception e)
{
Console.WriteLine(
@"Something went wrong when trying to run the tests,
here is the exception: {0} ", e.ToString());
}

What I have tried:

When I run the program it says it cant find the specified file and the directory it references is the native directory for the solution instead of the directory i specify above. I checked to see if the directory is being set before the process begins and it is, so I am stumped. Please Help !

推荐答案

为什么在绝对文件夹规范中使用两个相对目录代码?
试试:
Why are you using two relative directory codes in an absolute folder specification?
Try:
Process.Start(@"C:\Users\Victor\workstride-automated-tests\DextapAutomation\SeleniumDemo\Batchfiles\RandomBatchFile.bat", "C-Sharp Console application");

并查看是否可行.


这篇关于从C#控制台应用程序执行批处理文件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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