EnvironmentVariableEditor:用C#编辑系统环境变量

下载地址: 【北方网通】    【电信网通】

【下载说明】

1 点击上面的地址,打开下载页面

2 点击"普通下载"--等待30秒--点击"下载"按钮--保存

运行截图:


主要源程序:
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2012/10/31
 * Time: 11:29
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

using System.Collections;
using System.IO;

namespace EnvironmentVariableEditor
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void ShowException(string msg){
			MessageBox.Show(msg,"EXCEPTION",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
		}
		
		void ShowInformation(string msg){
			this.textBox1.Text += msg + Environment.NewLine;
			this.textBox1.SelectionStart = this.textBox1.TextLength;
			this.textBox1.ScrollToCaret();
		}
		
		void MainFormLoad(object sender, EventArgs e)
		{
			this.checkedListBox1.Items.Clear();
			this.checkedListBox2.Items.Clear();
			this.textBox1.Text = "";
			
			BtnScanClick(sender,e);
		}
		
		void BtnScanClick(object sender, EventArgs e)
		{
			try{
				this.checkedListBox1.Items.Clear();
				this.checkedListBox2.Items.Clear();
				// read the environment variable for the user
				foreach(DictionaryEntry de in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User)){
					string fmt = "[" + de.Key.ToString() + "]" + de.Value.ToString();
					this.checkedListBox1.Items.Add(fmt);
					ShowInformation("Find [" + de.Key.ToString() + "] in User.");
				}
				// read the environment variable for the machine
				foreach(DictionaryEntry de in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine)){
					string fmt = "[" + de.Key.ToString() + "]" + de.Value.ToString();
					this.checkedListBox2.Items.Add(fmt);
					ShowInformation("Find [" + de.Key.ToString() + "] in Machine.");
				}
			}catch(Exception ex){
				ShowException(ex.Message);
			}
		}
		
		void BtnExportClick(object sender, EventArgs e)
		{
			try{
				SaveFileDialog sfd = new SaveFileDialog();
				sfd.Filter = "EVE File(*.eve)|*.eve";
				if(sfd.ShowDialog() == DialogResult.OK){
					if(this.checkedListBox1.SelectedItems.Count > 0){
						using(StreamWriter sw = new StreamWriter(sfd.FileName,false)){
							foreach(object o in this.checkedListBox1.CheckedItems){
								string line = "User\t";
								string fmt = o.ToString();
								string key = fmt.Substring(fmt.IndexOf('[') + 1,fmt.IndexOf(']') - fmt.IndexOf('[') - 1);
								string val = fmt.Substring(fmt.IndexOf(']') + 1);
								line += key + "\t" + val;
								sw.WriteLine(line);
								ShowInformation("Write [User][" + key + "] to " + sfd.FileName + ".");
							}
						}
					}
					
					if(this.checkedListBox2.SelectedItems.Count > 0){
						using(StreamWriter sw = new StreamWriter(sfd.FileName,true)){
							foreach(object o in this.checkedListBox2.CheckedItems){
								string line = "Machine\t";
								string fmt = o.ToString();
								string key = fmt.Substring(fmt.IndexOf('[') + 1,fmt.IndexOf(']') - fmt.IndexOf('[') - 1);
								string val = fmt.Substring(fmt.IndexOf(']') + 1);
								line += key + "\t" + val;
								sw.WriteLine(line);
								ShowInformation("Write [Machine][" + key + "] to " + sfd.FileName + ".");
							}
						}
					}
				}
			}catch(Exception ex){
				ShowException(ex.Message);
			}
		}
		
		void BtnImportClick(object sender, EventArgs e)
		{
			try{
				OpenFileDialog ofd = new OpenFileDialog();
				ofd.Filter = "EVE File(*.eve)|*.eve";
				if(ofd.ShowDialog() == DialogResult.OK){
					// read the environment variable editor file type
					using(StreamReader sr = new StreamReader(ofd.FileName)){
						while(!sr.EndOfStream){
							string line = sr.ReadLine();
							string[] parts = line.Split('\t');
							if(parts.Length == 3){
								if(parts[0].Equals("User")){
									Environment.SetEnvironmentVariable(parts[1], parts[2], EnvironmentVariableTarget.User);
									ShowInformation("Set User Variable [" + parts[1] + "] to " + parts[2] + ".");
								}
								
								if(parts[0].Equals("Machine")){
									Environment.SetEnvironmentVariable(parts[1], parts[2], EnvironmentVariableTarget.Machine);
									ShowInformation("Set Machine Variable [" + parts[1] + "] to " + parts[2] + ".");
								}
							}
						}
					}
				}
			}catch(Exception ex){
				ShowException(ex.Message);
			}
		}
		
		void BtnEditClick(object sender, EventArgs e)
		{
			try{
				if(this.checkedListBox1.CheckedItems.Count > 0){
					foreach(object o in this.checkedListBox1.CheckedItems){
						string fmt = o.ToString();
						string key = fmt.Substring(fmt.IndexOf('[') + 1,fmt.IndexOf(']') - fmt.IndexOf('[') - 1);
						string val = fmt.Substring(fmt.IndexOf(']') + 1);
						EnvironmentVariableEditor.EditorForm ef = new EditorForm("User", key, val);
						if( ef.ShowDialog() == DialogResult.OK){
							if(ef.key != string.Empty && ef.val != string.Empty && ef.target != string.Empty){
								if(ef.target.Equals("User")){
									Environment.SetEnvironmentVariable(ef.key, ef.val, EnvironmentVariableTarget.User);
									ShowInformation("Set User Variable [" + ef.key + "] to " + ef.val + ".");
								}
								
								if(ef.target.Equals("Machine")){
									Environment.SetEnvironmentVariable(ef.key, ef.val, EnvironmentVariableTarget.Machine);
									ShowInformation("Set Machine Variable [" + ef.key + "] to " + ef.val + ".");
								}
							}
						}
					}
				}
				
				if(this.checkedListBox2.CheckedItems.Count > 0){
					foreach(object o in this.checkedListBox2.CheckedItems){
						string fmt = o.ToString();
						string key = fmt.Substring(fmt.IndexOf('[') + 1,fmt.IndexOf(']') - fmt.IndexOf('[') - 1);
						string val = fmt.Substring(fmt.IndexOf(']') + 1);
						EnvironmentVariableEditor.EditorForm ef = new EditorForm("Machine", key, val);
						if( ef.ShowDialog() == DialogResult.OK){
							if(ef.key != string.Empty && ef.val != string.Empty && ef.target != string.Empty){
								if(ef.target.Equals("User")){
									Environment.SetEnvironmentVariable(ef.key, ef.val, EnvironmentVariableTarget.User);
									ShowInformation("Set User Variable [" + ef.key + "] to " + ef.val + ".");
								}
								
								if(ef.target.Equals("Machine")){
									Environment.SetEnvironmentVariable(ef.key, ef.val, EnvironmentVariableTarget.Machine);
									ShowInformation("Set Machine Variable [" + ef.key + "] to " + ef.val + ".");
								}
							}
						}
					}
				}
			}catch(Exception ex){
				ShowException(ex.Message);
			}
		}
		
		void BtnNewClick(object sender, EventArgs e)
		{
			try{
				EnvironmentVariableEditor.EditorForm ef = new EditorForm("New","Key","");
				if(ef.ShowDialog() == DialogResult.OK){
					if(ef.target != string.Empty && ef.key != string.Empty && ef.val != string.Empty){
						if(ef.target.Equals("User")){
							Environment.SetEnvironmentVariable(ef.key, ef.val, EnvironmentVariableTarget.User);
							ShowInformation("Set User Variable [" + ef.key + "] to " + ef.val + ".");
						}
						
						if(ef.target.Equals("Machine")){
							Environment.SetEnvironmentVariable(ef.key, ef.val, EnvironmentVariableTarget.Machine);
							ShowInformation("Set Machine Variable [" + ef.key + "] to " + ef.val + ".");
						}
					}
				}
			}catch(Exception ex){
				ShowException(ex.Message);
			}
		}
		
		void BtnDeleteClick(object sender, EventArgs e)
		{
			try{
				foreach(object o in this.checkedListBox1.CheckedItems){
					string fmt = o.ToString();
					string key = fmt.Substring(fmt.IndexOf('[') + 1,fmt.IndexOf(']') - fmt.IndexOf('[') - 1);
					
					Environment.SetEnvironmentVariable(key, string.Empty, EnvironmentVariableTarget.User);
					ShowInformation("Delete User Variable [" + key + " ].");
				}
				
				foreach(object o in this.checkedListBox2.SelectedItems){
					string fmt = o.ToString();
					string key = fmt.Substring(fmt.IndexOf('[') + 1,fmt.IndexOf(']') - fmt.IndexOf('[') - 1);
					
					Environment.SetEnvironmentVariable(key, string.Empty, EnvironmentVariableTarget.Machine);
					ShowInformation("Delete Machine Variable [" + key + " ].");
				}
			}catch(Exception ex){
				ShowException(ex.Message);
			}
		}
	}
}
【更多阅读】
  1. [译]C# DirectShow编程手册及实例
  2. [原]Html2Pdf:C调用wkhtmltopdf的API来将Html转换为pdf文件
  3. [原]C#:SevenZipSharp使用7z.dll来进行压缩与解压缩
  4. [原]Android NDK开发环境的搭建,无需Cygwin
  5. [原]C#用firefox3.6下载yunfile的文件
  6. [译]C#将Enum枚举映射到文本字符串
  7. [译]TIOBE 2012年10月份编程语言排行榜
  8. [原]C#实现将文本转换为图片
  9. [转]开启“以后自动采用相同的动作处理此类文件”命令
  10. [原]Freeplot: MATLAB来帮你手动画图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值