ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文
【C#】PATH 環境変数エディタ ( No.1 )
日時: 2008/04/24 11:24
名前: lightbox



拡張子:
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; 

namespace PATH_EDITOR_CS
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}
		// ユーザーかシステムかを示す変数( 1:ユーザー,2:システム ) 
		private int mode = 0;

		// ********************************************************* 
		// 初期処理 
		// ********************************************************* 
		private void Form1_Load(object sender, EventArgs e)
		{

			this.LboxGrid1.AddColumn("PATH", "検索パス");
			this.LboxGrid1.ReadOnly = false;
			this.LboxGrid1.EditMode = DataGridViewEditMode.EditOnF2;

		}

		// ********************************************************* 
		// ユーザーの PATH 環境変数の表示 
		// ********************************************************* 
		private void ユーザー_Click(object sender, EventArgs e)
		{

			string subkey = "Environment";
			RegistryKey regkey = Registry.CurrentUser.OpenSubKey(subkey, true);

			try
			{
				if ((regkey != null))
				{

					mode = 1;
					DataEnum(regkey.GetValue("PATH").ToString());
				}

				else
				{

					MessageBox.Show("レジストリが正しく開かれていません");

				}
			}

			catch (Exception ex)
			{

				MessageBox.Show(ex.ToString());

			}

			if ((regkey != null))
			{

				regkey.Close();

			}

		}

		// ********************************************************* 
		// システムの PATH 環境変数の表示 
		// ********************************************************* 
		private void システム_Click(object sender, EventArgs e)
		{

			string subkey = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
			RegistryKey regkey = Registry.LocalMachine.OpenSubKey(subkey, true);

			try
			{
				if ((regkey != null))
				{

					mode = 2;
					DataEnum(regkey.GetValue("PATH").ToString());
				}

				else
				{

					MessageBox.Show("レジストリが正しく開かれていません");

				}
			}

			catch (Exception ex)
			{

				MessageBox.Show(ex.ToString());

			}

			if ((regkey != null))
			{

				regkey.Close();

			}

		}

		// ********************************************************* 
		// 文字列を ";" で分解して配列にする 
		// ※ 引数は 値渡し 
		// ********************************************************* 
		private void DataEnum(string Path)
		{

			// 区切り文字定義 
			string delimStr = ";";
			char[] delimiter = delimStr.ToCharArray();

			// 配列に分割 
			string[] split = Path.Split(delimiter);

			// ソートはしない( パスの順序に意味がある ) 
			// 表示のみならばソートしても良い 
			// Array.Sort(split, split.GetLowerBound(0), split.Length) 

			LoadGrid(ref split);

		}

		// ********************************************************* 
		// LboxGrid にデータをロード 
		// ※ 引数は 参照渡し 
		// ********************************************************* 
		private void LoadGrid(ref string[] data)
		{

			this.LboxGrid1.Clear();
			// 自動サイズ変更モード 
			this.LboxGrid1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

			foreach (string value in data)
			{

				this.LboxGrid1.AddRow();
				this.LboxGrid1.SetColumnText("PATH", value);

			}

			// 自動サイズ変更の列をユーザが変更できるようにする 
			this.LboxGrid1.AllowResizeColumn(true);

		}


		// ********************************************************* 
		// 更新 
		// ********************************************************* 
		private void 更新_Click(object sender, EventArgs e)
		{ 
	        
			string str = ""; 
			string fld = ""; 
	        
			// LboxGrid の全行を列挙して PATH 環境変数の値を作成する 
			this.LboxGrid1.SetCurrentRow(); 
			while ((this.LboxGrid1.FindNextRow())) { 
	            
				fld = this.LboxGrid1.GetColumnText("PATH"); 
				if (fld == null) { 
					continue; 
				} 
				else { 
					fld = fld.Trim(); 
					if (fld == "") { 
						continue; 
					} 
				} 
	            
				if (str != "") { 
					str += ";";
				}

				str += fld;
	            
			} 
	        
			// 作成された内容の表示 
			MessageBox.Show(str); 
	        
			// レジストリへの書き込み 
			if (mode == 1) { 
				string subkey = "Environment"; 
				RegistryKey regkey = Registry.CurrentUser.OpenSubKey(subkey, true); 
	            
				regkey.SetValue("PATH", str); 
				DataEnum(regkey.GetValue("PATH").ToString()); 
	            
				regkey.Close(); 
			} 
	        
			// レジストリへの書き込み 
			if (mode == 2) { 
				string subkey = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
				RegistryKey regkey = Registry.LocalMachine.OpenSubKey(subkey, true); 
	            
				regkey.SetValue("PATH", str); 
				DataEnum(regkey.GetValue("PATH").ToString()); 
	            
				regkey.Close(); 
			} 
	        
		}

	}
}