『练手』通过注册表 获取 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) >= && productName.IndexOf("Visual Studio", StringComparison.InvariantCultureIgnoreCase) >= )
{
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 < ) { continue; } tempPath = tempPath.Substring(, 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) >= && productName.IndexOf("Microsoft SQL Server", StringComparison.InvariantCultureIgnoreCase) >= )
{
sqlPath = fileVersion.FileName;
if (File.Exists(sqlPath))
{
if (sqlKey != null) { sqlKey.SetValue("MSSQLPATH", sqlPath); sqlKey.Flush(); sqlKey.Close(); }
return sqlPath;
}
}
}
}
} return string.Empty;
}
运行结果:

『练手』通过注册表 获取 VS 和 SQLServer 文件路径的更多相关文章
- 『练手』005 Laura.SqlForever历史遗留 的 架构思想缺陷
005 Laura.SqlForever历史遗留 的 架构思想缺陷 我们 比较一下 Laura.WinFramework 和 Laura.XtraFramework 的差异: Laura.WinFra ...
- 『练手』003 Laura.SqlForever如何扩展 兼容更多数据库引擎
003 Laura.SqlForever如何扩展 兼容更多数据库引擎 数据库引擎插件 在 界面上的体现 导航窗体 的 工具栏 中的 引擎下拉列表 导航窗体 的 树形控件 中的 引擎主节 ...
- 『练手』手写一个独立Json算法 JsonHelper
背景: > 一直使用 Newtonsoft.Json.dll 也算挺稳定的. > 但这个框架也挺闹心的: > 1.影响编译失败:https://www.cnblogs.com/zih ...
- 『练手』001 Laura.SqlForever架构基础(Laura.XtraFramework 的变迁)
001 Laura.SqlForever架构的基础(Laura.XtraFramework 的变迁之路) Laura.XtraFramework 到底是 做什么的? Laura.XtraFramewo ...
- 『练手』004 Laura.SqlForever如何扩展 导航栏 工具栏 右键菜单 插件
004 Laura.SqlForever如何扩展 导航栏 工具栏 右键菜单 插件 导航栏 插件扩展 比如下图的 窗口 > 关闭所有文档 这个导航栏: 在 任何程序集,任何命名空间,任 ...
- C# 系统应用之通过注册表获取USB使用记录(一)
该文章是“个人电脑历史记录清除软件”项目的系统应用系列文章.前面已经讲述了如何清除IE浏览器的历史记录.获取Windows最近访问文件记录.清除回收站等功能.现在我需要完成的是删除USB设备上的U盘. ...
- 练手爬虫用urllib模块获取
练手爬虫用urllib模块获取 有个人看一段python2的代码有很多错误 import re import urllib def getHtml(url): page = urllib.urlope ...
- delphi 获取 TreeView选中的文件路径
//获取 TreeView选中的文件路径 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, G ...
- C# 根据注册表获取当前用户的常用目录整理
1.使用C#获取当前程序或解决方案的路径 2.使用C#获取当前登录用户的相关目录 3.也可以获取当前系统通用目录 4.获取Windows系统的目录,从注册表中获取. 一.当前用户的目录,HKEY_Cu ...
随机推荐
- Jupyter notebook Tensorflow GPU Memory 释放
Jupyter notebook 每次运行完tensorflow的程序,占着显存不释放.而又因为tensorflow是默认申请可使用的全部显存,就会使得后续程序难以运行.暂时还没有找到在jupyter ...
- JustMock .NET单元测试利器(一)
1.什么是Mock? Mock一词是指模仿或者效仿,用于创建实例和静态模拟.安排和验证行为.在软件开发中提及"mock",通常理解为模拟对象.模拟对象的概念就是我们想要创建一个可以 ...
- Spring中的IOC和AOP是什么含义,他们在项目中起到什么作用,并举例说明?
IOC:控制反转,是一种设计模式.一层哈尼是控制权的转移:由传统的在程序中控制并依赖转移到容器赖控制:第二是依赖注入:将相互以来的对象分离,在Spring配置文件中描述他们的依赖关系.他们的依赖关系只 ...
- [POI2007]ATR-Tourist Attractions [TPLY]
[POI2007]ATR-Tourist Attractions 题目链接(https://www.luogu.org/problemnew/show/P3451) 这种稠密图还是建议你不要跑spfa ...
- [Luogu3377]【模板】左偏树(可并堆)
题面戳我 题目描述 如题,一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 x y 将第x个数和第y个数所在的小根堆合并(若第x或第y个数已经被删除或第x和第y个数 ...
- [BZOJ2733] [HNOI2012] 永无乡 (splay启发式合并)
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
- golang 详解defer
什么是defer defer用来声明一个延迟函数,把这个函数放入到一个调用链表上, 当外部的包含方法return之前,返回参数到调用方法之前调用,也可以说是运行到最外层方法体的"}" ...
- Eclipse常用不常用快捷键
逼格高且常用的7个快捷键:Ctrl+O:列出该类下的所有方法 Ctrl+E:列出打开的所有类 Shift+Enter:在当前行下一行创建空白行(Ctrl+Shift+Enter:在当前行上一行 ...
- Hello English Again
Currently, I just want to write something in English.Maybe I just want to review my Egnlish knowledg ...
- java接口----继承(实现)方法
文中"实现"一词特指接口的继承. 一个类实现多个接口时,不能出现同名的默认方法. 一个类既要实现接口又要继承抽象类,先继承后实现. 一个抽象类可以继承多个接口(implements ...