C# 调用cmd 执行注册dll 并复制到Windows下对应的文件夹

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;
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值