我正在尝试使用 System.Diagnostics.Process 命名空间在 C#程序中调用 chrome.exe 。
我的 chrome.exe 位于路径 C:\ Program Files(x86)\ Google \ Chrome \ Application
如果我通过传递以下参数来调用 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;
}
}
答案 0 :(得分:1)
唯一可行的方法是在尝试启动其他进程之前,将您的工作目录更改为传入的工作目录。 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;
}
}
答案 1 :(得分:0)
为什么不直接从它所在的路径中调用.exe?
array.filter
或者只是放
Process.Start(@"C:\new\folder\abcd.exe");
在proc.start()之前;
答案 2 :(得分:0)
在将静态方法中的.exe绝对路径合并在一起并检查该路径是否存在之前,您如何看待调用Process起点:
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;
}
}
}
也许RunProc
和DirectoryInfo和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;
}
}
}