Process.Start()和PATH环境变量 [英] Process.Start() and PATH environment variable

查看:162
本文介绍了Process.Start()和PATH环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下普通的C#应用​​程序,仅尝试启动"jconsole.exe",该文件在我的计算机上位于C:\ Programs \ jdk16 \ bin.

I have the following trivial C# application that simply attempts to launch "jconsole.exe", which on my machine is located in C:\Programs\jdk16\bin.

using System;
using System.Diagnostics;

namespace dnet {
  public class dnet {
    static void Main( string[] args ) {
      try {
        Process.Start("jconsole.exe");
        Console.WriteLine("Success!");
      } catch (Exception e) {
        Console.WriteLine("{0} Exception caught.", e);
      }
    }
  }
}

如果我的PATH环境变量设置为

If my PATH environment variable is set to

c:\windows;c:\windows\sytem32;c:\programs\jdk16\bin

它完美地工作.但是,如果PATH环境变量设置为

it works perfectly. However, if the PATH environment variable is set to

c:\windows;c:\windows\sytem32;c:\\programs\jdk16\bin

(请注意"c:"和"programs"之间的两个反斜杠),它会失败并出现win32异常.

(note the two backslashes between "c:" and "programs"), it fails with a win32 exception.

System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at dnet.dnet.Main(String[] args)

有趣的是,在运行.NET程序并获得异常的同一命令提示符下,我只需键入"jconsole.exe",程序便会启动. Windows在PATH中找到带有双反斜杠的可执行文件似乎没有问题,但是Process.Start()确实可以.

Interestingly, in the same command prompt where I run the .NET program and get the exception, I can simply type "jconsole.exe", and the program will start. Windows appears to have no trouble finding the executable with the double backslash in the PATH, but Process.Start() does.

为什么PATH中多余的反斜杠会引起问题,如何解决该问题?我不知道我要调用的可执行文件在运行时位于何处,所以我宁愿依赖PATH变量.

Why is the extra backslash in the PATH causing problems, and how I can get around the problem? I don't know where the executable I want to call will be located at runtime, so I'd rather rely on the PATH variable.

推荐答案

不太确定为什么会出现此问题.不过,我可以想到一种适用于我的机器的解决方案:

Not quite sure why the problem occurs. Though, I can think of one solution that works on my machine:

var enviromentPath = System.Environment.GetEnvironmentVariable("PATH");

Console.WriteLine(enviromentPath);
var paths = enviromentPath.Split(';');
var exePath = paths.Select(x => Path.Combine(x, "mongo.exe"))
                   .Where(x => File.Exists(x))
                   .FirstOrDefault();

Console.WriteLine(exePath);

if (string.IsNullOrWhiteSpace(exePath) == false)
{
    Process.Start(exePath);
}

我确实找到了一个段落,它给了我解决方案的想法.从 Process.Start文档

I did find one para which gave me the idea for this solution. From the documentation for Process.Start

如果您的系统中使用引号声明了路径变量,则您 在启动该过程中发现的任何过程时,必须完全限定该路径 地点.否则,系统将找不到路径.例如, 如果c:\ mypath不在您的路径中,并且您使用引号将其添加 标记:path =%path%;"c:\ mypath",您必须完全限定任何过程 启动时在c:\ mypath中.

If you have a path variable declared in your system using quotes, you must fully qualify that path when starting any process found in that location. Otherwise, the system will not find the path. For example, if c:\mypath is not in your path, and you add it using quotation marks: path = %path%;"c:\mypath", you must fully qualify any process in c:\mypath when starting it.

即使PATH变量包含Windows能够使用的有效路径,但Process.Start无法使用它并需要完全限定的路径.

The way I read it, even though the PATH variable contained a valid path that Windows is able to use, Process.Start is unable to use it and needs the fully qualified path .

这篇关于Process.Start()和PATH环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆