我正试图使用System.Diagnostics.Process命名空间在C#程序内调用chrome.exe。
mychrome.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)GoogleChromeApplication");
}
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;
}
}
实现这一点的唯一方法是在尝试启动其他进程之前,将您的工作目录更改为传入的工作目录。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所在的路径调用它呢?
Process.Start(@"C:newfolderabcd.exe");
或者只放
proc.StartInfo.WorkingDirectory = @"c:newfolder";
proc.start((之前;
您认为如何在静态方法中组合.exe的绝对路径,并在调用Process start:之前检查该路径是否存在
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)GoogleChromeApplication");
}
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;
}
}
}
也许,使用DirectoryInfo和FileInfo 的方法RunProc
更清晰一些
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)GoogleChromeApplication");
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;
}
}
}