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

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

问题描述

我有以下简单的 C# 应用程序,它只是尝试启动jconsole.exe",它在我的机器上位于 C:Programsjdk16in.

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

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:windowssytem32;c:programsjdk16in

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

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

c:windows;c:windowssytem32;c:\programsjdk16in

(注意 "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天全站免登陆