日期:2014-05-17  浏览次数:20967 次

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(l