AutoStart.cs 6.2 KB
using System;
using Microsoft.Win32;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace ServiceManager
{
	/// <summary>
	/// AutoStart 的摘要说明。
	/// </summary>
	public class AutoStart
	{
		public AutoStart()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}


		public bool ValueExisted(RegistryKey Root,string Dir,string ValueName)
		{
			RegistryKey p_SubKey=Root;
			bool Existed = false;
			
			try
			{	
				p_SubKey=Root.OpenSubKey(Dir);
				if(p_SubKey==null)
				{
					return false;
				}

				string [] ItemNames = p_SubKey.GetValueNames();
				
				for(int i=0;i<ItemNames.Length;i++)
				{
					if(ItemNames[i] == ValueName)
					{	
						Existed = true;
						break;
					}
				
				}
				
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
				
			}
			return 	Existed;
		
		}
		//重载ValueExisted
		public bool ValueExisted(RegistryKey Root,string Dir,string ValueName,object ValueProperty)
		{
			RegistryKey p_SubKey=Root;
			bool Existed = false;
			
			try
			{	
				p_SubKey=Root.OpenSubKey(Dir);
				if(p_SubKey==null)
				{
					return false;
				}

				string [] ItemNames = p_SubKey.GetValueNames();
				
				for(int i=0;i<ItemNames.Length;i++)
				{
					if((ItemNames[i] == ValueName)&&(ValueProperty.ToString()==p_SubKey.GetValue(ValueName).ToString()))
					{	
						Existed = true;
						break;
					}
				
				}
				
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
				
			}
			return 	Existed;
		
		}

		public bool SetValueProperty(RegistryKey Root,string Dir,string ValueName,object ValueProperty)
		{	
			RegistryKey p_SubKey=Root;
			string temp=string.Empty;
			bool done = false;
			try
			{
					bool bExisted =false;
				bExisted=this.ValueExisted(Root,Dir,ValueName);
				if(bExisted)
				{
					p_SubKey = p_SubKey.OpenSubKey(Dir,true);
					p_SubKey.SetValue(ValueName,ValueProperty);
					done=true;
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}

			return done;
		}
		//返回空字串表明路径错误
		public string CreateValue(RegistryKey Root,string Dir,string ValueName,object ValueProperty)
		{
			int index =0,count = 0;
			RegistryKey p_SubKey=Root;
			string strTemp ="";
						
			try
			{	
				p_SubKey=Root.OpenSubKey(Dir,true);
				if(p_SubKey==null)
				{
					return "";
				}

				string [] ItemNames = p_SubKey.GetValueNames();
				string NameContainer ="",CharPart="",NumPart="";
				int temp=0;
				bool found =false;

				for(int i=0;i<ValueName.Length;i++)
				{
						
					if(char.IsDigit(ValueName[i]))
					{
						CharPart = ValueName.Substring(0,i);
						NumPart = ValueName.Remove(0,i);
						break;
					}
				}
				if(String.IsNullOrEmpty(CharPart))
				{
					CharPart = ValueName;
					NumPart = "";
				}
				//查找现有的名字,看是否有重名。如果有重名,则将名字后的数字递增。生成新的名字返回。
				for(int i=0;i<ItemNames.Length;i++)
				{	
					index = -1;
					NameContainer = ItemNames[i];

					index = NameContainer.IndexOf(CharPart);
					
					if(index==0)
					{
						NameContainer = NameContainer.Remove(index,CharPart.Length);
						if(String.IsNullOrEmpty(NameContainer))
						{
							count = 0;
							if(String.IsNullOrEmpty(NumPart))
							{
								count =1;
							}
						}
						else
						{
							temp = Convert.ToInt32(NameContainer);
							if(temp>=count)
							{
								count = temp+1;
							}
						}
						
						found = true;
					}
				}
				if(found == true)
				{
					ValueName = CharPart+count.ToString();
				}
				
				p_SubKey.SetValue(ValueName,ValueProperty.ToString());
				strTemp=ValueName;
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return strTemp;

				
		}

		public bool DeleteValue(RegistryKey Root,string Dir,string ValueName)
		{
			bool done = false;

			try
			{
				bool bExisted = false;
				bExisted = this.ValueExisted(Root,Dir,ValueName);
				if(bExisted)
				{
					RegistryKey p_SubKey = Root.OpenSubKey(Dir,true);
					p_SubKey.DeleteValue(ValueName,true);
					done = true;
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return done;
		}

		public object ReadValueProperty(RegistryKey Root,string Dir,string ValueName)
		{
			RegistryKey p_SubKey=Root;
			object oValue =null;
			
			try
			{
				bool bExisted = false;
				bExisted = this.ValueExisted(Root,Dir,ValueName);
				if(bExisted)
				{
					p_SubKey = p_SubKey.OpenSubKey(Dir,true);
					oValue = p_SubKey.GetValue(ValueName);
					 
				}

			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}

			return oValue;
		}

		public string [] ListAllValueName(RegistryKey Root,string Dir)
		{
			RegistryKey p_SubKey=Root;
			string [] ReturnValue=null;
			
			
			try
			{
				p_SubKey = p_SubKey.OpenSubKey(Dir,true);
				if(p_SubKey==null)
				{
					return ReturnValue;
				}
				ReturnValue = p_SubKey.GetValueNames();
					 
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}

			return ReturnValue;
		}

		public string GetValueNameByProperty(RegistryKey Root,string Dir,object ValueProperty)
		{
			RegistryKey p_SubKey=Root;
			string strTemp="";
			string [] ReturnValue=null;

			try
			{
					object oValue=null;
				p_SubKey = p_SubKey.OpenSubKey(Dir,true);
				if(p_SubKey==null)
				{
					return strTemp;
				}
				ReturnValue = p_SubKey.GetValueNames();


				for(int i=0;i<ReturnValue.Length;i++)
				{
					oValue = p_SubKey.GetValue(ReturnValue[i]);
					if(oValue.ToString()==ValueProperty.ToString())
					{
						strTemp = ReturnValue[i];
					}
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return strTemp;
		
		}

		public bool PropertyExisted(RegistryKey Root,string Dir,object ValueProperty)
		{
			RegistryKey p_SubKey=Root;
			bool existed = false;
			string [] ReturnValue=null;

			try
			{
				object oValue=null;
				p_SubKey = p_SubKey.OpenSubKey(Dir,true);
				if(p_SubKey==null)
				{
					return existed;
				}
				ReturnValue = p_SubKey.GetValueNames();


				for(int i=0;i<ReturnValue.Length;i++)
				{
					oValue = p_SubKey.GetValue(ReturnValue[i]);
					if(oValue.ToString()==ValueProperty.ToString())
					{
						existed = true;
					}
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return existed;
		}

	}
	
}