2012-04-15 3 views
0

다음 코드 [C#]를 사용하여 googlechrome이 설치된 경로를 알 수 있습니다. 그러나이 코드는 먼저 chrome.exe를 시작한 다음 경로를 사용합니다. 내 질문에 chrome.exe를 시작하지 않고 어떻게 경로를 알 수 있습니까?C# windows 서비스 위치

 ProcessStartInfo startInfo = new ProcessStartInfo(); 
     startInfo.FileName = "chrome.exe"; 
     string argmnt = @"-g"; 
     startInfo.Arguments = argmnt; 

     String path = null; 
     try 
     { 
      Process p = Process.Start(startInfo); 
      path = p.MainModule.FileName; 
      p.Kill(); 
     } 
     catch (Exception e) 
     { 
      return String.Empty; 
     } 
+0

이 문제를 해결 했습니까? – harpo

답변

0

PATH 환경 변수를 구문 분석하고 해당 exe를 찾는 모든 디렉토리를 열거합니다. 이것은 본질적으로 p.Start 메소드를 호출 할 때 OS가 수행하는 작업입니다.

+0

무엇입니까? Windows 레지스트리에서 검색해야합니까? 또는 다른 곳? 분명히 해줄 수 있니? – user743246

0

먼저 "프로세스 모니터"라는 도구를 Microsoft에서 다운로드하십시오 : ProcessMonitor available from Microsoft SysInternals 다음 Chrome을 시작하여 위치를 찾습니다.

팁 : "프로세스 모니터"는 하드 디스크 드라이브, 레지스트리 및 스레드/프로세스 정보를 실시간으로 모니터링하고 캡처 한 흔적을 저장할 수 있도록합니다.

enter image description here

. Process Monitor를 열면 정보 추적이 시작됩니다.

b. 돋보기 도구 모음 단추 (켜기/끄기 추적 단추)를 클릭하여 추적을 중지하십시오.

c. 그런 다음 추적 지우기 단추를 클릭하여 추적을 지 웁니다.

d. Chrome을 시작하고 Chrome.exe를 열면 Chrome.exe를 검색하면 파일 경로가 표시됩니다.

0

chrome.exe과 함께 WHERE 명령을 인수로 사용하십시오. 그러면 쉘이로드 할 실행 파일의 경로를 알려줍니다.

명령의 출력을 다시 읽으면됩니다.

현재 버전과 마찬가지로 실행 파일이 시스템 PATH에 있다고 가정합니다.

다음은 사용자 요구에 맞출 수있는 몇 가지 코드입니다. 그것은 본질적으로 WHERE 명령을 래핑합니다 (도중에 실행 파일이므로 WHERE WHERE은 경로를 표시합니다).

using System; 
using System.Diagnostics; 

public sealed class WhereWrapper 
{ 
    private static string _exePath = null; 

    public static int Main(string[] args) { 

     int exitCode; 
     string exeToFind = args.Length > 0 ? args[0] : "WHERE"; 

     Process whereCommand = new Process(); 

     whereCommand.OutputDataReceived += Where_OutputDataReceived; 

     whereCommand.StartInfo.FileName = "WHERE"; 
     whereCommand.StartInfo.Arguments = exeToFind; 
     whereCommand.StartInfo.UseShellExecute = false; 
     whereCommand.StartInfo.CreateNoWindow = true; 
     whereCommand.StartInfo.RedirectStandardOutput = true; 
     whereCommand.StartInfo.RedirectStandardError = true; 

     try { 
      whereCommand.Start(); 
      whereCommand.BeginOutputReadLine(); 
      whereCommand.BeginErrorReadLine(); 
      whereCommand.WaitForExit(); 
      exitCode = whereCommand.ExitCode; 

     } catch (Exception ex) { 
      exitCode = 1; 
      Console.WriteLine(ex.Message); 

     } finally { 
      whereCommand.Close(); 
     } 

     Console.WriteLine("The path to {0} is {1}", exeToFind, _exePath ?? "{not found}"); 

     return exitCode; 
    } 

    private static void Where_OutputDataReceived(object sender, DataReceivedEventArgs args) { 

     if (args.Data != null) { 
      _exePath = args.Data; 
     } 
    } 
}