Registery

//============================================================================================================

// Copyright (C) 2004-2005 Microsoft Corporation

// All rights reserved.

// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY

// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT

// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR

// FITNESS FOR A PARTICULAR PURPOSE.

//============================================================================================================

using System;

using Microsoft.Win32;

   

namespace Helper

{

/// <summary>

/// Wrapper for functions to access the Registry.

/// </summary>

public sealed class RegistryHelper

{

   

/// <summary>

/// Get the value from the keyName and the valueName specified

/// key should be a subkey under HKeyLocalMachine

/// </summary>

/// <param name="keyName"></param>

/// <param name="valueName"></param>

/// <returns></returns>

public static string GetValueFromLocalMachine(string keyName, string valueName)

{

RegistryKey subKey;

   

try

{

//open the localMachine

RegistryKey localMachine = Registry.LocalMachine;

   

//open the subkey specified

subKey = localMachine.OpenSubKey(keyName);

}

catch(Exception ex)

{

throw new RegistryKeyNotFoundException("Key not found!", keyName, valueName, ex);

}

   

//if the key was not found, throw an error

if(subKey == null)

{

throw new RegistryKeyNotFoundException("Key not found!", keyName, valueName);

}

   

//get the value

object keyValue = subKey.GetValue(valueName);

   

//throw an exception if the value not found

if(keyValue == null)

{

throw new RegistryKeyNotFoundException("Value not found!" ,keyName, valueName);

}

   

   

return keyValue.ToString();

}

   

/// <summary>

/// Set Registry Key in the Current user node

/// </summary>

/// <param name="keyValue"></param>

public static void SetValueOnCurrentUser(string keyName, string valueName, int keyValue)

{

 

RegistryKey key = Registry.CurrentUser.CreateSubKey(keyName);        

key.SetValue(valueName,keyValue);

}

   

 

/// <summary>

/// Get the value of a registry key from the current user node

/// </summary>

/// <param name="keyName"></param>

/// <param name="valueName"></param>

/// <returns></returns>

public static object GetValueFromCurrentUser(string keyName, string valueName)

{

RegistryKey subKey;

try

{

//open the localMachine

RegistryKey localMachine = Registry.CurrentUser;

   

//open the subkey specified

subKey = localMachine.OpenSubKey(keyName);

}

catch(Exception ex)

{

throw new RegistryKeyNotFoundException("Key not found!", keyName, valueName, ex);

 

}

   

//if the key was not found, throw an error

if(subKey == null)

{

throw new RegistryKeyNotFoundException("Key not found!", keyName, valueName);

 

}

   

//get the value

object keyValue = subKey.GetValue(valueName);

   

//throw an exception if the value not found

if(keyValue == null)

{

throw new RegistryKeyNotFoundException("Value not found!" ,keyName, valueName);

}

   

   

return keyValue;

   

   

}

   

   

   

/// <summary>

/// New exception type for missing keys

/// </summary>

public class RegistryKeyNotFoundException : ApplicationException

{

/// <summary>

/// define private variables to hold data

/// </summary>

private string keyName;

private string valueName;

private Exception innerException;

   

/// <summary>

/// Get the KeyName associated with this exception

/// </summary>

public string KeyName

{

get

{

 

return keyName;

}

}

   

/// <summary>

/// Get the value name associated with this exception

/// </summary>

public string ValueName

{

get

{

return valueName;

}

}

   

 

 

 

/// <summary>

/// pass the key that was not found

/// </summary>

/// <param name="keyName"></param>

public RegistryKeyNotFoundException(string message, string keyName, string valueName, Exception innerException) :base(message,innerException)

{

this.keyName= keyName;

this.valueName = valueName;

this.innerException = innerException;

 

}

   

   

/// <summary>

/// pass the key and the value that was not found

/// </summary>

/// <param name="keyName"></param>

public RegistryKeyNotFoundException(string message, string keyName, string valueName) : base(message)

{

this.valueName = valueName;

this.keyName= keyName;

 

}

}

}

}

posted on 2008-09-22 09:29  fanet  阅读(316)  评论(0编辑  收藏  举报