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





