C#读取设置path环境变量并重启计算机[转载]
来源:互联网 发布:华为用户数据分区破坏 编辑:程序博客网 时间:2024/06/05 10:23
有时候需要修改path系统环境变量, 不多说,代码如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Microsoft.Win32;//注册表操作要引用的空间using System.Runtime.InteropServices;//调用API函数需要的引用,来加载非托管类user32.dllnamespace 用程序修改环境变量{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } /**//// /// 读取注册表 /// path的路径:[HKEY_LOCAL_MACHINE/SYSTEM/ControlSet001/Control/Session Manager/Environment] /// /// /// private void tbnRead_Click(object sender, EventArgs e) { RegistryKey regLocalMachine = Registry.LocalMachine; RegistryKey regSYSTEM = regLocalMachine.OpenSubKey("SYSTEM", true);//打开HKEY_LOCAL_MACHINE下的SYSTEM RegistryKey regControlSet001 = regSYSTEM.OpenSubKey("ControlSet001", true);//打开ControlSet001 RegistryKey regControl = regControlSet001.OpenSubKey("Control", true);//打开Control RegistryKey regManager = regControl.OpenSubKey("Session Manager", true);//打开Control RegistryKey regEnvironment = regManager.OpenSubKey("Environment", true);//打开MSSQLServer下的MSSQLServer this.richTextBox1.Text = regEnvironment.GetValue("path").ToString();//读取path的值 } private void btnClose_Click(object sender, EventArgs e) { Close(); } /**//// /// /// /// /// [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]//SendMessageTimeout是在user32.dll中定义的 public static extern IntPtr SendMessageTimeout( IntPtr windowHandle, uint Msg, IntPtr wParam, IntPtr lParam, SendMessageTimeoutFlags flags, uint timeout, out IntPtr result ); [Flags] public enum SendMessageTimeoutFlags : uint { SMTO_NORMAL = 0x0000, SMTO_BLOCK = 0x0001, SMTO_ABORTIFHUNG = 0x0002, SMTO_NOTIMEOUTIFNOTHUNG = 0x0008 } private void btnWrite_Click(object sender, EventArgs e) { RegistryKey regLocalMachine = Registry.LocalMachine; RegistryKey regSYSTEM = regLocalMachine.OpenSubKey("SYSTEM", true);//打开HKEY_LOCAL_MACHINE下的SYSTEM RegistryKey regControlSet001 = regSYSTEM.OpenSubKey("ControlSet001", true);//打开ControlSet001 RegistryKey regControl = regControlSet001.OpenSubKey("Control", true);//打开Control RegistryKey regManager = regControl.OpenSubKey("Session Manager", true);//打开Control RegistryKey regEnvironment = regManager.OpenSubKey("Environment", true);//打开MSSQLServer下的MSSQLServer regEnvironment.SetValue("path", this.richTextBox1.Text);//读取path的值 MessageBox.Show("修改成功"); //下面利用发送系统消息,就不要重新启动计算机了 const int HWND_BROADCAST=0xffff; // DWORD dwMsgResult = 0L; const uint WM_SETTINGCHANGE = 0; const long SMTO_ABORTIFHUNG = 0x2; System.UInt32 dwMsgResult1=0; long s; // SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (string)"Environment", SMTO_ABORTIFHUNG, 5000,(long)s); } /**//// /// 重新启动计算机 /// /// /// /// [DllImport("user32.dll")] //主要API是这个,注意:必须声明为static extern private static extern int ExitWindowsEx(int x, int y); private void button1_Click(object sender, EventArgs e) { ExitWindowsEx(2,0); } }}
- C#读取设置path环境变量并重启计算机[转载]
- C#读取设置path环境变量并重启计算机
- linux PATH环境变量设置与读取
- 设置 PATH 环境变量
- Linux设置环境变量PATH
- 设置PATH环境变量
- 设置path环境变量
- 环境变量PATH的设置
- Ubuntu环境变量PATH设置
- Ubuntu环境变量PATH设置
- ubuntu环境变量PATH设置
- Ubuntu环境变量PATH设置
- Linux 环境变量PATH设置
- Linux 环境变量PATH设置
- linux 设置环境变量path
- 如何设置环境变量Path
- 设置添加环境变量Path
- C# 环境变量path
- WinEdt中添加和删除块注释的快捷键
- Oracle 手动实现自动增长列(Idnetity)
- Oracle 9i初始化参数文件详解
- 对象的序列化和反序列化
- ACCESS SQL连接代码
- C#读取设置path环境变量并重启计算机[转载]
- Shutting Down a Database
- linux 安装git--fedora
- Form开发中组件控制的几个常用方法
- 支持全客户端模式-dotNetFlexGrid v1.1稳定版本发布(可以快速的进行单据类开发哦)
- VC++孙鑫视频笔记(二)
- flash与声音:波频
- 2010七夕快乐
- 《转》保存Session方式