随笔 - 86  文章 - 0  评论 - 737  阅读 - 18万

『练手』通过注册表 获取 VS 和 SQLServer 文件路径

获取任意 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
复制代码

 

运行结果:

 

posted on   InkFx  阅读(772)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示