繁体   English   中英

Process.Start找不到指定的文件,但是绝对存在-为什么?

[英]Process.Start can not find file specified, but it is definitely there - why?

我正在尝试从C#运行DJOIN命令。 (默认情况下,它在Win 10的c:\\ windows \\ system32目录中。)

当我运行以下命令时:

ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.FileName = @"c:\windows\system32\djoin.exe";
psi.RedirectStandardOutput = true;
psi.Arguments = "/C toast";
using (Process proc = Process.Start(psi))
{
using (StreamReader reader = proc.StandardOutput)
{
   string result = reader.ReadToEnd();
   MessageBox.Show(result);
}

我收到“找不到文件”错误:

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll    
Additional information: The system cannot find the file specified

在此处输入图片说明

但是,如果我使用另一个“开箱即用”的.exe,例如“ tasklist.exe”,则可以正常工作。 例如:

proc.StartInfo.FileName = "tasklist.exe";

给我以下输出:

在此处输入图片说明

您还可以禁用64位操作系统的文件夹重定向

[System.Runtime.InteropServices.DllImport("Kernel32.Dll", EntryPoint = "Wow64EnableWow64FsRedirection")]
public static extern bool EnableWow64FSRedirection(bool enable);


        EnableWow64FSRedirection(false);

        try
        {
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.UseShellExecute = false;
            psi.FileName = @"c:\windows\system32\djoin.exe";
            psi.RedirectStandardOutput = true;
            psi.Arguments = " /C toast ";
            using (Process proc = Process.Start(psi))
            {
                using (System.IO.StreamReader reader = proc.StandardOutput)
                {
                    string result = reader.ReadToEnd();
                    MessageBox.Show(result);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        EnableWow64FSRedirection(true);

原来问题是DJOIN.exe是64位的。 我的应用程序以32位运行,因此我将平台更改为X64,并且可以正常工作。

请参阅: 以64位启动进程

尝试在应用程序为参数的情况下运行cmd命令。 以下对我有用:

ProcessStartInfo processInfo = new ProcessStartInfo("cmd", @"djoin.exe /C toast");

processInfo.RedirectStandardOutput = true;
processInfo.UseShellExecute = false;

Process ProcessObj = new Process();
ProcessObj.StartInfo = processInfo;

ProcessObj.Start();

暂无
暂无

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

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