/* * http接口工具类 */ public class httpUitls { /* * get请求 */ public static string Get(string Url) { //System.GC.Collect(); httpWebRequest request = (httpWebRequest)WebRequest.Create(Url); request.Proxy = null; request.KeepAlive = false; request.Method = "GET"; request.ContentType = "application/Json; charset=UTF-8"; request.automaticDecompression = DecompressionMethods.GZip; httpWebResponse response = (httpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream,EnCoding.UTF8); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); if (response != null) { response.Close(); } if (request != null) { request.Abort(); } return retString; } /* * post请求 Referer(允许为空) */ public static string Post(string Url,string Data,string Referer) { string retString = ""; try { httpWebRequest request = (httpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.Referer = Referer; byte[] bytes = EnCoding.UTF8.GetBytes(Data); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = bytes.Length; Stream myResponseStream = request.GetRequestStream(); myResponseStream.Write(bytes,0,bytes.Length); httpWebResponse response = (httpWebResponse)request.GetResponse(); StreamReader myStreamReader = new StreamReader(response.GetResponseStream(),EnCoding.UTF8); retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); if (response != null) { response.Close(); } if (request != null) { request.Abort(); } } catch (Exception ex) { Console.Writeline("异常:" + ex); } return retString; } }
/* * Wav音频播放工具类 */ public class WavUtils { //播放 public static voID WAVPlay(string myWav) { Soundplayer sound = new Soundplayer(); sound.soundLocation = @myWav; sound.Load(); sound.Play(); } //暂停 public static voID WAVStop(string myWav) { Soundplayer sound = new Soundplayer(); sound.soundLocation = @myWav; sound.Load(); sound.Stop(); } }
/* * 加密解密工具类 */ public class EncryptHelper { //默认密钥 private static string AESKey = "[45/*YUIDse..e;]"; private static string DESKey = "[&HdN72]"; /// <summary> /// AES加密 /// </summary> public static string AESEncrypt(string value,string _aeskey = null) { if (string.IsNullOrEmpty(_aeskey)) { _aeskey = AESKey; } byte[] keyArray = EnCoding.UTF8.GetBytes(_aeskey); byte[] toEncryptArray = EnCoding.UTF8.GetBytes(value); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.padding = paddingMode.PKCS7; ICryptotransform ctransform = rDel.CreateEncryptor(); byte[] resultArray = ctransform.transformFinalBlock(toEncryptArray,toEncryptArray.Length); return Convert.ToBase64String(resultArray,resultArray.Length); } /// <summary> /// AES解密 /// </summary> public static string AESDecrypt(string value,string _aeskey = null) { try { if (string.IsNullOrEmpty(_aeskey)) { _aeskey = AESKey; } byte[] keyArray = EnCoding.UTF8.GetBytes(_aeskey); byte[] toEncryptArray = Convert.FromBase64String(value); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.padding = paddingMode.PKCS7; ICryptotransform ctransform = rDel.CreateDecryptor(); byte[] resultArray = ctransform.transformFinalBlock(toEncryptArray,toEncryptArray.Length); return EnCoding.UTF8.GetString(resultArray); } catch { return string.Empty; } } /// <summary> /// DES加密 /// </summary> public static string DESEncrypt(string value,string _deskey = null) { if (string.IsNullOrEmpty(_deskey)) { _deskey = DESKey; } byte[] keyArray = EnCoding.UTF8.GetBytes(_deskey); byte[] toEncryptArray = EnCoding.UTF8.GetBytes(value); DESCryptoServiceProvIDer rDel = new DESCryptoServiceProvIDer(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.padding = paddingMode.PKCS7; ICryptotransform ctransform = rDel.CreateEncryptor(); byte[] resultArray = ctransform.transformFinalBlock(toEncryptArray,resultArray.Length); } /// <summary> /// DES解密 /// </summary> public static string DESDecrypt(string value,string _deskey = null) { try { if (string.IsNullOrEmpty(_deskey)) { _deskey = DESKey; } byte[] keyArray = EnCoding.UTF8.GetBytes(_deskey); byte[] toEncryptArray = Convert.FromBase64String(value); DESCryptoServiceProvIDer rDel = new DESCryptoServiceProvIDer(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.padding = paddingMode.PKCS7; ICryptotransform ctransform = rDel.CreateDecryptor(); byte[] resultArray = ctransform.transformFinalBlock(toEncryptArray,toEncryptArray.Length); return EnCoding.UTF8.GetString(resultArray); } catch { return string.Empty; } } public static string MD5(string value) { byte[] result = EnCoding.UTF8.GetBytes(value); MD5 md5 = new MD5CryptoServiceProvIDer(); byte[] output = md5.ComputeHash(result); return BitConverter.ToString(output).Replace("-",""); } public static string HMACMD5(string value,string hmacKey) { HMACSHA1 hmacsha1 = new HMACSHA1(EnCoding.UTF8.GetBytes(hmacKey)); byte[] result = System.Text.EnCoding.UTF8.GetBytes(value); byte[] output = hmacsha1.ComputeHash(result); return BitConverter.ToString(output).Replace("-",""); } /// <summary> /// base64编码 /// </summary> /// <returns></returns> public static string Base64Encode(string value) { string result = Convert.ToBase64String(EnCoding.Default.GetBytes(value)); return result; } /// <summary> /// base64解码 /// </summary> /// <returns></returns> public static string Base64Decode(string value) { string result = EnCoding.Default.GetString(Convert.FromBase64String(value)); return result; } }
/* * 获取版本号工具类 */ public class ExeVersionUtils { /* * exe路径,返回当前版本号 */ public static string getoldVersionNumber(string exePath) { string FiLeversions = ""; FiLeversionInfo file1 = System.Diagnostics.FiLeversionInfo.GetVersionInfo(exePath); //版本号显示为“主版本号.次版本号.内部版本号.专用部件号”。 FiLeversions = String.Format("{0}.{1}.{2}.{3}",file1.fileMajorPart,file1.fileMinorPart,file1.fileBuildPart,file1.filePrivatePart); return FiLeversions; } /* * 检测是否需要升级本地的客户端,不需要返回"",需要直接返回下载地址 * 调用接口返回当前服务器最新的版本号 */ public static string CheckVersion(String LocalVersion,String loginname) { string downloadURL = ""; //把接口写了 string ServerVersion = httpUitls.Get(""); if (Compareversion(ServerVersion,LocalVersion)) { //下载最新客户端安装包的地址 downloadURL = ""; } return downloadURL; } /* * 版本号比较(使用C#自带的Version对象来比较版本号) * ServerVersion 服务器最新版本号,LocalVersion 本地程序版本号 */ public static bool Compareversion(string ServerVersion,string LocalVersion) { bool flag = false; Version sv = new Version(ServerVersion); Version lv = new Version(LocalVersion); //服务器上面的客户端的版本号比本地客户端的版本号高。 if (sv > lv) { flag = true; } if (sv < lv) { flag = false; } return flag; } }
/* * 下载文件(显示下载百分比,winform控件) */ public class fileDownload { /// <summary> /// 下载文件 /// </summary> /// <param name="URL">下载文件地址</param> /// <param name="filename">下载后的存放地址</param> /// <param name="Prog">用于显示的进度条</param> /// public static bool Downloadfile(string URL,string filename,System.windows.Forms.Progressbar prog,System.windows.Forms.Label label1) { float percent = 0; try { System.Net.httpWebRequest Myrq = (System.Net.httpWebRequest)System.Net.httpWebRequest.Create(URL); System.Net.httpWebResponse myrp = (System.Net.httpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; if (prog != null) { prog.Maximum = (int)totalBytes; } System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream so = new System.IO.fileStream(filename,System.IO.fileMode.Create); long totalDownloadedByte = 0; byte[] by = new byte[1024]; int osize = st.Read(by,(int)by.Length); while (osize > 0) { totalDownloadedByte = osize + totalDownloadedByte; System.windows.Forms.Application.DoEvents(); so.Write(by,osize); if (prog != null) { prog.Value = (int)totalDownloadedByte; } osize = st.Read(by,(int)by.Length); percent = (float)totalDownloadedByte / (float)totalBytes * 100; label1.Text = percent.ToString() + "%"; System.windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息 } so.Close(); st.Close(); return true; } catch (System.Exception) { return false; throw; } } }
总结 以上是内存溢出为你收集整理的本人亲测-C#常用工具类全部内容,希望文章能够帮你解决本人亲测-C#常用工具类所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)