獲取任意 VS 和 SQLServer 的 磁碟安裝目錄。 背景需求:如果磁碟電腦安裝了 VS 或者 SQLServer 則 認定這台電腦 的使用者 是一名 軟體研發人員,則讓程式 以最高許可權運行。 代碼如下:(基於註冊表讀取、exe版權信息校驗) static void Main(string[ ...
獲取任意 VS 和 SQLServer 的 磁碟安裝目錄。
背景需求:如果磁碟電腦安裝了 VS 或者 SQLServer 則 認定這台電腦 的使用者 是一名 軟體研發人員,則讓程式 以最高許可權運行。
代碼如下:(基於註冊表讀取、exe版權信息校驗)
static void Main(string[] args) { string vsPath = FindVisualStudioPath(); Console.WriteLine(vsPath); string sqlPath = FindSQLServerPath(); Console.WriteLine(sqlPath); Console.ReadKey(); } private static string FindVisualStudioPath() { RegistryKey studioKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\MICROSOFT\VISUALSTUDIO"); string studioPath = studioKey == null ? string.Empty : (studioKey.GetValue("VSPATH") ?? string.Empty).ToString(); if (File.Exists(studioPath)) { if (studioKey != null) studioKey.Close(); return studioPath; } RegistryKey devenvKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\APP PATHS\DEVENV.EXE"); string devenvPath = devenvKey == null ? string.Empty : (devenvKey.GetValue(string.Empty) ?? string.Empty).ToString(); if (devenvKey != null) devenvKey.Close(); if (File.Exists(devenvPath)) { FileVersionInfo fileVersion = FileVersionInfo.GetVersionInfo(devenvPath); string companyName = fileVersion.CompanyName ?? string.Empty; string productName = fileVersion.ProductName ?? string.Empty; if (companyName.IndexOf("Microsoft", StringComparison.InvariantCultureIgnoreCase) >= 0 && productName.IndexOf("Visual Studio", StringComparison.InvariantCultureIgnoreCase) >= 0) { studioPath = fileVersion.FileName; if (studioKey != null && File.Exists(studioPath)) { studioKey.SetValue("VSPATH", studioPath); studioKey.Flush(); studioKey.Close(); return studioPath; } } } return string.Empty; } private static string FindSQLServerPath() { RegistryKey sqlKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\MICROSOFT\MICROSOFT SQL SERVER"); string sqlPath = sqlKey == null ? string.Empty : (sqlKey.GetValue("MSSQLPATH") ?? string.Empty).ToString(); if (File.Exists(sqlPath)) { if (sqlKey != null) sqlKey.Close(); return sqlPath; } RegistryKey svcRootKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CURRENTCONTROLSET\SERVICES"); if (svcRootKey != null) { string[] svcArray = svcRootKey.GetSubKeyNames(); foreach (string svc in svcArray) { RegistryKey svcKey = svcRootKey.OpenSubKey(svc); if (svcKey == null) { continue; } string tempPath = (svcKey.GetValue("ImagePath") ?? string.Empty).ToString(); svcKey.Close(); if (string.IsNullOrEmpty(tempPath)) { continue; } int index = tempPath.IndexOf("sqlservr.exe", StringComparison.InvariantCultureIgnoreCase); if (index < 0) { continue; } tempPath = tempPath.Substring(0, index + "sqlservr.exe".Length).Trim().Trim('\'', '"').Trim(); if (File.Exists(tempPath)) { FileVersionInfo fileVersion = FileVersionInfo.GetVersionInfo(tempPath); string companyName = fileVersion.CompanyName ?? string.Empty; string productName = fileVersion.ProductName ?? string.Empty; if (companyName.IndexOf("Microsoft", StringComparison.InvariantCultureIgnoreCase) >= 0 && productName.IndexOf("Microsoft SQL Server", StringComparison.InvariantCultureIgnoreCase) >= 0) { sqlPath = fileVersion.FileName; if (File.Exists(sqlPath)) { if (sqlKey != null) { sqlKey.SetValue("MSSQLPATH", sqlPath); sqlKey.Flush(); sqlKey.Close(); } return sqlPath; } } } } } return string.Empty; }View Code
運行結果: