private static bool TryGetPath(string installRegisterPath, string serviceRegisterPath, string windowsServiceProcessName, string processName, string relativePath, out string detectedPath, out int step) { //0.根据安装的注册表获取路径 try { if (CheckInstall(installRegisterPath)) { detectedPath = RegistUtils.GetStringValue(installRegisterPath, InstallDirOption); step = 1; return(true); } } catch (Exception) { } //1.根据服务名获取服务安装路径 try { var serviceExeFileName = RegistUtils.GetStringValue(serviceRegisterPath, ImagePathOption).Replace( "\"", ""); var fileInfo = new FileInfo(serviceExeFileName); if (fileInfo.Exists) { //获取到的是 //E:\E10\Server\Control\Digiwin.Mars.ServerStart.WindowsService.exe detectedPath = fileInfo.Directory.FullName; step = 2; return(true); } } catch (Exception) { } //2.根据 Windows Service 进程获取 try { var ps = Process.GetProcessesByName(windowsServiceProcessName); if (ps.Length != 0) { var fileInfo = new FileInfo(ps[0].MainModule.FileName); if (fileInfo.Exists) { detectedPath = fileInfo.Directory.FullName; step = 3; return(true); } } } catch (Exception) { } try { //3.根据启动的进程获取 var process = Process.GetProcessesByName(processName); if (process.Length != 0) { var fileInfo = new FileInfo(process[0].MainModule.FileName); if (fileInfo.Exists) { detectedPath = fileInfo.Directory.FullName; step = 4; return(true); } } } catch (Exception) { } try { detectedPath = Path.Combine(BaseDirectoryInfo.Parent.Parent.FullName, relativePath); //4.根据当前安装目录的平行目录获取 if (Directory.Exists(detectedPath)) { step = 5; return(true); } } catch (Exception) { } detectedPath = string.Empty; step = 0; return(false); }
private static bool CheckInstall(string installRegisterPath) { return(string.Equals( RegistUtils.GetStringValue(installRegisterPath, StatusOption), InstalledStatus)); }