根据自己是32位还是64位来选择Regsvr32,以下是用代码注册的DLL
// Assembly assembly = Assembly.LoadFile(dllPath);
// 获取CLSID
// Guid guid = new Guid(((GuidAttribute)Attribute.GetCustomAttribute(assembly, typeof(GuidAttribute))).Value);
using (Process process = new Process())
{
string regPath = System.Environment.GetFolderPath(Environment.SpecialFolder.System);
if (Environment.Is64BitOperatingSystem)
{
regPath = System.Environment.GetFolderPath(Environment.SpecialFolder.SystemX86);
}
regPath = Path.Combine(regPath, "regsvr32");
string reg32DllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "OPCDAAuto.dll"); // 要注册的dll路径
ProcessStartInfo startInfo = new ProcessStartInfo(reg32DllPath)
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = regPath,
Arguments = $" {reg32DllPath} /s ",
Verb = "runas"
};
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
Console.WriteLine(reg32DllPath + "注册成功");