C#:注册组件

 

注册flash 为例: 代码比较差 仅供学习参考

        /// <summary>
        ///     注册组件
        /// </summary>
        private static void Registor()
        {
            PrintLog("注册flash");
#if _RELEASE && !_ORG_MANAGER
            string ver=GetRegistData("CurrentVersion");
            bool flag = true;
            if (ver.Length != 0)
            {
                string[] vString = ver.Split(',');
                if (vString.Length == 4)
                {
                    int major = 0;
                    int minor = 0;
                    int build = 0;
                    int revision = 0;

                    int.TryParse(vString[0],out major);
                    int.TryParse(vString[1], out minor);
                    int.TryParse(vString[2], out build);
                    int.TryParse(vString[3], out revision);

                    Version v1 = new Version(11, 6, 0, 0);
                    Version v2 = new Version(major, minor, build, revision);
                    if (v2>v1)
                        flag = false;
                }                          
            }
            if(flag)
            {
                Registor(@"bin\Flash32.ocx");      
            }
#endif
        }


        private static void Registor(string filename)
        {
            try
            {
                var fi = new FileInfo(filename);
                File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\" + fi.Name);
                File.Copy(fi.FullName, Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\" + fi.Name);
            }
            catch (Exception ex)
            {
                PrintException(ex);
            }

            try
            {
                var fi = new FileInfo(filename);

                if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\" + fi.Name))
                {
                    var p = new Process();
                    p.StartInfo.FileName = "regsvr32.exe";
                    p.StartInfo.CreateNoWindow = false;
                    p.StartInfo.Arguments = "/s " + Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\" +
                                            fi.Name;
                    p.Start();
                }
            }
            catch (Exception ex)
            {
                PrintException(ex);
            }
        }


        private static string GetRegistData(string name)
        {
            string registData = "";
            try
            {
                RegistryKey hkml = Registry.LocalMachine;
                RegistryKey software = hkml.OpenSubKey("SOFTWARE", false);
                RegistryKey aimdir = software.OpenSubKey("Macromedia\\Flashplayer", false);
                registData = aimdir.GetValue(name).ToString();
            }
            catch (Exception)
            {
            }
            return registData;
        }
View Code

 

posted @ 2016-12-22 16:06  慧由心生  阅读(345)  评论(0编辑  收藏  举报