『練手』通過註冊表 獲取 VS 和 SQLServer 文件路徑

来源:http://www.cnblogs.com/shuxiaolong/archive/2017/12/03/VS_SQL_ExePath.html
-Advertisement-
Play Games

獲取任意 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

 

運行結果:

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本文歡迎引用,轉載。 引用,轉載請標明出處! 調試完畢源碼將上傳到GitHub 為了嵌入式系統與STEP 7 PLC 通過S7 協議通訊,嘗試移植 Snap 7 到STM32F407 cpu 上。 今天基本編譯通過,尚未通電調試。 核心技術要點,及方案如下 1。測試系統構建(待續) 2。代碼修改 A ...
  • Linux嵌入式工程師技能要求: 1.C語言 具備C語言基礎、理解C語言基礎編程及高級編程,包括:數據類型、數組、指針、結構體、鏈表、文件操作、隊列、棧。 2.Linux基礎 Linux操作系統的概念、安裝方法,瞭解Linux下目錄結構、基本命令、Vim+GCC+GDB+make,shell Mak ...
  • 一、Linux開發板操作命令1.查看命令: 系統相關: **任務管理器: gnome-system-monitor **查找命令 : find 實例:find -name gpio* 在當前目錄下查找以“gpio”開始的文件 find ../ -name gpio* 在上級目錄下查找名為“gpio” ...
  • 簡單寫一個 linux查看文件夾占用空間大小--du命令的一種用法 ...
  • yum(Yellowdog Update Modifier)rpm的前端程式,可解決軟體包相關依賴性 在搭建前先開啟掛載,並設置為開機啟動 將光碟掛載到/misc/cd yum的配置文件 在/etc/yum.repos.d目錄下把系統自帶的配置文件刪除或備份 vim新建base.repo配置文件並編 ...
  • 在整型陣列中,我們需要從中獲取陣列元素的最大值和最小值: 方法一:先是使用Array進行排序,然後從排序後數組中,最一個元素為最小,最後一個元素為最大。 public static int FindMaxNumber(params int[] stringValue) { Array.Sort(st ...
  • 有一個函數,從來沒有被其他任何類用到。將這個函數設為private。 ...
  • 資料庫中有一個City表 初始時數據: 實體類與Fluent Api配置映射 public class City { public int Id { get; set; } public string Name { get; set; } public int? ParentId { get; se ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...