먼저 windows가 실행 파일을 검색하고 환경 변수 % PATH %에서 검색하는 모든 디렉토리를 찾아야합니다. 그런 다음 % PATHEXT %에서 모든 확장자 (.com, .exe, .bat 등)를 찾아야합니다.
그럼 그냥 같이 그들을 확인 :
internal class Program {
private static void Main(string[] args) {
if (args.Length != 1) {
Console.WriteLine("Incorrect usage!");
return;
}
var extensions = GetExecutableExtensions(args[0]);
var paths = GetPaths();
var exeFile = GetFirstExecutableFile(args[0], extensions, paths);
if (exeFile == null) {
Console.WriteLine("No file found!");
}
else {
Console.WriteLine(exeFile);
}
}
private static string GetFirstExecutableFile(string file, string[] extensions, string[] paths) {
foreach (var path in paths) {
var filename = Path.Combine(path, file);
if (extensions.Length == 0) {
if (File.Exists(filename)) {
return filename;
}
}
else {
foreach (var ext in extensions) {
filename = Path.Combine(path, file + ext);
if (File.Exists(filename)) {
return filename;
}
}
}
}
return null;
}
private static string[] GetExecutableExtensions(string file) {
var data = GetCmdOutput("echo %PATHEXT%");
var arr = data.TrimEnd('\n', '\r').Split(new [] {';'}, StringSplitOptions.RemoveEmptyEntries);
//If the command passed in ends with a executable extension then we dont need to test all extensions so set it to emtpy array
foreach (var ext in arr) {
if (file.EndsWith(ext, StringComparison.OrdinalIgnoreCase)) {
return new string[0];
}
}
return arr;
}
private static string[] GetPaths() {
var data = GetCmdOutput("echo %PATH%");
return data.TrimEnd('\n', '\r').Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
}
private static string GetCmdOutput(string cmd) {
using (var proc = new Process {
StartInfo = new ProcessStartInfo {
FileName = "cmd.exe",
Arguments = "/c " + cmd,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
}) {
proc.Start();
return proc.StandardOutput.ReadToEnd();
}
}
}
은 왜 플래그를 사용합니까? 또는 메뉴 항목? 또는 프로그램이 설치되어있는 곳을 지정하기위한 환경 변수 또는 바이너리가 PATH 변수에 있는지 확인하고 디렉토리를 지정하지 않고 exec를 사용하십시오 (which which는 무엇을합니다). –
질문 2를 닫으면, 이것은 프로그래밍과 관련이없는 방법을 이해합니다. –
은'Graphviz2.38' 부분이 수정 되었습니까? 그렇다면 Enviorment.GetFolderPath (Environment.SpecialFolder.ProgramFilesX86); –