private void RegisterDll()
{
try
{
//查找固定路径下
string path = AppDomain.CurrentDomain.BaseDirectory + "/Resource/ZKMachine/";
string is64FolderPath = string.Empty;
//判断系统是否64 位
if (Environment.Is64BitOperatingSystem)
{
string targetPath = "C:/WINDOWS/SysWOW64/";
//如果已经注册则不需要二次注册
if (File.Exists(targetPath + "zkemkeeper.dll"))
{
return;
}
//获取当前文件夹所有文件
string[] files = Directory.GetFiles(path + "x64/", "*.dll");
foreach (string file in files)
{
string name = System.IO.Path.GetFileName(file);
//复制文件到windows 文件夹下
if (!File.Exists(targetPath+ name))
{
string dest = System.IO.Path.Combine(targetPath, name);
System.IO.File.Copy(file, dest);//复制文件
}
}
is64FolderPath = "x64/";
}
else
{
string targetPath = "C:/WINDOWS/system32/";
//如果已经注册则不需要二次注册
if (File.Exists(targetPath + "zkemkeeper.dll"))
{
return;
}
//获取当前文件夹所有文件
string[] files = Directory.GetFiles(path + "x86/", "*.dll");
foreach (string file in files)
{
//复制文件到windows 文件夹下
string name = System.IO.Path.GetFileName(file);
if (!File.Exists(targetPath + name))
{
string dest = System.IO.Path.Combine(targetPath, name);
System.IO.File.Copy(file, dest);//复制文件
}
}
is64FolderPath = "x86/";
}
//注册文件路径
string dllPath = Path.Combine( path + is64FolderPath, "zkemkeeper.dll");
if (!File.Exists(dllPath))
{
return;
}
//拼接命令参数
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)
{
throw ex;
}
}
C# 调用cmd 执行注册dll 并复制到Windows下对应的文件夹
最新推荐文章于 2024-09-19 22:16:55 发布