请教高人在程序中修改系统path变量,要求修改后不重启就生效

apollonain 2010-10-03 08:29:44
如下代码:
string tmp="c:\\wwww\\ddd\\ass";
System.Collections.IDictionary dict = Environment.GetEnvironmentVariables();
string str = dict["Path"].ToString();
MessageBox.Show(str,"原始值");
str = str + ";"+tmp ;
MessageBox.Show(str,"期望值");
System.Environment.SetEnvironmentVariable("Path", str);
str = dict["Path"].ToString();
MessageBox.Show(str,"实际值");


第一次运行dict["Path"].ToString();的值和第2次运行的都是一样的,希望能直接生效,不需要重启。

如果能付部分代码更好,谢谢。
...全文
168 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
apollonain 2010-10-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
string s= System.Environment.GetEnvironmentVariable("path");
MessageBox.Show(s);
System.Environment.SetEnvironmentVariable("path", "c:\\");
System.Environment.SetEnvironmentVariable("path", "");
……
[/Quote]

正解,想请教下System.Environment.GetEnvironmentVariable("path");

System.Collections.IDictionary dict = Environment.GetEnvironmentVariables();
string str = dict["Path"].ToString();
有什么不同?为什么出现的值不是一样的?
ZengHD 2010-10-03
  • 打赏
  • 举报
回复
[Quote=引用楼主 apollonain 的回复:]
如下代码:
C# code
string tmp="c:\\wwww\\ddd\\ass";
System.Collections.IDictionary dict = Environment.GetEnvironmentVariables();
string str = dict["Path"].ToString();
MessageBox.Show(str,"原始值");
str = str + ";"+tmp ;
MessageBox.Show(str,"期望值");
System.Environment.SetEnvironmentVariable("Path", str);
dict = Environment.GetEnvironmentVariables();
str = dict["Path"].ToString();
MessageBox.Show(str,"实际值");

[/Quote]
兔子-顾问 2010-10-03
  • 打赏
  • 举报
回复
http://blog.sina.com.cn/s/blog_48a45b950100gmuf.html
明白原理了。搜索一下如何操作注册表,以及如何用mainfest文件提升程序的权限。你问题就解决了。
先自己尝试搜索我告诉你的思路,不懂再说。
wuyq11 2010-10-03
  • 打赏
  • 举报
回复
string s= System.Environment.GetEnvironmentVariable("path");
MessageBox.Show(s);
System.Environment.SetEnvironmentVariable("path", "c:\\");
System.Environment.SetEnvironmentVariable("path", "");

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧