#region 判断字符串是否为数字 public static bool isNumber(string sourcestr) { bool tag = true; foreach(char chr in sourcestr) { if (!char.IsDigit(chr)) { tag = false; } } return tag; } #endregion #region 远程服务器下载文件 public static ArrayList GetImgFileUrl(string sourceString) { ArrayList imgArray = new ArrayList(); Regex r = new Regex("<IMG(.*?)src=('|/"|)(http://.*?)('|/"| |>)", RegexOptions.IgnoreCase | RegexOptions.Compiled); MatchCollection mc = r.Matches(sourceString); for (int i = 0; i < mc.Count; i++) { if (!imgArray.Contains(mc[i].Result("$3"))) { imgArray.Add(mc[i].Result("$3")); } } return imgArray; } /// <summary> /// 远程提取文件保存至服务器上(使用WebRequest) /// </summary> /// <param name="url">一个URI上的文件</param> /// <param name="saveurl">文件保存在服务器上的全路径</param> public static void RemoteGetFile(string url, string saveurl) { HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse(); Stream stream = myWebResponse.GetResponseStream(); //获得请求的文件大小 int fileSize = (int)myWebResponse.ContentLength; int bufferSize = 102400; byte[] buffer = new byte[bufferSize]; // function.WriteFile(saveurl, "temp"); // 建立一个写入文件的流对象 FileStream saveFile = File.Create(saveurl, bufferSize); int bytesRead; do { bytesRead = stream.Read(buffer, 0, buffer.Length); saveFile.Write(buffer, 0, bytesRead); } while (bytesRead > 0); saveFile.Flush(); saveFile.Close(); stream.Flush(); stream.Close(); } #endregion #region 转半角的函数 ToDBC(string input) /**/ /// <summary> /// 转半角的函数(DBC case) /// </summary> /// <param name="input">任意字符串</param> /// <returns>半角字符串</returns> ///<remarks> ///全角空格为12288,半角空格为32 ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248 ///</remarks> public string ToDBC(string input) { char[] c = input.ToCharArray(); for (int i = 0; i < c.Length; i++) { if (c[i] == 12288) { c[i] = (char)32; continue; } if (c[i] > 65280 && c[i] < 65375) c[i] = (char)(c[i] - 65248); } return new string(c); } #endregion #region 过滤危险单词,将其转化为全角字符 GetSafeStr(string sourcestr) public static string GetSafeStr(string sourcestr) { sourcestr = sourcestr.Trim(); string lowstr = sourcestr.ToLower().Trim(); string tmpstr = ""; string[] safekey = System.Configuration.ConfigurationManager.AppSettings["safekey"].Split(','); foreach (string sk in safekey) { if (lowstr.IndexOf(sk) > 0) { tmpstr = sourcestr.Substring(lowstr.IndexOf(sk), sk.Length); sourcestr = sourcestr.Replace(tmpstr, ToSBC(sk)); } } return sourcestr; } #endregion