C# 注册dll的两种方式

本文介绍了C#中注册DLL的两种方法:普通注册和管理员注册。普通注册使用`regsvr32`命令,而管理员注册则通过检查当前用户权限,必要时以管理员权限运行进程,确保DLL的正确注册。

普通注册

string path = System.AppDomain.CurrentDomain.BaseDirectory ;

System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", path + "DicomObjects.ocx");

 

管理员注册

private bool RegisterDll(String dllPath)

{

bool result = true;

try

{

if (!File.Exists(dllPath))

{

//Loger.Write(string.Format("“{0}”目录下无“XXX.dll”文件!", AppDomain.CurrentDomain.BaseDirectory));

return false;

}

//拼接命令参数

string startArgs = string.Format("/s \"{0}\"", dllPath);

 

Process p = new Process();//创建一个新进程,以执行注册动作

p.StartInfo.FileName = "regsvr32";

p.StartInfo.Arguments = startArgs;

 

//以管理员权限注册dll文件

WindowsIdentity winIdentity = WindowsIdentity.GetCurrent(); //引用命名空间 System.Security.Principal

WindowsPrincipal winPrincipal = new WindowsPrincipal(winIdentity);

if (!winPrincipal.IsInRole(WindowsBuiltInRole.Administrator))

{

p.StartInfo.Verb = "runas";//管理员权限运行

}

p.Start();

p.WaitForExit();

p.Close();

p.Dispose();

}

catch (Exception ex)

{

result = false;         //记录日志,抛出异常

}

return result;

}

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值