简体   繁体   English

C#中的.CMD执行莫名其妙地失败

[英].CMD execution in C# inexplicably failing

I'm attempting to execute a .cmd process from a C# program. 我正在尝试从C#程序执行.cmd进程。 When I run the process in command line, ie 当我在命令行中运行该进程时,即

C:\Directory\Process.cmd  -x 1000 -y 1000 C:\Input\input.txt

I get an appropriate result (in this case, that means that the process writes a file to: 我得到一个适当的结果(在这种情况下,这意味着该进程将文件写入:

C:\Output\output.txt

However, When I try to run this process from a simple C# program, the output file is not created. 但是,当我尝试从简单的C#程序运行此过程时,未创建输出文件。 Here are a couple of my attempts: 这是我的一些尝试:

Attempt 1) 尝试1)

try
{
    string processName = @"C:\Directory\Process.cmd";
    string argString = @" -x 1000 -y 1000 C:\Input\input.txt"; //The extra space in front of the '-x' is here on purpose
    Process prs = new Process();
    prs.StartInfo.UseShellExecute = false;
    prs.StartInfo.RedirectStandardOutput = false;
    prs.StartInfo.FileName = processName;
    prs.StartInfo.Arguments = argString;
    prs.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

    prs.Start()
}
catch (Exception e)
{
    Console.Writeline(e.Message);
}

Attempt 2) 尝试2)

try
{
    System.Diagnostics.Process.Start(@"C:\\Directory\\Process.cmd", " -x 1000 -y 1000 C:\\Input\\input.txt";
}
catch (Exception e)
{
    Console.Writeline(e.message);
}

Now, in both cases, no exceptions are thrown, and Process.cmd is accessed (it prints status updates in a shell), but the process does not create any output files. 现在,在两种情况下,都不会引发异常,并且可以访问Process.cmd(它在shell中打印状态更新),但是该进程不会创建任何输出文件。 Is there something wrong with the way I am attempting to call Process.cmd, that it works properly when run directly form the command line but does not work properly when I attempt to call it from my C# program? 我尝试调用Process.cmd的方式有什么问题,当直接从命令行运行时它可以正常工作,但是当我尝试从C#程序中调用它时却不能正常工作吗?

Thy this one? 你这个吗?

System.Diagnostics.Process.Start("cmd.exe", @"/c C:\Directory\Process.cmd -x 1000 -y 1000 C:\Input\input.txt");

AFAIK, '@' prepends verbatim strings, wich do not requires backslash masking ) AFAIK,'@'加上逐字字符串,但不需要反斜线掩码)

The file may be getting created, but not where you think. 该文件可能正在创建,但不是您想的位置。 Use 采用

prs.StartInfo.WorkingDirectory = "yourpath"

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx http://msdn.microsoft.com/zh-CN/library/system.diagnostics.processstartinfo.workingdirectory.aspx

The WorkingDirectory property must be set if UserName and Password are provided. 如果提供了用户名和密码,则必须设置WorkingDirectory属性。 If the property is not set, the default working directory is %SYSTEMROOT%\\system32. 如果未设置该属性,则默认工作目录为%SYSTEMROOT%\\ system32。

If the directory is already part of the system path variable, you do not have to repeat the directory's location in this property. 如果目录已经是系统路径变量的一部分,则不必在此属性中重复目录的位置。

The WorkingDirectory property behaves differently when UseShellExecute is true than when UseShellExecute is false. UseShellExecute为true时,WorkingDirectory属性的行为与UseShellExecute为false时的行为不同。 When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable. 当UseShellExecute为true时,WorkingDirectory属性指定可执行文件的位置。 If WorkingDirectory is an empty string, the current directory is understood to contain the executable. 如果WorkingDirectory是一个空字符串,则当前目录将被理解为包含可执行文件。

When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. 如果UseShellExecute为false,则不使用WorkingDirectory属性查找可执行文件。 Instead, it is used by the process that is started and only has meaning within the context of the new process. 相反,它由已启动的流程使用,并且仅在新流程的上下文中具有含义。

I deleted this after realizing that a path is passed in as an argument and was probably using hardcoded path logic for the file it writes to, but since a comment has referenced this, I'll undelete in case it is still of assistance. 我意识到将路径作为参数传递并且可能对其写入的文件使用了硬编码的路径逻辑后,将其删除,但是由于注释已引用此路径,因此我将取消删除,以防它仍然有帮助。

So I was finally able to get my hands on the source code, and realized that the problem was in the java code... it was interpreting the project directory as the output directory. 因此,我终于能够接触到源代码,并意识到问题出在Java代码中……它正在将项目目录解释为输出目录。 Thank you for all of the help though, you guys gave some very useful information! 谢谢您提供的所有帮助,你们提供了一些非常有用的信息!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM