无法设置 Process.StartInfo.WorkingDirectory 从 c# 调用 exe

Unable to set Process.StartInfo.WorkingDirectory to call exe from c#

我正在尝试使用 [=55 在 C# 程序中调用 chrome.exe =] 命名空间。

我的 chrome.exe 位于路径 C:\Program Files (x86)\Google\Chrome\Application[= 内14=]

如果我通过传递以下参数调用 RunProc 函数 -(保持 exe 的绝对路径并保持 WorkingDirectory 为空)

("C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe" , "https://www.google.com", "") 效果很好。

但是,使用参数 -

("Chrome.exe , "https://www.google.com", "C:\Program Files (x86)\Google\Chrome\Application") 它在步骤 proc.Start( ); 说明 - 系统找不到指定的文件。

我也尝试在初始化 StartInfo 时编写 WorkingDirectory = workingDir 但仍在寻找解决方案。

class Program
{
    static void Main(string[] args)
    {
        RunProc(@"chrome.exe", @"https://www.google.com", @"C:\Program Files (x86)\Google\Chrome\Application");
    }

    static bool RunProc(string exe, string args, string workingDir)
    {
        Process proc = new Process
        {
            StartInfo =
            {
                FileName =  exe,
                CreateNoWindow = true,
                RedirectStandardInput = true,
                WindowStyle = ProcessWindowStyle.Hidden,
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                Arguments = args,
                //WorkingDirectory = workingDir
            }
        };

        if (!string.IsNullOrEmpty(workingDir))
        {
            proc.StartInfo.WorkingDirectory = workingDir;
        }

        proc.Start();
        proc.StandardInput.WriteLine(args);
        proc.StandardInput.Flush();
        proc.StandardInput.Close();

        return true;
    }

}

为什么不直接从 .exe 所在的路径调用它呢?

Process.Start(@"C:\new\folder\abcd.exe");

或者只输入

proc.StartInfo.WorkingDirectory = @"c:\new\folder";

在 proc.start() 之前;

唯一可行的方法是在尝试启动其他进程之前将您的 工作目录更改为传入的工作目录。 WorkingDirectory 属性 就是这样,不会以任何方式参与将可执行文件定位到 运行。如果您未能提供完全限定名称,那仅取决于您的工作目录和您的 PATH 环境变量。

static bool RunProc(string exe, string args, string workingDir)
{
    var prevWorking = Environment.CurrentDirectory;
    try
    {
        Environment.CurrentDirectory = workingDir;
        Process proc = new Process
        {
            StartInfo =
            {
               FileName =  exe,
               CreateNoWindow = true,
               RedirectStandardInput = true,
               WindowStyle = ProcessWindowStyle.Hidden,
               UseShellExecute = false,
               RedirectStandardError = true,
               RedirectStandardOutput = true,
               Arguments = args,
            }
        };

        proc.Start();
        proc.StandardInput.WriteLine(args);
        proc.StandardInput.Flush();
        proc.StandardInput.Close();

        return true;
    }
    finally
    {
        Environment.CurrentDirectory = prevWorking;
    }
}

您如何看待在您的静态方法中结合 .exe 的绝对路径并在调用进程启动之前检查该路径是否存在:

using System.Diagnostics;
using System.IO;

namespace RunProc
{
    class Program
    {
        static void Main(string[] args)
        {
            RunProc(@"chrome.exe", @"https://www.google.com", @"C:\Program Files (x86)\Google\Chrome\Application");
        }

        static bool RunProc(string exe, string args, string workingDir)
        {
            string filePath = workingDir + "\"" + exe;

            if (!File.Exists(filePath))
                return false;

            Process proc = new Process
            {
                StartInfo =
                {
                    FileName =  filePath,
                    CreateNoWindow = true,
                    RedirectStandardInput = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = false,
                    RedirectStandardError = true,
                    RedirectStandardOutput = true,
                    Arguments = args,
                }
            };

            proc.Start();
            proc.StandardInput.WriteLine(args);
            proc.StandardInput.Flush();
            proc.StandardInput.Close();

            return true;
        }
    }
}

也许,方法RunProcDirectoryInfo and FileInfo

更清楚一点
using System.Diagnostics;
using System.IO;

namespace RunProc
{
    class Program
    {
        static void Main(string[] args)
        {
            FileInfo myRelativeFileExe = new FileInfo(@"chrome.exe");
            DirectoryInfo myAbsoluteFileDir = new DirectoryInfo(@"C:\Program Files (x86)\Google\Chrome\Application");
            
            RunProc(myRelativeFileExe, myAbsoluteFileDir, @"https://www.google.com");
        }

        static bool RunProc(FileInfo exe, DirectoryInfo workingDir, string args)
        {
            FileInfo myAbsoluteFilePath = new FileInfo(Path.Combine(workingDir.ToString(), exe.ToString()));

            if (!myAbsoluteFilePath.Exists)
                return false;

            Process proc = new Process
            {
                StartInfo =
                {
                    FileName =  myAbsoluteFilePath.FullName,
                    CreateNoWindow = true,
                    RedirectStandardInput = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = false,
                    RedirectStandardError = true,
                    RedirectStandardOutput = true,
                    Arguments = args,
                }
            };

            proc.Start();
            proc.StandardInput.WriteLine(args);
            proc.StandardInput.Flush();
            proc.StandardInput.Close();

            return true;
        }
    }
}