2011-11-04 126 views
0

我要的是什么样的“的”命令:提供一个文件名(EXE,蝙蝠......),并返回该文件的完整路径:路径解析器在.NET

它的java.exe
C:\ WINDOWS \ SYSTEM32 \ java.exe的

的代码将是这样的:

string fileName = "java.exe" 
string fullPath = PathResolver.Resolve(fileName); 

我们有在.NET框架这样的设施?

谢谢。

更新:
最后,我写了一个自己:

// Reoslve the full path of a file name 
    // fileName: of absolute path or relative path; with ext or without ext 
    static string ResolvePath(string fileName) 
    { 
     // 0. absolute path 
     string[] stdExts = { ".bat", ".cmd", ".pl", ".exe" }; 
     if (Path.IsPathRooted(fileName)) 
     { 
      if (File.Exists(fileName)) 
      { 
       return fileName; 
      } 
      else 
      { 
       foreach (string eachExt in stdExts) 
       { 
        string fullPath = fileName + eachExt; 
        if (File.Exists(fullPath)) 
        { 
         return fullPath; 
        } 
       } 
      } 

      return ""; 
     } 

     // 1. candidate extensions 
     string fileNameNoExt = Path.GetFileNameWithoutExtension(fileName); 
     string ext = Path.GetExtension(fileName); 
     string[] candidateExts; 
     if (string.IsNullOrEmpty(ext)) 
     { 
      candidateExts = stdExts; 
     } 
     else 
     { 
      string[] exts = { ext }; 
      candidateExts = exts; 
     } 

     // 2. candidate path: 
     //  http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx#search_order_for_desktop_applications 
     List<string> candidatePaths = new List<string>(); 

     // application dir 
     string fileApp = Process.GetCurrentProcess().MainModule.FileName; 
     candidatePaths.Add(Path.GetDirectoryName(fileApp)); 

     // current dir 
     candidatePaths.Add(Directory.GetCurrentDirectory()); 

     // system dir 
     candidatePaths.Add(Environment.SystemDirectory); 

     // windows dir 
     string winDir = Environment.GetEnvironmentVariable("windir"); 
     candidatePaths.Add(winDir); 

     // PATH 
     string[] paths = Environment.GetEnvironmentVariable("PATH").Split(';'); 
     foreach (string path in paths) 
     { 
      // strip the trailing '\' 
      candidatePaths.Add(Path.GetDirectoryName(path)); 
     } 

     // 3. resolve 
     foreach (string eachPath in candidatePaths) 
     { 
      foreach (string eachExt in candidateExts) 
      { 
       string fullPath = eachPath + "\\" + fileNameNoExt + eachExt; 
       if (File.Exists(fullPath)) 
        return fullPath; 
      } 
     } 

     return ""; 
    } 

回答

2

我不知道这是否可以帮助你:

public string PathResolver(string filename) 
    { 
     string[] paths = Environment.GetEnvironmentVariable("PATH").Split(';'); 
     foreach (string path in paths) 
     { 
      string fname = Path.Combine(path, filename); 
      if (File.Exists(filename)) return fname; 
     } 
     return ""; 
    } 
+0

是啊,谢谢。我们绝对可以自己写一个,我们可以按照DLL搜索顺序:http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx,但我很希望有一个在.NET框架第一:) –

+0

最后,我写了一个自己的建议... –

0

看看FileInfoFullName property类。

FileInfo fi = new FileInfo("foo.exe"); 
string fullanme = fi.FullName; 
+1

这不会做任何路径解析,而是返回:CurrentDir + foo.exe –

0

我可以提出一个办法,找出可执行文件的完整路径后,你实际上仅使用文件名(可能不是你想要什么)运行它:

Process p = new Process(); 
p.StartInfo = new ProcessStartInfo("notepad.exe"); 
p.Start(); 
Console.WriteLine(p.MainModule.FileName); 
+1

谢谢,是的,这不是我想要的 - 我想先得到路径,然后启动应用程序:因为如果它是一个bat/cmd文件,我需要运行它像:cmd/c fileName –

1

你应该采取一起来看看这款免费的图书馆,在NDepend的和用于其他项目,

http://filedirectorypath.codeplex.com/

+0

谢谢你,这是一个强大的库,但似乎不包括我想要的路径解析器。 –