zoukankan      html  css  js  c++  java
  • C# 注册Dll文件

    有时会遇到dll在系统中不存在,需要程序自己去注册所需的dll文件。

    注册dll 需要用到regsvr32命令,其用法为:
    "regsvr32 [/s] [/n] [/u] [/i[:cmdline]] dllname”。其中dllname为dll文件名

    参数有如下意义:
    /u——反注册控件
    /s——不管注册成功与否,均不显示提示框
    /c——控制台输出
    /i——跳过控件的选项进行安装(与注册不同)
    /n——不注册控件,此选项必须与/i选项一起使用

    分享代码如下:

    private bool RegisterDll()
            {
                bool result = true;
                try
                {
                    string dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XXX.dll");//获得要注册的dll的物理路径
                    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; }
  • 相关阅读:
    我对管理信息系统定位的理解
    正斜杠和反斜杠-windows、web、c语言大讨论
    java异常处理的两种方法
    使用throws抛出异常
    课后作业
    每日自学
    《梦断代码》读后感
    每日自学
    每日自学
    每日自学
  • 原文地址:https://www.cnblogs.com/xiesong/p/7243397.html
Copyright © 2011-2022 走看看