win7下调用regsvr32注册组件失败,但是以管理员身份使用regsvr32命令注册就成功
   那C#如何以管理员身份电泳regsvr32命令呢

解决方案 »

  1.   

    右键工程-->属性-->安全性-->启用ClickOnce安全设置
    这时Properties目录下会有一个app.manifest文件,双击编辑
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <assemblyIdentity version="1.0.0.0"
         processorArchitecture="X86"
         name="IsUserAdmin"
         type="win32"/>
    <description>Description of your application</description>
    <!-- Identify the application security requirements. -->
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
          <requestedPrivileges>//加下这个节点
            <requestedExecutionLevel
              level="requireAdministrator"
              uiAccess="false"/>
            </requestedPrivileges>
           </security>
    </trustInfo>
    </assembly>
    听说这样可以让程序自动以管理员方式运行,但我没试过。你可以试一下
      

  2.   

    给你一个注册com组件的类吧using System;
    using System.Runtime.InteropServices;public static class ComUtils
    {
        public static bool RegisterComComponent(string path)
        {
            // Register COM component, false if not a COM component
            IntPtr module = LoadLibraryEx(path, IntPtr.Zero, 0);
            if (module == IntPtr.Zero) throw new ArgumentException("Could not load DLL, error code=" + Marshal.GetLastWin32Error().ToString());
            try
            {
                IntPtr addr = GetProcAddress(module, "DllRegisterServer");
                if (addr == IntPtr.Zero) return false;
                DllRegisterServer dlg = (DllRegisterServer)Marshal.GetDelegateForFunctionPointer(addr, typeof(DllRegisterServer));
                int hr = dlg.Invoke();
                if (hr != 0) Marshal.ThrowExceptionForHR(hr);
                return true;
            }
            finally
            {
                FreeLibrary(module);
            }
        }
        // P/Invoke declarations
        private delegate int DllRegisterServer();
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr LoadLibraryEx(string path, IntPtr dummy, int flags);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool FreeLibrary(IntPtr hdl);
        [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
        private static extern IntPtr GetProcAddress(IntPtr hdl, string name);
    }
    看看这个好不好使。
      

  3.   

    这个方法好像只能是VS2008以上的吧VS2005也可以吗?
      

  4.   

    可以的,vs 2005和2008都可以的
    http://clacklin.blog.163.com/blog/static/103209820106108538269/
      

  5.   

    为什么我用VSO8可以,VS05就不行啊,app.manifest文件如下:<?xml version="1.0" encoding="utf-8"?>
    <asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
          <requestedPrivileges>
            <requestedExecutionLevel  level="requireAdministrator"   uiAccess="false"/>
          </requestedPrivileges>      <applicationRequestMinimum>
            <defaultAssemblyRequest permissionSetReference="Custom" />
            <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
          </applicationRequestMinimum>
        </security>
      </trustInfo>
    </asmv1:assembly>