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

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Cisco Talos Blog
Cisco Talos Blog
T
Threat Research - Cisco Blogs
P
Privacy International News Feed
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
Scott Helme
Scott Helme
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
O
OpenAI News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
PCI Perspectives
PCI Perspectives
GbyAI
GbyAI
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
T
Troy Hunt's Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
腾讯CDC
C
Check Point Blog
Spread Privacy
Spread Privacy
L
LINUX DO - 最新话题
Recent Announcements
Recent Announcements
大猫的无限游戏
大猫的无限游戏
P
Palo Alto Networks Blog
Hacker News: Ask HN
Hacker News: Ask HN
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Hacker News
The Hacker News
H
Hacker News: Front Page
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
T
Tor Project blog
Martin Fowler
Martin Fowler
博客园 - 叶小钗
罗磊的独立博客
C
Cyber Attacks, Cyber Crime and Cyber Security
H
Heimdal Security Blog
V
Vulnerabilities – Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
Latest news
Latest news
WordPress大学
WordPress大学
G
Google Developers Blog
N
Netflix TechBlog - Medium
S
Security Affairs
S
Secure Thoughts
Know Your Adversary
Know Your Adversary

博客园 - 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. 在测试之前最好先备份下注册表。