本文介绍了的Process.Start()可以把系统PATH考虑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找和试验与此一段时间,但我有没有运气。

I've been searching and experimenting for a while with this, but I have had no luck.

我试图做一个控制台程序自动执行某些任务我不能完全做一个BAT文件。我想从Windows SDK,在我的系统路径中的所有工具的bin文件夹叫signcode.exe,我可以从任何地方拨打signcode,但的Process.Start 被忽略路径

I am trying to make a console program to automate some tasks that I couldn't quite do with a BAT file. I want to call "signcode.exe" from the Windows SDK, the bin folder with all the tools in my system PATH, and I can call "signcode" from anywhere, but Process.Start is ignoring the path.

当前代码:

System.Diagnostics.Process sign = new System.Diagnostics.Process();
sign.StartInfo.FileName         = signCommand.Substring(0, signCommand.IndexOf(' '));  // signtool.exe
sign.StartInfo.Arguments        = signCommand.Substring(signCommand.IndexOf(' ') + 1); // /sign /a file1 file2

// sign.StartInfo.EnvironmentVariables["Path"] = Environment.GetEnvironmentVariable("PATH");  // This doesn't work either
sign.StartInfo.UseShellExecute              = false;
sign.StartInfo.RedirectStandardOutput       = true;
sign.StartInfo.RedirectStandardError        = true;

sign.Start();  // Throws Win32Exception - The system cannot find the file specified



我已经证实,StartInfo.EnvironmentVariables [ 路径]匹配我的系统路径,并包含Windows SDK文件夹。手动设置它不工作的。

I've confirmed that StartInfo.EnvironmentVariables["Path"] matches my system path, and contains the Windows SDK folder. Setting it manually doesn't work either.

我甚至试着设置为TEMPPATH的,但是这也不能工作。我不知道为什么你就可以设置这个,如果它没有任何效果。

I've even tried setting TempPath as shown on the MSDN page for EnvironmentVariables Property, but that didn't work either. I wonder why you would be able to set this if it has no effect.

如果的System.Diagnostics.Process 不能使用的路径,还有没有其他的功能,我可以用?我想看看命令的输出在我的控制台应用程序以及

If System.Diagnostics.Process cannot use the path, are there any other functions I could use? I'd like to see the output of the command in my console application as well.

下面是一些额外的调试值:

Here is some additional debug values:

Console.WriteLine("Sign Filename = '{0}'", sign.StartInfo.FileName);
Sign Filename = 'signtool.exe'

Console.WriteLine("Sign Arguments = '{0}'", sign.StartInfo.Arguments);
Sign Arguments = '/sign /f C:\Visual Studio\Projects\MGInsight\MGInsight\APPARENTINC.pfx /t http://timestamp.comodoca.com/authenticode "C:\Visual Studio\Projects\MGInsight\MGInsight\Publish\Application Files\\MGInsight_0_9_1_85\MGInsight.exe" "C:\Visual Studio\Projects\MGInsight\MGInsight\Publish\Application Files\\MGInsight_0_9_1_85\XPXScanner.dll" "C:\Visual Studio\Projects\MGInsight\MGInsight\Publish\Application Files\\MGInsight_0_9_1_85\NetworkCalculations.dll"'

Console.WriteLine("Sign Path = '{0}'", sign.StartInfo.EnvironmentVariables["Path"]);
Sign Path = 'C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;"C:\Program Files\Intel\WiFi\bin\";"C:\Program Files\Common Files\Intel\WirelessCommon\";"C:\Program Files (x86)\cwRsync\bin";"C:\Program Files (x86)\Git\cmd";"C:\Program Files (x86)\Git\bin";"C:\Program Files (x86)\Zend\ZendServer\bin";"C:\Program Files (x86)\Zend\ZendServer\share\ZendFramework\bin";"C:\Program Files\Java\jre6\bin";"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\";"C:\Program Files\Microsoft Windows Performance Toolkit\";C:\MinGW\bin;"C:\Program Files (x86)\Microsoft\ILMerge";"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin";C:\Program Files (x86)\Nmap'

路径C:\Program文件(x86)\Microsoft SDKs\Windows\v7.0A\Bin是signtool.exe是的,我可以从命令提示符只需键入 signtool 运行它,但如果我从同一提示符下运行该应用程序,它不注册该路径。

The path "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin" is where signtool.exe is, and I can run it from a command prompt by simply typing signtool, but if I run this application from the same prompt, it doesn't register that path.

推荐答案

我敢肯定的Process.Start不尊重PATH。

I'm pretty sure Process.Start does respect PATH.


  • 你确定你的signCommand值是正确的?

  • 是用引号指定PATH中的目录价值? 提到这样的值将不被尊重的文档。

  • Are you sure your signCommand value is correct?
  • Is the directory value in PATH specified using quotes? The docs mention that such values will not be respected.

请注意该文件名也可以是可执行文件完整路径。

Note that FileName can also be a full path to the executable.

这篇关于的Process.Start()可以把系统PATH考虑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 01:39