c#下注册表操作的一个小细节
所属分类:
软件编程 / C#教程
阅读数:
149
收藏 0赞 0分享
先看一个有错误的代码:
string subKeyName = @"Software\Microsoft\Windows\CurrentVersion\Run\"; //subkey name
string valueName = @"App Name"; //name of the more specific key that will hold the value, "" means (Default)
try
...{
RegistryKey reg = Registry.LocalMachine.OpenSubKey(subKeyName);
if (reg != null)
...{
reg.DeleteValue(valueName);
reg.Close();
}
}
catch (Exception ex)
...{
MessageBox.Show(this, ex.ToString());
}
执行这段代码,你可以会收到以下异常:
System.UnauthorizedAccessException
原因很简单:
RegistryKey.OpenSubKey (String) 以只读方式检索子项
public RegistryKey OpenSubKey ( string name, bool writable)writable如果需要项的写访问权限,则设置为 true。
我们需要带第二个参数,标示我们是可写方式打开的。
c# 深拷贝与浅拷贝的区别分析及实例
浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
收藏 0赞 0分享
C#利用com操作excel释放进程的解决方法
最近利用Microsoft.Office.Interop.Excel.Application读取一个excel后,进程中一直存在excel,在网上找了一阵子,其中有几个解决方案
收藏 0赞 0分享
查看更多