示例#1
0
        /// <summary>
        /// 获得IE浏览器的版本号
        /// </summary>
        /// <returns></returns>
        public Version GetIEVersion()
        {
            try
            {
                RegisterUtil util = new RegisterUtil("software\\Microsoft\\Internet Explorer", RegDomain.LocalMachine);
                util.CreateSubKey();

                util.RegeditKey = "Version";
                Version version = null;
                if (util.IsRegeditKeyExist() == true)
                {
                    string versionStr = util.ReadRegeditKey().ToString();
                    version = new Version(versionStr);
                }

                util.RegeditKey = "svcVersion";
                Version svcVersion = null;
                if (util.IsRegeditKeyExist() == true)
                {
                    string svcVersionStr = util.ReadRegeditKey().ToString();
                    svcVersion = new Version(svcVersionStr);
                }

                Version newVersion = version;
                if (version == null && svcVersion != null)
                {
                    newVersion = svcVersion;
                }
                else if (version != null && svcVersion == null)
                {
                    newVersion = version;
                }
                else if (version != null && svcVersion != null && version.Major >= svcVersion.Major)
                {
                    newVersion = version;
                }
                else if (version != null && svcVersion != null && version.Major < svcVersion.Major)
                {
                    newVersion = svcVersion;
                }

                return(newVersion);
            }
            catch (Exception ex)
            {
                LogHelper.WriteException(ex);
                return(null);
            }
        }
示例#2
0
 /// <summary>
 /// 刷新系统托盘的图标
 /// </summary>
 public static void RefreshSystemTray()
 {
     try
     {
         int TrayWindow;
         NativeMethods.Rect WindowRect;
         int   SmallIconWidth;
         int   SmallIconHeight;
         Point CursorPos = Point.Empty;
         int   Row;
         int   Col;
         //{ 获得任务栏句柄和边框}
         int hwd = NativeMethods.FindWindow("Shell_TrayWnd", null);
         TrayWindow = NativeMethods.FindWindowEx(hwd, 0, "TrayNotifyWnd", null);
         int isGet = NativeMethods.GetWindowRect(new IntPtr(TrayWindow), out WindowRect);
         if (isGet == 0)
         {
             return;
         }
         //{ 获得小图标大小}
         SmallIconWidth  = NativeMethods.GetSystemMetrics(SM_CXSMICON);
         SmallIconHeight = NativeMethods.GetSystemMetrics(SM_CYSMICON);
         //{ 保存当前鼠标位置}
         NativeMethods.GetCursorPos(ref CursorPos);
         //{ 使鼠标快速划过每个图标 }
         for (Row = 0; Row <= (WindowRect.Bottom - WindowRect.Top) / SmallIconHeight; Row++)
         {
             for (Col = 0; Col <= (WindowRect.Right - WindowRect.Left) / SmallIconWidth; Col++)
             {
                 NativeMethods.SetCursorPos(WindowRect.Left + Col * SmallIconWidth, WindowRect.Top + Row * SmallIconHeight);
                 Thread.Sleep(1);
             }
         }
         //{恢复鼠标位置}
         NativeMethods.SetCursorPos(CursorPos.X, CursorPos.Y);
         //{ 重画任务栏 }
         NativeMethods.Rect tempRect = new NativeMethods.Rect();
         NativeMethods.RedrawWindow(new IntPtr(TrayWindow), ref tempRect, 0, 0x85);
     }
     catch (Exception ex)
     {
         LogHelper.WriteException(ex);
     }
 }
示例#3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="findType"></param>
 /// <param name="findName"></param>
 /// <returns></returns>
 protected static bool CheckCertificate(X509FindType findType, string findName)
 {
     try
     {
         X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
         store.Open(OpenFlags.MaxAllowed);
         X509Certificate2Collection certs = store.Certificates.Find(findType, findName, false);
         if (certs.Count == 0 || certs[0].NotAfter < DateTime.Now)
         {
             return(false);
         }
         return(true);
     }
     catch (Exception ex)
     {
         LogHelper.WriteException(ex);
         return(false);
     }
 }
示例#4
0
 /// <summary>
 /// 安装CA的根证书到受信任根证书颁发机构
 /// </summary>
 /// <param name="certificatePath">证书路径</param>
 /// <returns></returns>
 public static bool SetupCertificate(string certificatePath)
 {
     try
     {
         if (System.IO.File.Exists(certificatePath) == false)
         {
             LogHelper.WriteLog("证书文件不存在,无法安装。" + certificatePath);
             return(false);
         }
         X509Certificate2 certificate = new X509Certificate2(certificatePath);
         X509Store        store       = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
         store.Open(OpenFlags.ReadWrite);
         store.Remove(certificate);   //可省略
         store.Add(certificate);
         store.Close();
         return(true);
     }
     catch (Exception ex)
     {
         LogHelper.WriteException(ex);
         return(false);
     }
 }
        /// <summary>
        /// 通过反射一个对象,拷贝到另一个对象
        /// </summary>
        /// <param name="sourObject"></param>
        /// <returns></returns>
        public static object ObjectMapToObject(object sourceObject)
        {
            try
            {
                //从源对象中获得类型转化的特性
                object[] converterAttributes = sourceObject.GetType().GetCustomAttributes(typeof(TypeConverterAttribute), true);
                if (converterAttributes == null || converterAttributes.Length < 1)
                {
                    return(null);
                }
                //获得源对象的所有属性
                PropertyInfo[] sourceProperties = sourceObject.GetType().GetProperties();
                if (sourceProperties == null || sourceProperties.Length < 1)
                {
                    return(null);
                }
                object mapObject = null;
                TypeConverterAttribute converterAttr = converterAttributes[0] as TypeConverterAttribute;
                //创建需要映射的对象实例
                mapObject = Activator.CreateInstance(Type.GetType(converterAttr.ConverterTypeName));
                //遍历所有的属性
                foreach (PropertyInfo sourceProperty in sourceProperties)
                {
                    //从属性中获得显示特性
                    object[] propertyAttrs = sourceProperty.GetCustomAttributes(typeof(DisplayNameAttribute), true);
                    if (propertyAttrs == null || propertyAttrs.Length < 1)
                    {
                        continue;
                    }
                    DisplayNameAttribute propertyAttr = propertyAttrs[0] as DisplayNameAttribute;
                    //从属性中获得需要映射的类型特性
                    //object[] propertyConvertAttrs = sourceProperty.GetCustomAttributes(typeof(TypeConverterAttribute), true);
                    //TypeConverterAttribute propertyConvertAttr = null;
                    //if (propertyConvertAttrs != null && propertyConvertAttrs.Length > 0)
                    //{
                    //    propertyConvertAttr = propertyAttrs[0] as TypeConverterAttribute;
                    //}

                    string   displayNameStr = propertyAttr.DisplayName;
                    string[] displayNames   = displayNameStr.Split(',');
                    if (displayNames == null || displayNames.Length < 1)
                    {
                        continue;
                    }
                    foreach (string displayName in displayNames)
                    {
                        //从映射对象中获得所有的属性
                        PropertyInfo[] mapProperties = mapObject.GetType().GetProperties();
                        if (mapProperties == null || mapProperties.Length < 1)
                        {
                            continue;
                        }
                        foreach (PropertyInfo mapProperty in mapProperties)
                        {
                            if (mapProperty.Name.Equals(displayName.Trim(), StringComparison.OrdinalIgnoreCase) == false)
                            {
                                continue;
                            }
                            if (mapProperty.PropertyType.IsGenericType)
                            {//判断是否是泛型类型
                                //获得源属性值
                                object sourceItem = sourceProperty.GetValue(sourceObject, null);
                                //获得源列表的元素数量
                                int count = (int)sourceItem.GetType().InvokeMember("get_Count", System.Reflection.BindingFlags.Instance
                                                                                   | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public, null, sourceItem, null);
                                //通过反射创建映射属性
                                object mapItems = Activator.CreateInstance(mapProperty.PropertyType, true);
                                //遍历源集合,赋值给目标对象
                                for (int i = 0; i < count; i++)
                                {
                                    object sourceItemObject = sourceItem.GetType().GetProperty("Item").GetValue(sourceItem, new object[] { i });
                                    Object mapChild         = ObjectMapToObject(sourceItemObject);

                                    mapItems.GetType().InvokeMember("Add", System.Reflection.BindingFlags.Public |
                                                                    System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance
                                                                    , null, mapItems, new object[] { mapChild });
                                }
                                mapProperty.SetValue(mapObject, mapItems, null);
                                break;
                            }
                            else if (!mapProperty.PropertyType.IsValueType && mapProperty.PropertyType != typeof(string))
                            {//判断 映射的属性是否是自定义类型
                                object sourceValue = sourceProperty.GetValue(sourceObject, null);
                                object mapValue    = ObjectMapToObject(sourceValue);
                                mapProperty.SetValue(mapObject, mapValue, null);
                            }
                            else
                            {
                                mapProperty.SetValue(mapObject, sourceProperty.GetValue(sourceObject, null), null);
                                break;
                            }
                        }
                    }
                }

                return(mapObject);
            }
            catch (Exception ex)
            {
                LogHelper.WriteException(ex);
                return(null);
            }
        }
示例#6
0
        protected static object Create(string url, string codespace, string classname)
        {
            try
            {
                Uri uri = new Uri(url);
            }
            catch (Exception ex)
            {
                LogHelper.WriteException(ex);
                throw new Exception("叫号服务地址错误,请确认配置正确!");
            }

            if (string.IsNullOrEmpty(classname))
            {
                classname = WebServiceProxy.GetWsClassName(url);
            }
            try
            {
                //获取WSDL
                WebClient          wc     = new WebClient();
                Stream             stream = wc.OpenRead(url + "?WSDL");
                ServiceDescription sd     = ServiceDescription.Read(stream);
                stream.Close();
                stream = null;
                ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
                sdi.AddServiceDescription(sd, "", "");
                CodeNamespace cn = new CodeNamespace(codespace);
                //生成客户端代理类代码
                CodeCompileUnit ccu = new CodeCompileUnit();
                ccu.Namespaces.Add(cn);
                sdi.Import(cn, ccu);
                CSharpCodeProvider csc = new CSharpCodeProvider();
                //ICodeCompiler icc = csc.CreateCompiler();
                //设定编译参数
                CompilerParameters cplist = new CompilerParameters();
                cplist.GenerateExecutable = false;
                cplist.GenerateInMemory   = true;
                cplist.ReferencedAssemblies.Add("System.dll");
                cplist.ReferencedAssemblies.Add("System.XML.dll");
                cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
                cplist.ReferencedAssemblies.Add("System.Data.dll");
                //编译代理类
                CompilerResults cr = csc.CompileAssemblyFromDom(cplist, ccu);  //icc.CompileAssemblyFromDom(cplist, ccu);
                if (true == cr.Errors.HasErrors)
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
                    {
                        sb.Append(ce.ToString());
                        sb.Append(System.Environment.NewLine);
                    }
                    throw new Exception(sb.ToString());
                }
                //生成代理实例,并调用方法
                System.Reflection.Assembly assembly = cr.CompiledAssembly;
                Type   t   = assembly.GetType(codespace + "." + classname, true, true);
                object obj = Activator.CreateInstance(t);
                //System.Reflection.MethodInfo mi = t.GetMethod(methodName);
                // return mi.Invoke(obj, args);
                return(obj);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
                }
                else
                {
                    throw ex;
                }
            }
        }