『练手』通过注册表 获取 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 ...
随机推荐
- ftp搭建 与http服务访问
Linux安装ftp组件 1 FTP http://jingyan.baidu.com/article/380abd0a77ae041d90192cf4.html安装vsftpd组件 安装完后,有/e ...
- IE浏览器兼容的常见问题及解决方案
常见6个问题及解决方案 1 IE6/IE7对display:inline-block的支持还欠缺 就是我们做导航栏时通常会用到<ul><li>标签去写,在现在一些主流的浏览器中 ...
- SSH框架之-hibernate 三种状态的转换
一.遇到的神奇的事情 使用jpa操作数据库,当我使用findAll()方法查处一个List的对象后,给对这个list的实体进行了一些操作,并没有调用update 或者 saveOrUpdate方法,更 ...
- Kettle根据时间戳同步数据实现
1 Kettle总体步骤 由于Kettle自身的特殊性以及在多个步骤中kettle自身处理数据库事务的特殊性,尝试了很多种方案,最终确定暂使用如下方案. 1.使用此方案可以解决kettle本身数据库事 ...
- 配置maven环境出现ARP tomcat native library 版本安装跟需求版本不一致时的解决方法An incompatible version xxxx of the APR based Apache Tomcat Native library is installed, while Tomcat requires version xxxx
此地址下载你所需要的library版本http://archive.apache.org/dist/tomcat/tomcat-connectors/native/ 点击binaries 点win32 ...
- [BZOJ4292] [PA2015] Równanie
Description 对于一个正整数n,定义f(n)为它十进制下每一位数字的平方的和.现在给定三个正整数k,a,b,请求出满足a<=n<=b且k*f(n)=n的n的个数. Input 第 ...
- kali使用Fluxion钓鱼WiFi
先介绍一下这个软件 这个软件是一个可以生成一个钓鱼WiFi的软件,可以伪装成一个正常的WiFi,但是是没有密码的,但是其他信息都是一样的,一旦开启这个攻击,正常的那个AP就无法正常连接,只能连到这个伪 ...
- lodash源码分析之数组的差集
外部世界那些破旧与贫困的样子,可以使我内心世界得到平衡. --卡尔维诺<烟云> 本文为读 lodash 源码的第十七篇,后续文章会更新到这个仓库中,欢迎 star:pocket-lodas ...
- [.Net Core] 简单使用 Mvc 内置的 Ioc
简单使用 Mvc 内置的 Ioc 本文基于 .NET Core 2.0. 鉴于网上的文章理论较多,鄙人不才,想整理一份 Hello World(Demo)版的文章. 目录 场景一:简单类的使用 场景二 ...
- FPGA加三移位算法:硬件逻辑实现二进制转BCD码
本文设计方式采用明德扬至简设计法.利用FPGA来完成显示功能不是个很理想的方式,当显示任务比较复杂,要通过各种算法显示波形或者特定图形时,当然要用单片机通过C语言完成这类流程控制复杂,又对时序要求不高 ...