public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
            System.Runtime.InteropServices.RegistrationServices RS = new System.Runtime.InteropServices.RegistrationServices();

            //This should be fixed with .NET 3.5 SP1
            RS.RegisterAssembly(System.Reflection.Assembly.LoadFile(Environment.ExpandEnvironmentVariables(@"%PROGRAMFILES%\Reference Assemblies\Microsoft\Framework\v3.5\System.Management.Instrumentation.dll")), System.Runtime.InteropServices.AssemblyRegistrationFlags.SetCodeBase);
        }
示例#2
0
        private static int moveAndRegisterFile(string fileToSaveAndReg)
        {
            int returnInt = -1; //file registration return code

            string curFileDirectory = Path.GetDirectoryName(fileToSaveAndReg);
            string systemDirectory  = Environment.GetFolderPath(Environment.SpecialFolder.SystemX86);

            string newFileLocation = fileToSaveAndReg.Replace(curFileDirectory, systemDirectory);

            if (File.Exists(newFileLocation) == false)
            {
                File.Copy(fileToSaveAndReg, newFileLocation, false);
            }

            try {
                File.Delete(fileToSaveAndReg);
            }
            catch {
                //don't really care right now
            }

            using (System.Diagnostics.Process regsvr32Process = new System.Diagnostics.Process()) {
                regsvr32Process.StartInfo.FileName               = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "regsvr32.exe");
                regsvr32Process.StartInfo.CreateNoWindow         = true;
                regsvr32Process.StartInfo.RedirectStandardOutput = true;
                regsvr32Process.StartInfo.UseShellExecute        = false;

                regsvr32Process.StartInfo.Arguments = string.Format("/s \"{0}\"", newFileLocation);
                regsvr32Process.Start();
                regsvr32Process.WaitForExit();
                returnInt = regsvr32Process.ExitCode;
            }

            //need to handle the possibility that the file is a .Net assembly, and not a COM library. If COM registration was successful, the return code will have been 0:
            if (returnInt != 0)
            {
                System.Reflection.Assembly curAssembly = null;

                try {
                    curAssembly = System.Reflection.Assembly.LoadFrom(newFileLocation);
                }
                catch {
                    return(returnInt);
                }

                System.Runtime.InteropServices.RegistrationServices regAsm = new System.Runtime.InteropServices.RegistrationServices();

                try { regAsm.RegisterAssembly(curAssembly, System.Runtime.InteropServices.AssemblyRegistrationFlags.SetCodeBase); }
                catch {
                    try {
                        regAsm.RegisterAssembly(curAssembly, System.Runtime.InteropServices.AssemblyRegistrationFlags.None);
                    }
                    catch {
                        return(returnInt);
                    }
                }
            }


            return(returnInt);
        }