帮助类2

#region 清除HTML标记且返回相应的长度
2         public static string DropHTML(string Htmlstring, int strLen)
3         {
4             return CutString(DropHTML(Htmlstring), strLen);
5         }
6         #endregion
#region TXT代码转换成HTML格式
 2         /// <summary>
 3         /// 字符串字符处理
 4         /// </summary>
 5         /// <param name="chr">等待处理的字符串</param>
 6         /// <returns>处理后的字符串</returns>
 7         /// //把TXT代码转换成HTML格式
 8         public static String ToHtml(string Input)
 9         {
10             StringBuilder sb = new StringBuilder(Input);
11             sb.Replace("&", "&amp;");
12             sb.Replace("<", "&lt;");
13             sb.Replace(">", "&gt;");
14             sb.Replace("\r\n", "<br />");
15             sb.Replace("\n", "<br />");
16             sb.Replace("\t", " ");
17             //sb.Replace(" ", "&nbsp;");
18             return sb.ToString();
19         }
20         #endregion

 #region HTML代码转换成TXT格式
 2         /// <summary>
 3         /// 字符串字符处理
 4         /// </summary>
 5         /// <param name="chr">等待处理的字符串</param>
 6         /// <returns>处理后的字符串</returns>
 7         /// //把HTML代码转换成TXT格式
 8         public static String ToTxt(String Input)
 9         {
10             StringBuilder sb = new StringBuilder(Input);
11             sb.Replace("&nbsp;", " ");
12             sb.Replace("<br>", "\r\n");
13             sb.Replace("<br>", "\n");
14             sb.Replace("<br />", "\n");
15             sb.Replace("<br />", "\r\n");
16             sb.Replace("&lt;", "<");
17             sb.Replace("&gt;", ">");
18             sb.Replace("&amp;", "&");
19             return sb.ToString();
20         }
21         #endregion
复制代码
#region 检测是否有Sql危险字符
 2         /// <summary>
 3         /// 检测是否有Sql危险字符
 4         /// </summary>
 5         /// <param name="str">要判断字符串</param>
 6         /// <returns>判断结果</returns>
 7         public static bool IsSafeSqlString(string str)
 8         {
 9             return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
10         }
11 
12         /// <summary>
13         /// 检查危险字符
14         /// </summary>
15         /// <param name="Input"></param>
16         /// <returns></returns>
17         public static string Filter(string sInput)
18         {
19             if (sInput == null || sInput == "")
20                 return null;
21             string sInput1 = sInput.ToLower();
22             string output = sInput;
23             string pattern = @"*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'";
24             if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success)
25             {
26                 throw new Exception("字符串中含有非法字符!");
27             }
28             else
29             {
30                 output = output.Replace("'", "''");
31             }
32             return output;
33         }
34 
35         /// <summary> 
36         /// 检查过滤设定的危险字符
37         /// </summary> 
38         /// <param name="InText">要过滤的字符串 </param> 
39         /// <returns>如果参数存在不安全字符,则返回true </returns> 
40         public static bool SqlFilter(string word, string InText)
41         {
42             if (InText == null)
43                 return false;
44             foreach (string i in word.Split('|'))
45             {
46                 if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1))
47                 {
48                     return true;
49                 }
50             }
51             return false;
52         }
53         #endregion
复制代码
region 过滤特殊字符
 2         /// <summary>
 3         /// 过滤特殊字符
 4         /// </summary>
 5         /// <param name="Input"></param>
 6         /// <returns></returns>
 7         public static string Htmls(string Input)
 8         {
 9             if (Input != string.Empty && Input != null)
10             {
11                 string ihtml = Input.ToLower();
12                 ihtml = ihtml.Replace("<script", "&lt;script");
13                 ihtml = ihtml.Replace("script>", "script&gt;");
14                 ihtml = ihtml.Replace("<%", "&lt;%");
15                 ihtml = ihtml.Replace("%>", "%&gt;");
16                 ihtml = ihtml.Replace("<$", "&lt;$");
17                 ihtml = ihtml.Replace("$>", "$&gt;");
18                 return ihtml;
19             }
20             else
21             {
22                 return string.Empty;
23             }
24         }
25         #endregion
复制代码
 #region 检查是否为IP地址
 2         /// <summary>
 3         /// 是否为ip
 4         /// </summary>
 5         /// <param name="ip"></param>
 6         /// <returns></returns>
 7         public static bool IsIP(string ip)
 8         {
 9             return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
10         }
11         #endregion

 #region 获得配置文件节点XML文件的绝对路径
2         public static string GetXmlMapPath(string xmlName)
3         {
4             return GetMapPath(ConfigurationManager.AppSettings[xmlName].ToString());
5         }
6         #endregion

#region 获得当前绝对路径
 2         /// <summary>
 3         /// 获得当前绝对路径
 4         /// </summary>
 5         /// <param name="strPath">指定的路径</param>
 6         /// <returns>绝对路径</returns>
 7         public static string GetMapPath(string strPath)
 8         {
 9             if (strPath.ToLower().StartsWith("http://"))
10             {
11                 return strPath;
12             }
13             if (HttpContext.Current != null)
14             {
15                 return HttpContext.Current.Server.MapPath(strPath);
16             }
17             else //非web程序引用
18             {
19                 strPath = strPath.Replace("/", "\\");
20                 if (strPath.StartsWith("\\"))
21                 {
22                     strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
23                 }
24                 return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
25             }
26         }
27         #endregion
复制代码
 #region 文件操作
  2         /// <summary>
  3         /// 删除单个文件
  4         /// </summary>
  5         /// <param name="_filepath">文件相对路径</param>
  6         public static bool DeleteFile(string _filepath)
  7         {
  8             if (string.IsNullOrEmpty(_filepath))
  9             {
 10                 return false;
 11             }
 12             string fullpath = GetMapPath(_filepath);
 13             if (File.Exists(fullpath))
 14             {
 15                 File.Delete(fullpath);
 16                 return true;
 17             }
 18             return false;
 19         }
 20 
 21         /// <summary>
 22         /// 删除上传的文件(及缩略图)
 23         /// </summary>
 24         /// <param name="_filepath"></param>
 25         public static void DeleteUpFile(string _filepath)
 26         {
 27             if (string.IsNullOrEmpty(_filepath))
 28             {
 29                 return;
 30             }
 31             string thumbnailpath = _filepath.Substring(0, _filepath.LastIndexOf("/")) + "mall_" + _filepath.Substring(_filepath.LastIndexOf("/") + 1);
 32             string fullTPATH = GetMapPath(_filepath); //宿略图
 33             string fullpath = GetMapPath(_filepath); //原图
 34             if (File.Exists(fullpath))
 35             {
 36                 File.Delete(fullpath);
 37             }
 38             if (File.Exists(fullTPATH))
 39             {
 40                 File.Delete(fullTPATH);
 41             }
 42         }
 43 
 44         /// <summary>
 45         /// 返回文件大小KB
 46         /// </summary>
 47         /// <param name="_filepath">文件相对路径</param>
 48         /// <returns>int</returns>
 49         public static int GetFileSize(string _filepath)
 50         {
 51             if (string.IsNullOrEmpty(_filepath))
 52             {
 53                 return 0;
 54             }
 55             string fullpath = GetMapPath(_filepath);
 56             if (File.Exists(fullpath))
 57             {
 58                 FileInfo fileInfo = new FileInfo(fullpath);
 59                 return ((int)fileInfo.Length) / 1024;
 60             }
 61             return 0;
 62         }
 63 
 64         /// <summary>
 65         /// 返回文件扩展名,不含“.”
 66         /// </summary>
 67         /// <param name="_filepath">文件全名称</param>
 68         /// <returns>string</returns>
 69         public static string GetFileExt(string _filepath)
 70         {
 71             if (string.IsNullOrEmpty(_filepath))
 72             {
 73                 return "";
 74             }
 75             if (_filepath.LastIndexOf(".") > 0)
 76             {
 77                 return _filepath.Substring(_filepath.LastIndexOf(".") + 1); //文件扩展名,不含“.”
 78             }
 79             return "";
 80         }
 81 
 82         /// <summary>
 83         /// 返回文件名,不含路径
 84         /// </summary>
 85         /// <param name="_filepath">文件相对路径</param>
 86         /// <returns>string</returns>
 87         public static string GetFileName(string _filepath)
 88         {
 89             return _filepath.Substring(_filepath.LastIndexOf(@"/") + 1);
 90         }
 91 
 92         /// <summary>
 93         /// 文件是否存在
 94         /// </summary>
 95         /// <param name="_filepath">文件相对路径</param>
 96         /// <returns>bool</returns>
 97         public static bool FileExists(string _filepath)
 98         {
 99             string fullpath = GetMapPath(_filepath);
100             if (File.Exists(fullpath))
101             {
102                 return true;
103             }
104             return false;
105         }
106 
107         /// <summary>
108         /// 获得远程字符串
109         /// </summary>
110         public static string GetDomainStr(string key, string uriPath)
111         {
112             string result = string.Empty;// CacheHelper.Get(key) as string;
113             if (result == null)
114             {
115                 System.Net.WebClient client = new System.Net.WebClient();
116                 try
117                 {
118                     client.Encoding = System.Text.Encoding.UTF8;
119                     result = client.DownloadString(uriPath);
120                 }
121                 catch
122                 {
123                     result = "暂时无法连接!";
124                 }
125                 //CacheHelper.Insert(key, result, 60);
126             }
127 
128             return result;
129         }
130         /// <summary>
131         /// 读取指定文件中的内容,文件名为空或找不到文件返回空串
132         /// </summary>
133         /// <param name="FileName">文件全路径</param>
134         /// <param name="isLineWay">是否按行读取返回字符串 true为是</param>
135         public static string GetFileContent(string FileName, bool isLineWay)
136         {
137             string result = string.Empty;
138             using (FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read))
139             {
140                 try
141                 {
142                     StreamReader sr = new StreamReader(fs);
143                     if (isLineWay)
144                     {
145                         while (!sr.EndOfStream)
146                         {
147                             result += sr.ReadLine() + "\n";
148                         }
149                     }
150                     else
151                     {
152                         result = sr.ReadToEnd();
153                     }
154                     sr.Close();
155                     fs.Close();
156                 }
157                 catch (Exception ee)
158                 {
159                     throw ee;
160                 }
161             }
162             return result;
163         }
164         #endregion
复制代码
posted on 2016-08-25 22:56  張暁磊  阅读(154)  评论(0编辑  收藏  举报