C# 无法将Process.StartInfo.WorkingDirectory设置为从c调用exe#
我正试图使用System.Diagnostics.Process命名空间在C#程序中调用chrome.exe mychrome.exe位于路径C:\ProgramFiles(x86)\Google\chrome\Application 如果我通过传递以下参数调用RunProc函数-(保持exe的绝对路径并保持WorkingDirectory为空) (“C:\ProgramFiles(x86)\Google\Chrome\Application\Chrome.exe”、“”、“”)它工作正常 但是,有了参数- (“Chrome.exe,”,“C:\Program Files(x86)\Google\Chrome\Application”)在步骤proc.Start();中出现异常,说明-系统找不到指定的文件 在初始化StartInfo时,我还尝试编写了WorkingDirectory=workingDir,但仍在寻找解决方案C# 无法将Process.StartInfo.WorkingDirectory设置为从c调用exe#,c#,process,system.diagnostics,C#,Process,System.diagnostics,我正试图使用System.Diagnostics.Process命名空间在C#程序中调用chrome.exe mychrome.exe位于路径C:\ProgramFiles(x86)\Google\chrome\Application 如果我通过传递以下参数调用RunProc函数-(保持exe的绝对路径并保持WorkingDirectory为空) (“C:\ProgramFiles(x86)\Google\Chrome\Application\Chrome.exe”、“”、“”)它工作正常 但是
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的绝对路径,并在调用进程开始之前检查该路径是否存在:
使用系统诊断;
使用System.IO;
名称空间RunProc
{
班级计划
{
静态void Main(字符串[]参数)
{
RunProc(@“chrome.exe”,@”https://www.google.com“,@“C:\ProgramFiles(x86)\Google\Chrome\Application”);
}
静态bool RunProc(字符串exe、字符串args、字符串workingDir)
{
字符串filePath=workingDir+“\”+exe;
如果(!File.Exists(filePath))
返回false;
proc流程=新流程
{
StartInfo=
{
FileName=文件路径,
CreateNoWindow=true,
重定向标准输入=真,
WindowsStyle=ProcessWindowsStyle.Hidden,
UseShellExecute=false,
RedirectStandardError=true,
重定向标准输出=真,
参数=args,
}
};
proc.Start();
过程标准输入写入线(args);
proc.StandardInput.Flush();
proc.StandardInput.Close();
返回true;
}
}
}
也许,方法RunProc
使用and更清晰一些
使用系统诊断;
使用System.IO;
名称空间RunProc
{
班级计划
{
静态void Main(字符串[]参数)
{
FileInfo myrelativefilexe=新文件信息(@“chrome.exe”);
DirectoryInfo myAbsoluteFileDir=新的DirectoryInfo(@“C:\Program Files(x86)\Google\Chrome\Application”);
RunProc(MyRelativeFileXe,myAbsoluteFileDir,@“https://www.google.com");
}
静态bool RunProc(FileInfo exe,DirectoryInfo workingDir,string args)
{
FileInfo myAbsoluteFilePath=新文件信息(Path.Combine(workingDir.ToString(),exe.ToString());
如果(!myAbsoluteFilePath.Exists)
返回false;
proc流程=新流程
{
StartInfo=
{
FileName=myAbsoluteFilePath.FullName,
CreateNoWindow=true,
重定向标准输入=真,
WindowsStyle=ProcessWindowsStyle.Hidden,
UseShellExecute=false,
RedirectStandardError=true,
重定向标准输出=真,
参数=args,
}
};
proc.Start();
过程标准输入写入线(args);
proc.StandardInput.Flush();
proc.StandardInput.Close();
返回true;
}
}
}
如果您能提供一个,那就太棒了,这样我们就可以在代码中看到您是如何调用该方法的(以及使用什么参数)。有了这一行:FileName=exe
,exe
是否包含要启动的文件的完整路径,或者仅仅是文件名?工作目录不用于执行文件。变量exe仅包含文件名。我想设置文件所在的工作目录。如果文件在您的工作中直接可用,那么您应该只能使用名称运行exe。@mjwills-我已经更新了问题,提供了完整的工作代码和示例。希望这会有所帮助。感谢您的回复。我可以按照你的第一个建议,但是我使用这段代码的地方将有复杂的exe名称、文件夹路径和参数,所以最好将所有这些分开。我已经试过你的第二个建议了,但没用。效果很好。。我把workingDirectory属性解释错了,非常感谢您的澄清。这解决了我的问题。如果您可以像Environment.CurrentDirectory=workingDir这样编辑第6行;对其他观众来说会更好。@ArkilShaikh-done。很高兴你把它弄错了。