/// <summary> /// 深度拷贝 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="myobject"></param> /// <returns></returns> public static T DeepCopy <T>(T myobject) where T : class { T TReturn = default(T);//返回的对象,default此关键字对于引用类型会返回空,对于数值类型会返回零 try { using (MemoryStream memory3 = new MemoryStream()) { XmlSerializer xs = new XmlSerializer(typeof(T)); xs.Serialize(memory3, myobject); //序列化 memory3.Seek(0, SeekOrigin.Begin); //移动到开头 //反序列化 XmlSerializer xs2 = new XmlSerializer(typeof(T)); TReturn = xs2.Deserialize(memory3) as T; //这样就深度拷贝了 memory3.Close(); //销毁 return(TReturn); } } catch (System.Exception ex) { ClsErrorFile.WriteLine("不能深度复制,原因是:", ex); } return(null); }
/// <summary> /// 将MYObject序列化保存到XML文件,类型为T, /// </summary> /// <typeparam name="T"></typeparam> /// <param name="strFileName"></param> /// <param name="MyObject"></param> /// <returns></returns> public static bool Save <T>(string strFileName, T MyObject) where T : class { //就是保存myShapes这个 try { using (Stream stream = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.None)) { XmlSerializer xmls = new XmlSerializer(typeof(T)); xmls.Serialize(stream, MyObject); return(true);//成功返回true; } } catch (Exception exception) { ClsErrorFile.WriteLine("保存XML不成功,原因是", exception); //MessageBox.Show("保存不成功,原因是" + exception.Message); if (exception.InnerException != null) { ClsErrorFile.WriteLine("保存XML不成功" + exception.InnerException.Message); } //MessageBox.Show(exception.InnerException.Message); } return(false);//意味着失败了。 }
private static byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) { try { //Create a new instance of RSACryptoServiceProvider. RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); //Import the RSA Key information. This needs //to include the private key information. RSA.ImportParameters(RSAKeyInfo); //Decrypt the passed byte array and specify OAEP padding. //OAEP padding is only available on Microsoft Windows XP or //later. return(RSA.Decrypt(DataToDecrypt, DoOAEPPadding)); } //Catch and display a CryptographicException //to the console. catch (CryptographicException e) { //Exceptions.LogException(e); //Console.Error.WriteLine(e.Message); //ClsErrorFile.WriteLine(e.Message); ClsErrorFile.WriteLine("", e); return(null); } }
/// <summary> /// 获取网页的HTML码 /// </summary> /// <param name="strUrl">链接地址</param> /// <param name="encoding">编码类型</param> /// <returns></returns> public static string GetHtmlFromInternet(string strUrl) { string htmlStr = ""; string strCharset = null; if (!String.IsNullOrEmpty(strUrl)) { try { WebRequest request = WebRequest.Create(strUrl); //实例化WebRequest对象 //创建WebResponse对象 //用两次流,第一次输出,第二次是取得判断编码后的输出 using (WebResponse response = request.GetResponse()) { using (Stream datastream = response.GetResponseStream()) { //用两次流,第一次输出,第二次是取得判断编码后的输出 using (StreamReader reader = new StreamReader(datastream)) { htmlStr = reader.ReadToEnd(); //读取数据 } strCharset = getCharSet(htmlStr); //datastream.Close(); } //response.Close(); } //根据编码读取流 if (strCharset != null) { request = WebRequest.Create(strUrl); using (WebResponse response = request.GetResponse()) { using (Stream datastream = response.GetResponseStream()) { Encoding ec = Encoding.GetEncoding(strCharset); using (StreamReader reader = new StreamReader(datastream, ec)) { htmlStr = reader.ReadToEnd(); //读取数据 //这里的就是编码后的数据啦 } } } } } catch (Exception ex) { ClsErrorFile.WriteLine("GetHtmlFromInternet:" + ex.Message); return(string.Empty); //throw; } } return(htmlStr); }
public static void Start(string str) { try { System.Diagnostics.Process.Start(str); } catch (System.Exception ex) { ClsErrorFile.WriteLine(ex); } }
/// <summary> /// 从指定文件反序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="strFileName"></param> /// <returns></returns> public static T Load <T>(string strFileName) where T : class { try { using (Stream stream = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { XmlSerializer formatter = new XmlSerializer(typeof(T)); T tReturn = formatter.Deserialize(stream) as T; return(tReturn); } } catch (Exception exception) { ClsErrorFile.WriteLine("加载XML不成功,原因是", exception); //MessageBox.Show("加载不成功,原因是" + exception.Message); return(null); } }
private static string Decrypt(string base64code) //解密 { try { //Create a UnicodeEncoder to convert between byte array and string. UnicodeEncoding ByteConverter = new UnicodeEncoding(); //Create a new instance of RSACryptoServiceProvider to generate //public and private key data. RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); string strPrivateKey = @"<RSAKeyValue><Modulus>qnfhMY6XO+fJDmd84nbAyH51xR3gb8ow7GWr3RPl172sYnCCTprCgSg2Y7HexH43p38WHk6bR1hdkic2cYIcz7gqrLs3CsY/YlxljJQ0MGjfeK+OY1L2tB482cE/wjVKAbCG5J+4vzo13S+whKHxsvlkGRM5KpDHyd0ZnE37V8k=</Modulus><Exponent>AQAB</Exponent><P>7W3IhAKh8njPL4XeIf9xjX2HqIgWUS1aIcIEr7bXY5ey53aw47yfkixSudeSZolJMPpGC+GO6hIyEmznlB63iw==</P><Q>t81LaijAd3Utn7xX/QQ/x9c8ijWgyeWQVWyA4F+7Ay6O5Ztke4ufJq6VFslpI0CDe4DUrp2gBtqEAjN/XZB4ew==</Q><DP>UtX3nF8Sw3b0yh7JdlEZ/ARs3RbFuoK5LIf1fJytHxkhGPJnGr2Hasc+AYq9kDqbp5PZ9nE2nGHGyHjoftwMqw==</DP><DQ>Uzx+TZoc5zxCqBcURbnZ5HddrD1zDluOzJCxoGrZ9yvrfKGtlKF7NnpTfBlEKrm5kYGbT2SEpvXoWFLX+BhH5w==</DQ><InverseQ>xKYnwi/1O57Na9fS0GJHxy5/BXdEwqZ7KSeZsftFxrUiO60meb5yFN6MnGANE0A6pqf0tBLgciK8muJVYg7Tsg==</InverseQ><D>Qc7NrKfzUjkEsP7ag0J84emP5WzHO+C+SkRluI755/NdHRN5+oZcGChB9vKvoQNo0MyK6WBHKZ+/X7Crn94u6I7+1+owWeppsd5uie3rruMIZOzaUeGxmiNXsMDZuY7r5aQVb/zccX9+ccMk6DPfE1UVjTsLcUwg8t4tjJ/49lE=</D></RSAKeyValue>"; RSA.FromXmlString(strPrivateKey); byte[] encryptedData; byte[] decryptedData; encryptedData = Convert.FromBase64String(base64code); //Pass the data to DECRYPT, the private key information //(using RSACryptoServiceProvider.ExportParameters(true), //and a boolean flag specifying no OAEP padding. decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false); //Display the decrypted plaintext to the console. return(ByteConverter.GetString(decryptedData)); } catch (Exception exc) { //Exceptions.LogException(exc); //ClsErrorFile.WriteLine(exc.Message); ClsErrorFile.WriteLine("", exc); //Console.Error.WriteLine(exc.Message); return(""); } }
/// <summary> /// 获取网页的HTML码,这个会自动判断网页编码 /// </summary> /// <param name="strUrl">链接地址</param> /// <param name="encoding">编码类型</param> /// <returns></returns> public static string GetHtmlFromInternet2(string strUrl) { string htmlStr = ""; Encoding encoding = Encoding.Default; if (!String.IsNullOrEmpty(strUrl)) { try { WebRequest request = WebRequest.Create(strUrl); //实例化WebRequest对象 //创建WebResponse对象 using (WebResponse response = request.GetResponse()) { //取得编码信息,如果有的话 string str_Content_type = response.ContentType; //获得chardet Regex regex = new Regex("charset\\s*=\\s*(\\S+)", RegexOptions.IgnoreCase); Match match = null; if (str_Content_type != null) { match = regex.Match(str_Content_type); //如果有编码信息 if (match.Success) { try { encoding = Encoding.GetEncoding(match.Groups[1].Value.Trim()); } catch (Exception ex) { ClsErrorFile.WriteLine("GetHtmlFromInternet2:" + ex.Message); return(null); //throw; } } } //////如上就取得了编码信息了,如果有的话,如果没有就是默认编码啦 //如下是读取啦。 try { using (TextReader reader = new StreamReader(response.GetResponseStream(), encoding)) { htmlStr = reader.ReadToEnd(); } } catch (Exception ex) { ClsErrorFile.WriteLine("GetHtmlFromInternet2:" + ex.Message); return(null); //Console.WriteLine(exx); } //response.Close(); } } catch (Exception ex) { ClsErrorFile.WriteLine("GetHtmlFromInternet:" + ex.Message); return(string.Empty); //throw; } } return(convertUnicodeToString(htmlStr)); }