惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
P
Proofpoint News Feed
The Hacker News
The Hacker News
Know Your Adversary
Know Your Adversary
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tenable Blog
AWS News Blog
AWS News Blog
S
Securelist
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
IT之家
IT之家
腾讯CDC
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
C
Check Point Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Engineering at Meta
Engineering at Meta
Latest news
Latest news
A
About on SuperTechFans
The Register - Security
The Register - Security
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
C
Cisco Blogs
T
Tailwind CSS Blog
Simon Willison's Weblog
Simon Willison's Weblog
阮一峰的网络日志
阮一峰的网络日志
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
T
Tor Project blog
L
Lohrmann on Cybersecurity
G
GRAHAM CLULEY
B
Blog RSS Feed
Scott Helme
Scott Helme
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
NISL@THU
NISL@THU
P
Privacy International News Feed
Security Latest
Security Latest
Recorded Future
Recorded Future
L
LangChain Blog
Cyberwarzone
Cyberwarzone
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
F
Fortinet All Blogs
O
OpenAI News
T
Threat Research - Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale

博客园 - cjfwu

设计模式学习4-Bridge模式 设计模式学习3-Strategy模式 设计模式学习2-Adapter模式 设计模式学习1-Facade模式 设备控制(反馈处理) 通过System.IO.Packaging实现打包和解包 设备控制之矩阵状态显示 windows shell 编程3(函数解释) windows shell 编程2(浏览文件夹) windows shell 编程1(概念) 不同命名空间下名称和结构相同的类相互序列化与反序列化 通过SvcUtil.exe生成客户端代码和配置 分组 在“添加引用”对话框中显示需要的Assembly 只运行一个实例 SVN操作 托盘操作 获得树节点的高度 枚举的操作
将目录添加环境变量
cjfwu · 2012-02-16 · via 博客园 - cjfwu

有的时候我们需要将exe所在目录添加环境变量,这样可以在cmd中直接调用此exe,方法如下:

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using Microsoft.Win32;
7 using System.Diagnostics;
8 using System.Runtime.InteropServices;
9
10 namespace AppGet {
11 class Program {
12 static void Main(string[] args) {
13 Register();
14
15 Console.WriteLine("press any key to exit.");
16 Console.ReadLine();
17 }
18
19 private static void Register() {
20 RegistryKey systemPathKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true);
21
22 if (string.IsNullOrEmpty(systemPathKey.GetValue("AppGet", string.Empty).ToString())) {
23 systemPathKey.SetValue("AppGet", Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), RegistryValueKind.String);
24 }
25
26 string path = systemPathKey.GetValue("Path", "Empty", RegistryValueOptions.DoNotExpandEnvironmentNames).ToString();
27 if (!path.ToLower().Contains(@"%appget%")) {
28 systemPathKey.SetValue("Path", @"%AppGet%;" + path, RegistryValueKind.ExpandString);
29 }
30
31 int rtnVal = 0;
32 SendMessageTimeoutA(HWND_BROADCAST, WM_SETTINGCHANGE, 0, "Environment", 2, 5000, rtnVal);
33 }
34
35 private const int HWND_BROADCAST = 0xffff;
36 private const int WM_WININICHANGE = 0x001a, WM_SETTINGCHANGE = WM_WININICHANGE, INI_INTL = 1;
37 [DllImport("user32.dll")]
38 private static extern int SendMessageTimeoutA(int hWnd, uint wMsg, uint wParam, string lParam, int fuFlags, int uTimeout, int lpdwResult);
39 }
40 }

说明:

1. 22行的AppGet为系统变量名(下同)

2. 26行GetValue必须指定RegistryValueOptions.DoNotExpandEnvironmentNames,否则在接下来的SetValue会将系统Path中的的变量全部替换掉。

3. 28行SetValue必须指定RegistryValueKind.ExpandString,否则系统会把%AppGet%当成普通字符串,而不是变量。

4. 32的SendMessageTimeoutA作用为广播上面的设置,这样就不需要注销或重启系统了。

5. 之前也尝试过用Environment.SetEnvironmentVariable和Environment.GetEnvironmentVariable,后来发现GetEnvironmentVariable方法没有DoNotExpandEnvironmentNames这样的参数,所以会将系统Path中的的变量全部替换掉!

6. 在测试之前最好先备份下注册表。