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;

 

}

}

}

}

转载于:https://www.cnblogs.com/fan158/archive/2008/09/22/1295654.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值