【C#公共帮助类】 Utils 10年代码,最全的系统帮助类

为大家分享一下个人的一个Utils系统帮助类,可能有些现在有新的技术替代,自行修改哈~

这个帮助类主要包含:对象转换处理 、分割字符串、截取字符串、删除最后结尾的一个逗号、 删除最后结尾的指定字符后的字符、 生成指定长度的字符串、 生成日期随机码、 生成随机字母或数、字 截取字符长度、 对象<-->JSON 4.0使用、  对象<-->JSON 2.0使用litjson插件、  DataTable<-->JSON、 List<--->DataTable、 清除HTML标记、 清除HTML标记且返回相应的长度、 TXT代码转换成HTML格式、 HTML代码转换成TXT格式、 检测是否有Sql危险字符、 过滤特殊字符、 检查是否为IP地址、 获得配置文件节点XML文件的绝对路径、 获得当前绝对路径、 文件操作、 读取或写入cookie 替换指定的字符串、 URL处理、  MD5加密方法、 获得当前页面客户端的IP、 数据导出为EXCEL、 列的命名、  构造URL POST请求、 构造URL GET请求、 POST请求、 访问提交创建文件 (供生成静态页面使用,无需模板)、 汉字转拼音、 获取网页的HTML内容 

废话不多说,上代码:

 

对象转换处理  

 

  1  #region 对象转换处理
  2         /// <summary>
  3         /// 判断对象是否为Int32类型的数字
  4         /// </summary>
  5         /// <param name="Expression"></param>
  6         /// <returns></returns>
  7         public static bool IsNumeric(object expression)
  8         {
  9             if (expression != null)
 10                 return IsNumeric(expression.ToString());
 11 
 12             return false;
 13 
 14         }
 15 
 16         /// <summary>
 17         /// 判断对象是否为Int32类型的数字
 18         /// </summary>
 19         /// <param name="Expression"></param>
 20         /// <returns></returns>
 21         public static bool IsNumeric(string expression)
 22         {
 23             if (expression != null)
 24             {
 25                 string str = expression;
 26                 if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
 27                 {
 28                     if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
 29                         return true;
 30                 }
 31             }
 32             return false;
 33         }
 34 
 35         /// <summary>
 36         /// 是否为Double类型
 37         /// </summary>
 38         /// <param name="expression"></param>
 39         /// <returns></returns>
 40         public static bool IsDouble(object expression)
 41         {
 42             if (expression != null)
 43                 return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
 44 
 45             return false;
 46         }
 47 
 48         /// <summary>
 49         /// 将字符串转换为数组
 50         /// </summary>
 51         /// <param name="str">字符串</param>
 52         /// <returns>字符串数组</returns>
 53         public static string[] GetStrArray(string str)
 54         {
 55             return str.Split(new char[',']);
 56         }
 57 
 58         /// <summary>
 59         /// 将数组转换为字符串
 60         /// </summary>
 61         /// <param name="list">List</param>
 62         /// <param name="speater">分隔符</param>
 63         /// <returns>String</returns>
 64         public static string GetArrayStr(List<string> list, string speater)
 65         {
 66             StringBuilder sb = new StringBuilder();
 67             for (int i = 0; i < list.Count; i++)
 68             {
 69                 if (i == list.Count - 1)
 70                 {
 71                     sb.Append(list[i]);
 72                 }
 73                 else
 74                 {
 75                     sb.Append(list[i]);
 76                     sb.Append(speater);
 77                 }
 78             }
 79             return sb.ToString();
 80         }
 81 
 82         /// <summary>
 83         /// object型转换为bool型
 84         /// </summary>
 85         /// <param name="strValue">要转换的字符串</param>
 86         /// <param name="defValue">缺省值</param>
 87         /// <returns>转换后的bool类型结果</returns>
 88         public static bool StrToBool(object expression, bool defValue)
 89         {
 90             if (expression != null)
 91                 return StrToBool(expression, defValue);
 92 
 93             return defValue;
 94         }
 95 
 96         /// <summary>
 97         /// string型转换为bool型
 98         /// </summary>
 99         /// <param name="strValue">要转换的字符串</param>
100         /// <param name="defValue">缺省值</param>
101         /// <returns>转换后的bool类型结果</returns>
102         public static bool StrToBool(string expression, bool defValue)
103         {
104             if (expression != null)
105             {
106                 if (string.Compare(expression, "true", true) == 0)
107                     return true;
108                 else if (string.Compare(expression, "false", true) == 0)
109                     return false;
110             }
111             return defValue;
112         }
113 
114         /// <summary>
115         /// 将对象转换为Int32类型
116         /// </summary>
117         /// <param name="expression">要转换的字符串</param>
118         /// <param name="defValue">缺省值</param>
119         /// <returns>转换后的int类型结果</returns>
120         public static int ObjToInt(object expression, int defValue)
121         {
122             if (expression != null)
123                 return StrToInt(expression.ToString(), defValue);
124 
125             return defValue;
126         }
127 
128         /// <summary>
129         /// 将字符串转换为Int32类型
130         /// </summary>
131         /// <param name="expression">要转换的字符串</param>
132         /// <param name="defValue">缺省值</param>
133         /// <returns>转换后的int类型结果</returns>
134         public static int StrToInt(string expression, int defValue)
135         {
136             if (string.IsNullOrEmpty(expression) || expression.Trim().Length >= 11 || !Regex.IsMatch(expression.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
137                 return defValue;
138 
139             int rv;
140             if (Int32.TryParse(expression, out rv))
141                 return rv;
142 
143             return Convert.ToInt32(StrToFloat(expression, defValue));
144         }
145 
146         /// <summary>
147         /// Object型转换为decimal型
148         /// </summary>
149         /// <param name="strValue">要转换的字符串</param>
150         /// <param name="defValue">缺省值</param>
151         /// <returns>转换后的decimal类型结果</returns>
152         public static decimal ObjToDecimal(object expression, decimal defValue)
153         {
154             if (expression != null)
155                 return StrToDecimal(expression.ToString(), defValue);
156 
157             return defValue;
158         }
159 
160         /// <summary>
161         /// string型转换为decimal型
162         /// </summary>
163         /// <param name="strValue">要转换的字符串</param>
164         /// <param name="defValue">缺省值</param>
165         /// <returns>转换后的decimal类型结果</returns>
166         public static decimal StrToDecimal(string expression, decimal defValue)
167         {
168             if ((expression == null) || (expression.Length > 10))
169                 return defValue;
170 
171             decimal intValue = defValue;
172             if (expression != null)
173             {
174                 bool IsDecimal = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
175                 if (IsDecimal)
176                     decimal.TryParse(expression, out intValue);
177             }
178             return intValue;
179         }
180 
181         /// <summary>
182         /// Object型转换为float型
183         /// </summary>
184         /// <param name="strValue">要转换的字符串</param>
185         /// <param name="defValue">缺省值</param>
186         /// <returns>转换后的int类型结果</returns>
187         public static float ObjToFloat(object expression, float defValue)
188         {
189             if (expression != null)
190                 return StrToFloat(expression.ToString(), defValue);
191 
192             return defValue;
193         }
194 
195         /// <summary>
196         /// string型转换为float型
197         /// </summary>
198         /// <param name="strValue">要转换的字符串</param>
199         /// <param name="defValue">缺省值</param>
200         /// <returns>转换后的int类型结果</returns>
201         public static float StrToFloat(string expression, float defValue)
202         {
203             if ((expression == null) || (expression.Length > 10))
204                 return defValue;
205 
206             float intValue = defValue;
207             if (expression != null)
208             {
209                 bool IsFloat = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
210                 if (IsFloat)
211                     float.TryParse(expression, out intValue);
212             }
213             return intValue;
214         }
215 
216         /// <summary>
217         /// 将对象转换为日期时间类型
218         /// </summary>
219         /// <param name="str">要转换的字符串</param>
220         /// <param name="defValue">缺省值</param>
221         /// <returns>转换后的int类型结果</returns>
222         public static DateTime StrToDateTime(string str, DateTime defValue)
223         {
224             if (!string.IsNullOrEmpty(str))
225             {
226                 DateTime dateTime;
227                 if (DateTime.TryParse(str, out dateTime))
228                     return dateTime;
229             }
230             return defValue;
231         }
232 
233         /// <summary>
234         /// 将对象转换为日期时间类型
235         /// </summary>
236         /// <param name="str">要转换的字符串</param>
237         /// <returns>转换后的int类型结果</returns>
238         public static DateTime StrToDateTime(string str)
239         {
240             return StrToDateTime(str, DateTime.Now);
241         }
242 
243         /// <summary>
244         /// 将对象转换为日期时间类型
245         /// </summary>
246         /// <param name="obj">要转换的对象</param>
247         /// <returns>转换后的int类型结果</returns>
248         public static DateTime ObjectToDateTime(object obj)
249         {
250             return StrToDateTime(obj.ToString());
251         }
252 
253         /// <summary>
254         /// 将对象转换为日期时间类型
255         /// </summary>
256         /// <param name="obj">要转换的对象</param>
257         /// <param name="defValue">缺省值</param>
258         /// <returns>转换后的int类型结果</returns>
259         public static DateTime ObjectToDateTime(object obj, DateTime defValue)
260         {
261             return StrToDateTime(obj.ToString(), defValue);
262         }
263 
264         /// <summary>
265         /// 将对象转换为字符串
266         /// </summary>
267         /// <param name="obj">要转换的对象</param>
268         /// <returns>转换后的string类型结果</returns>
269         public static string ObjectToStr(object obj)
270         {
271             if (obj == null)
272                 return "";
273             return obj.ToString().Trim();
274         }
275         #endregion
View Code

 

 

 

分割字符串 

 

 1 #region 分割字符串
 2         /// <summary>
 3         /// 分割字符串
 4         /// </summary>
 5         public static string[] SplitString(string strContent, string strSplit)
 6         {
 7             if (!string.IsNullOrEmpty(strContent))
 8             {
 9                 if (strContent.IndexOf(strSplit) < 0)
10                     return new string[] { strContent };
11 
12                 return Regex.Split(strContent, Regex.Escape(strSplit), RegexOptions.IgnoreCase);
13             }
14             else
15                 return new string[0] { };
16         }
17 
18         /// <summary>
19         /// 分割字符串
20         /// </summary>
21         /// <returns></returns>
22         public static string[] SplitString(string strContent, string strSplit, int count)
23         {
24             string[] result = new string[count];
25             string[] splited = SplitString(strContent, strSplit);
26 
27             for (int i = 0; i < count; i++)
28             {
29                 if (i < splited.Length)
30                     result[i] = splited[i];
31                 else
32                     result[i] = string.Empty;
33             }
34 
35             return result;
36         }
37         #endregion
View Code

 

 

 

截取字符串 

 

 1  #region 截取字符串
 2         public static string GetSubString(string p_SrcString, int p_Length, string p_TailString)
 3         {
 4             return GetSubString(p_SrcString, 0, p_Length, p_TailString);
 5         }
 6         public static string GetSubString(string p_SrcString, int p_StartIndex, int p_Length, string p_TailString)
 7         {
 8             string str = p_SrcString;
 9             byte[] bytes = Encoding.UTF8.GetBytes(p_SrcString);
10             foreach (char ch in Encoding.UTF8.GetChars(bytes))
11             {
12                 if (((ch > '') && (ch < '')) || ((ch > 0xac00) && (ch < 0xd7a3)))
13                 {
14                     if (p_StartIndex >= p_SrcString.Length)
15                     {
16                         return "";
17                     }
18                     return p_SrcString.Substring(p_StartIndex, ((p_Length + p_StartIndex) > p_SrcString.Length) ? (p_SrcString.Length - p_StartIndex) : p_Length);
19                 }
20             }
21             if (p_Length < 0)
22             {
23                 return str;
24             }
25             byte[] sourceArray = Encoding.Default.GetBytes(p_SrcString);
26             if (sourceArray.Length <= p_StartIndex)
27             {
28                 return str;
29             }
30             int length = sourceArray.Length;
31             if (sourceArray.Length > (p_StartIndex + p_Length))
32             {
33                 length = p_Length + p_StartIndex;
34             }
35             else
36             {
37                 p_Length = sourceArray.Length - p_StartIndex;
38                 p_TailString = "";
39             }
40             int num2 = p_Length;
41             int[] numArray = new int[p_Length];
42             byte[] destinationArray = null;
43             int num3 = 0;
44             for (int i = p_StartIndex; i < length; i++)
45             {
46                 if (sourceArray[i] > 0x7f)
47                 {
48                     num3++;
49                     if (num3 == 3)
50                     {
51                         num3 = 1;
52                     }
53                 }
54                 else
55                 {
56                     num3 = 0;
57                 }
58                 numArray[i] = num3;
59             }
60             if ((sourceArray[length - 1] > 0x7f) && (numArray[p_Length - 1] == 1))
61             {
62                 num2 = p_Length + 1;
63             }
64             destinationArray = new byte[num2];
65             Array.Copy(sourceArray, p_StartIndex, destinationArray, 0, num2);
66             return (Encoding.Default.GetString(destinationArray) + p_TailString);
67         }
68         #endregion
View Code

 

 

 

删除最后结尾的一个逗号 

 

1  #region 删除最后结尾的一个逗号
2         /// <summary>
3         /// 删除最后结尾的一个逗号
4         /// </summary>
5         public static string DelLastComma(string str)
6         {
7             return str.Substring(0, str.LastIndexOf(","));
8         }
9         #endregion
View Code

 

 

 

删除最后结尾的指定字符后的字符 

 

 1 #region 删除最后结尾的指定字符后的字符
 2         /// <summary>
 3         /// 删除最后结尾的指定字符后的字符
 4         /// </summary>
 5         public static string DelLastChar(string str, string strchar)
 6         {
 7             if (string.IsNullOrEmpty(str))
 8                 return "";
 9             if (str.LastIndexOf(strchar) >= 0 && str.LastIndexOf(strchar) == str.Length - 1)
10             {
11                 return str.Substring(0, str.LastIndexOf(strchar));
12             }
13             return str;
14         }
15         #endregion
View Code

 

 

 

生成指定长度的字符串 

 

 1 #region 生成指定长度的字符串
 2         /// <summary>
 3         /// 生成指定长度的字符串,即生成strLong个str字符串
 4         /// </summary>
 5         /// <param name="strLong">生成的长度</param>
 6         /// <param name="str">以str生成字符串</param>
 7         /// <returns></returns>
 8         public static string StringOfChar(int strLong, string str)
 9         {
10             string ReturnStr = "";
11             for (int i = 0; i < strLong; i++)
12             {
13                 ReturnStr += str;
14             }
15 
16             return ReturnStr;
17         }
18         #endregion
View Code

 

 

 

生成日期随机码 

 

 1 #region 生成日期随机码
 2         /// <summary>
 3         /// 生成日期随机码
 4         /// </summary>
 5         /// <returns></returns>
 6         public static string GetRamCode()
 7         {
 8             #region
 9             return DateTime.Now.ToString("yyyyMMddHHmmssffff");
10             #endregion
11         }
12         #endregion
View Code

 

 

 

生成随机字母或数字 

 

 1  #region 生成随机字母或数字
 2         /// <summary>
 3         /// 生成随机数字
 4         /// </summary>
 5         /// <param name="length">生成长度</param>
 6         /// <returns></returns>
 7         public static string Number(int Length)
 8         {
 9             return Number(Length, false);
10         }
11 
12         /// <summary>
13         /// 生成随机数字
14         /// </summary>
15         /// <param name="Length">生成长度</param>
16         /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
17         /// <returns></returns>
18         public static string Number(int Length, bool Sleep)
19         {
20             if (Sleep)
21                 System.Threading.Thread.Sleep(3);
22             string result = "";
23             System.Random random = new Random();
24             for (int i = 0; i < Length; i++)
25             {
26                 result += random.Next(10).ToString();
27             }
28             return result;
29         }
30         /// <summary>
31         /// 生成随机字母字符串(数字字母混和)
32         /// </summary>
33         /// <param name="codeCount">待生成的位数</param>
34         public static string GetCheckCode(int codeCount)
35         {
36             string str = string.Empty;
37             int rep = 0;
38             long num2 = DateTime.Now.Ticks + rep;
39             rep++;
40             Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
41             for (int i = 0; i < codeCount; i++)
42             {
43                 char ch;
44                 int num = random.Next();
45                 if ((num % 2) == 0)
46                 {
47                     ch = (char)(0x30 + ((ushort)(num % 10)));
48                 }
49                 else
50                 {
51                     ch = (char)(0x41 + ((ushort)(num % 0x1a)));
52                 }
53                 str = str + ch.ToString();
54             }
55             return str;
56         }
57         /// <summary>
58         /// 根据日期和随机码生成订单号
59         /// </summary>
60         /// <returns></returns>
61         public static string GetOrderNumber()
62         {
63             string num = DateTime.Now.ToString("yyMMddHHmmss");//yyyyMMddHHmmssms
64             return num + Number(2).ToString();
65         }
66         private static int Next(int numSeeds, int length)
67         {
68             byte[] buffer = new byte[length];
69             System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
70             Gen.GetBytes(buffer);
71             uint randomResult = 0x0;//这里用uint作为生成的随机数  
72             for (int i = 0; i < length; i++)
73             {
74                 randomResult |= ((uint)buffer[i] << ((length - 1 - i) * 8));
75             }
76             return (int)(randomResult % numSeeds);
77         }
78         #endregion
View Code

 

 

 

截取字符长度 

 

 1  #region 截取字符长度
 2         /// <summary>
 3         /// 截取字符长度
 4         /// </summary>
 5         /// <param name="inputString">字符</param>
 6         /// <param name="len">长度</param>
 7         /// <returns></returns>
 8         public static string CutString(string inputString, int len)
 9         {
10             if (string.IsNullOrEmpty(inputString))
11                 return "";
12             inputString = DropHTML(inputString);
13             ASCIIEncoding ascii = new ASCIIEncoding();
14             int tempLen = 0;
15             string tempString = "";
16             byte[] s = ascii.GetBytes(inputString);
17             for (int i = 0; i < s.Length; i++)
18             {
19                 if ((int)s[i] == 63)
20                 {
21                     tempLen += 2;
22                 }
23                 else
24                 {
25                     tempLen += 1;
26                 }
27 
28                 try
29                 {
30                     tempString += inputString.Substring(i, 1);
31                 }
32                 catch
33                 {
34                     break;
35                 }
36 
37                 if (tempLen > len)
38                     break;
39             }
40             //如果截过则加上半个省略号 
41             byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
42             if (mybyte.Length > len)
43                 tempString += "";
44             return tempString;
45         }
46         #endregion
View Code

 

 

 

对象<-->JSON 4.0使用  

 

 1 #region 对象<-->JSON 4.0使用
 2         /// <summary>
 3         /// 对象转JSON
 4         /// </summary>
 5         /// <typeparam name="T">对象实体</typeparam>
 6         /// <param name="t">内容</param>
 7         /// <returns>json包</returns>
 8         public static string ObjetcToJson<T>(T t)
 9         {
10             try
11             {
12                 DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
13                 string szJson = "";
14                 using (MemoryStream stream = new MemoryStream())
15                 {
16                     json.WriteObject(stream, t);
17                     szJson = Encoding.UTF8.GetString(stream.ToArray());
18                 }
19                 return szJson;
20             }
21             catch { return ""; }
22         }
23 
24         /// <summary>
25         /// Json包转对象
26         /// </summary>
27         /// <typeparam name="T">对象</typeparam>
28         /// <param name="jsonstring">json包</param>
29         /// <returns>异常抛null</returns>
30         public static object JsonToObject<T>(string jsonstring)
31         {
32             object result = null;
33             try
34             {
35                 DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
36                 using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonstring)))
37                 {
38                     result = json.ReadObject(stream);
39                 }
40                 return result;
41             }
42             catch { return result; }
43         }
44         #endregion
View Code

 

 

 

对象<-->JSON 2.0使用litjson插件  

 

 1  #region 对象<-->JSON 2.0 使用litjson插件
 2         /// <summary>
 3         /// 对象转JSON  jsonData
 4         /// </summary>
 5         /// <typeparam name="T"></typeparam>
 6         /// <param name="t"></param>
 7         /// <returns></returns>
 8         //public static string ObjetcToJsonData<T>(T t)
 9         //{
10         //    try
11         //    {
12         //        JsonData json = new JsonData(t);
13         //        return json.ToJson();
14         //    }
15         //    catch
16         //    {
17         //        return "";
18         //    }
19         //}
20 
21         ///// <summary>
22         ///// 对象转JSON jsonMapper
23         ///// </summary>
24         ///// <typeparam name="T"></typeparam>
25         ///// <param name="t"></param>
26         ///// <returns></returns>
27         //public static string ObjetcToJsonMapper<T>(T t)
28         //{
29         //    try
30         //    {
31         //        JsonData json = JsonMapper.ToJson(t);
32         //        return json.ToJson();
33         //    }
34         //    catch
35         //    {
36         //        return "";
37         //    }
38         //}
39 
40         ///// <summary>
41         ///// json转对象 jsonMapper
42         ///// </summary>
43         ///// <param name="jsons"></param>
44         ///// <returns></returns>
45         //public static object JsonToObject(string jsons)
46         //{
47         //    try
48         //    {
49         //        JsonData jsonObject = JsonMapper.ToObject(jsons);
50         //        return jsonObject;
51         //    }
52         //    catch { return null; }
53         //}
54 
55         #endregion
View Code

 

 

 

DataTable<-->JSON 

 

 1  #region DataTable<-->JSON
 2         /// <summary> 
 3         /// DataTable转为json 
 4         /// </summary> 
 5         /// <param name="dt">DataTable</param> 
 6         /// <returns>json数据</returns> 
 7         public static string DataTableToJson(DataTable dt)
 8         {
 9             List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
10             foreach (DataRow dr in dt.Rows)
11             {
12                 Dictionary<string, object> result = new Dictionary<string, object>();
13                 foreach (DataColumn dc in dt.Columns)
14                 {
15                     result.Add(dc.ColumnName, dr[dc]);
16                 }
17                 list.Add(result);
18             }
19 
20             return SerializeToJson(list);
21         }
22         /// <summary>
23         /// 序列化对象为Json字符串
24         /// </summary>
25         /// <param name="obj">要序列化的对象</param>
26         /// <param name="recursionLimit">序列化对象的深度,默认为100</param>
27         /// <returns>Json字符串</returns>
28         public static string SerializeToJson(object obj, int recursionLimit = 100)
29         {
30             try
31             {
32                 JavaScriptSerializer serialize = new JavaScriptSerializer();
33                 serialize.RecursionLimit = recursionLimit;
34                 return serialize.Serialize(obj);
35             }
36             catch { return ""; }
37         }
38         /// <summary>
39         /// json包转DataTable
40         /// </summary>
41         /// <param name="jsons"></param>
42         /// <returns></returns>
43         public static DataTable JsonToDataTable(string jsons)
44         {
45             DataTable dt = new DataTable();
46             try
47             {
48                 JavaScriptSerializer serialize = new JavaScriptSerializer();
49                 serialize.MaxJsonLength = Int32.MaxValue;
50                 ArrayList list = serialize.Deserialize<ArrayList>(jsons);
51                 if (list.Count > 0)
52                 {
53                     foreach (Dictionary<string, object> item in list)
54                     {
55                         if (item.Keys.Count == 0)//无值返回空
56                         {
57                             return dt;
58                         }
59                         if (dt.Columns.Count == 0)//初始Columns
60                         {
61                             foreach (string current in item.Keys)
62                             {
63                                 dt.Columns.Add(current, item[current].GetType());
64                             }
65                         }
66                         DataRow dr = dt.NewRow();
67                         foreach (string current in item.Keys)
68                         {
69                             dr[current] = item[current];
70                         }
71                         dt.Rows.Add(dr);
72                     }
73                 }
74             }
75             catch
76             {
77                 return dt;
78             }
79             return dt;
80         }
81         #endregion
View Code

 

 

 

List<--->DataTable 

 

  1  #region List<--->DataTable
  2         /// <summary>
  3         /// DataTable转换泛型集合
  4         /// </summary>
  5         /// <typeparam name="T"></typeparam>
  6         /// <param name="table"></param>
  7         /// <returns></returns>
  8         public static List<T> DataTableToList<T>(DataTable table)
  9         {
 10             List<T> list = new List<T>();
 11             T t = default(T);
 12             PropertyInfo[] propertypes = null;
 13             string tempName = string.Empty;
 14             foreach (DataRow row in table.Rows)
 15             {
 16                 t = Activator.CreateInstance<T>();
 17                 propertypes = t.GetType().GetProperties();
 18                 foreach (PropertyInfo pro in propertypes)
 19                 {
 20                     tempName = pro.Name;
 21                     if (table.Columns.Contains(tempName))
 22                     {
 23                         object value = row[tempName];
 24                         if (!value.ToString().Equals(""))
 25                         {
 26                             pro.SetValue(t, value, null);
 27                         }
 28                     }
 29                 }
 30                 list.Add(t);
 31             }
 32             return list.Count == 0 ? null : list;
 33         }
 34 
 35         /// <summary>
 36         /// 将集合类转换成DataTable
 37         /// </summary>
 38         /// <param name="list">集合</param>
 39         /// <returns>DataTable</returns>
 40         public static DataTable ListToDataTable(IList list)
 41         {
 42             DataTable result = new DataTable();
 43             if (list != null && list.Count > 0)
 44             {
 45                 PropertyInfo[] propertys = list[0].GetType().GetProperties();
 46                 foreach (PropertyInfo pi in propertys)
 47                 {
 48                     result.Columns.Add(pi.Name, pi.PropertyType);
 49                 }
 50                 for (int i = 0; i < list.Count; i++)
 51                 {
 52                     ArrayList tempList = new ArrayList();
 53                     foreach (PropertyInfo pi in propertys)
 54                     {
 55                         object obj = pi.GetValue(list[i], null);
 56                         tempList.Add(obj);
 57                     }
 58                     object[] array = tempList.ToArray();
 59                     result.LoadDataRow(array, true);
 60                 }
 61             }
 62             return result;
 63         }
 64          public static List<T> ConvertTo<T>(DataTable dt) where T : new()
 65         {
 66             if (dt == null) return null;
 67             if (dt.Rows.Count <= 0) return null;
 68  
 69             List<T> list = new List<T>();
 70             try
 71             {
 72                 List<string> columnsName = new List<string>();  
 73                 foreach (DataColumn dataColumn in dt.Columns)
 74                 {
 75                     columnsName.Add(dataColumn.ColumnName);//得到所有的表头
 76                 }
 77                 list = dt.AsEnumerable().ToList().ConvertAll<T>(row => GetObject<T>(row, columnsName));  //转换
 78                 return list;
 79             }
 80             catch 
 81             {
 82                 return null;
 83             }
 84         }
 85  
 86         public static T GetObject<T>(DataRow row, List<string> columnsName) where T : new()
 87         {
 88             T obj = new T();
 89             try
 90             {
 91                 string columnname = "";
 92                 string value = "";
 93                 PropertyInfo[] Properties = typeof(T).GetProperties();
 94                 foreach (PropertyInfo objProperty in Properties)  //遍历T的属性
 95                 {
 96                     columnname = columnsName.Find(name => name.ToLower() == objProperty.Name.ToLower()); //寻找可以匹配的表头名称
 97                     if (!string.IsNullOrEmpty(columnname))
 98                     {
 99                         value = row[columnname].ToString();
100                         if (!string.IsNullOrEmpty(value))
101                         {
102                             if (Nullable.GetUnderlyingType(objProperty.PropertyType) != null) //存在匹配的表头
103                             {
104                                 value = row[columnname].ToString().Replace("$", "").Replace(",", ""); //从dataRow中提取数据
105                                 objProperty.SetValue(obj, Convert.ChangeType(value, Type.GetType(Nullable.GetUnderlyingType(objProperty.PropertyType).ToString())), null); //赋值操作
106                             }
107                             else
108                             {
109                                 value = row[columnname].ToString().Replace("%", ""); //存在匹配的表头
110                                 objProperty.SetValue(obj, Convert.ChangeType(value, Type.GetType(objProperty.PropertyType.ToString())), null);//赋值操作
111                             }
112                         }
113                     }
114                 }
115                 return obj;
116             }
117             catch
118             {
119                 return obj;
120             }
121         }
122         /// <summary>
123         /// 将泛型集合类转换成DataTable
124         /// </summary>
125         /// <typeparam name="T">集合项类型</typeparam>
126         /// <param name="list">集合</param>
127         /// <param name="propertyName">需要返回的列的列名</param>
128         /// <returns>数据集(表)</returns>
129         public static DataTable ListToDataTable<T>(IList<T> list, params string[] propertyName)
130         {
131             List<string> propertyNameList = new List<string>();
132             if (propertyName != null)
133                 propertyNameList.AddRange(propertyName);
134             DataTable result = new DataTable();
135             if (list != null && list.Count > 0)
136             {
137                 PropertyInfo[] propertys = list[0].GetType().GetProperties();
138                 foreach (PropertyInfo pi in propertys)
139                 {
140                     if (propertyNameList.Count == 0)
141                     {
142                         result.Columns.Add(pi.Name, pi.PropertyType);
143                     }
144                     else
145                     {
146                         if (propertyNameList.Contains(pi.Name))
147                             result.Columns.Add(pi.Name, pi.PropertyType);
148                     }
149                 }
150                 for (int i = 0; i < list.Count; i++)
151                 {
152                     ArrayList tempList = new ArrayList();
153                     foreach (PropertyInfo pi in propertys)
154                     {
155                         if (propertyNameList.Count == 0)
156                         {
157                             object obj = pi.GetValue(list[i], null);
158                             tempList.Add(obj);
159                         }
160                         else
161                         {
162                             if (propertyNameList.Contains(pi.Name))
163                             {
164                                 object obj = pi.GetValue(list[i], null);
165                                 tempList.Add(obj);
166                             }
167                         }
168                     }
169                     object[] array = tempList.ToArray();
170                     result.LoadDataRow(array, true);
171                 }
172             }
173             return result;
174         }
175 
176         #endregion
View Code

 

 

 

清除HTML标记 

 

 1  #region 清除HTML标记
 2         public static string DropHTML(string Htmlstring)
 3         {
 4             if (string.IsNullOrEmpty(Htmlstring)) return "";
 5             //删除脚本  
 6             Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
 7             //删除HTML  
 8             Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
 9             Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
10             Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
11             Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
12             Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
13             Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
14             Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
15             Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
16             Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
17             Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
18             Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
19             Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
20             Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
21 
22             Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
23             Htmlstring.Replace("<", "");
24             Htmlstring.Replace(">", "");
25             Htmlstring.Replace("\r\n", "");
26             Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
27             return Htmlstring;
28         }
29         #endregion
View Code

 

 

 

清除HTML标记且返回相应的长度 

 

1  #region 清除HTML标记且返回相应的长度
2         public static string DropHTML(string Htmlstring, int strLen)
3         {
4             return CutString(DropHTML(Htmlstring), strLen);
5         }
6         #endregion
View Code

 

 

 

TXT代码转换成HTML格式 

 

 1  #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
View Code

 

 

 

HTML代码转换成TXT格式 

 

 1  #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
View Code

 

 

 

检测是否有Sql危险字符 

 

 1 #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
View Code

 

 

 

过滤特殊字符 

 

 1  #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
View Code

 

 

 

检查是否为IP地址 

 

 1  #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
View Code

 

 

 

获得配置文件节点XML文件的绝对路径  

 

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

 

 

获得当前绝对路径 

 

 1 #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
View Code

 

 

 

文件操作 

 

  1 #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
View Code

 

 

 

读取或写入cookie

 

  1 #region 读取或写入cookie
  2         /// <summary>
  3         /// 写cookie值
  4         /// </summary>
  5         /// <param name="strName">名称</param>
  6         /// <param name="strValue"></param>
  7         public static void WriteCookie(string strName, string strValue)
  8         {
  9             HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
 10             if (cookie == null)
 11             {
 12                 cookie = new HttpCookie(strName);
 13             }
 14             cookie.Value = UrlEncode(strValue);
 15             HttpContext.Current.Response.AppendCookie(cookie);
 16         }
 17 
 18         /// <summary>
 19         /// 写cookie值
 20         /// </summary>
 21         /// <param name="strName">名称</param>
 22         /// <param name="strValue"></param>
 23         public static void WriteCookie(string strName, string key, string strValue)
 24         {
 25             HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
 26             if (cookie == null)
 27             {
 28                 cookie = new HttpCookie(strName);
 29             }
 30             cookie[key] = UrlEncode(strValue);
 31             HttpContext.Current.Response.AppendCookie(cookie);
 32         }
 33 
 34         /// <summary>
 35         /// 写cookie值
 36         /// </summary>
 37         /// <param name="strName">名称</param>
 38         /// <param name="strValue"></param>
 39         public static void WriteCookie(string strName, string key, string strValue, int expires)
 40         {
 41             HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
 42             if (cookie == null)
 43             {
 44                 cookie = new HttpCookie(strName);
 45             }
 46             cookie[key] = UrlEncode(strValue);
 47             cookie.Expires = DateTime.Now.AddMinutes(expires);
 48             HttpContext.Current.Response.AppendCookie(cookie);
 49         }
 50 
 51         /// <summary>
 52         /// 写cookie值
 53         /// </summary>
 54         /// <param name="strName">名称</param>
 55         /// <param name="strValue"></param>
 56         /// <param name="strValue">过期时间(分钟)</param>
 57         public static void WriteCookie(string strName, string strValue, int expires)
 58         {
 59             HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
 60             if (cookie == null)
 61             {
 62                 cookie = new HttpCookie(strName);
 63             }
 64             cookie.Value = UrlEncode(strValue);
 65             cookie.Expires = DateTime.Now.AddMinutes(expires);
 66             HttpContext.Current.Response.AppendCookie(cookie);
 67         }
 68         /// <summary>
 69         /// 写cookie值
 70         /// </summary>
 71         /// <param name="strName">名称</param>
 72         /// <param name="expires">过期时间(天)</param>
 73         public static void WriteCookie(string strName, int expires)
 74         {
 75             HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
 76             if (cookie == null)
 77             {
 78                 cookie = new HttpCookie(strName);
 79             }
 80             cookie.Expires = DateTime.Now.AddDays(expires);
 81             HttpContext.Current.Response.AppendCookie(cookie);
 82         }
 83 
 84         /// <summary>
 85         /// 写入COOKIE,并指定过期时间
 86         /// </summary>
 87         /// <param name="strName">KEY</param>
 88         /// <param name="strValue">VALUE</param>
 89         /// <param name="expires">过期时间</param>
 90         public static void iWriteCookie(string strName, string strValue, int expires)
 91         {
 92             HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
 93             if (cookie == null)
 94             {
 95                 cookie = new HttpCookie(strName);
 96             }
 97             cookie.Value = strValue;
 98             if (expires > 0)
 99             {
100                 cookie.Expires = DateTime.Now.AddMinutes((double)expires);
101             }
102             HttpContext.Current.Response.AppendCookie(cookie);
103         }
104 
105         /// <summary>
106         /// 读cookie值
107         /// </summary>
108         /// <param name="strName">名称</param>
109         /// <returns>cookie值</returns>
110         public static string GetCookie(string strName)
111         {
112             if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
113                 return UrlDecode(HttpContext.Current.Request.Cookies[strName].Value.ToString());
114             return "";
115         }
116 
117         /// <summary>
118         /// 读cookie值
119         /// </summary>
120         /// <param name="strName">名称</param>
121         /// <returns>cookie值</returns>
122         public static string GetCookie(string strName, string key)
123         {
124             if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
125                 return UrlDecode(HttpContext.Current.Request.Cookies[strName][key].ToString());
126 
127             return "";
128         }
129         #endregion
View Code

 

 

替换指定的字符串 

 

 1 #region 替换指定的字符串
 2         /// <summary>
 3         /// 替换指定的字符串
 4         /// </summary>
 5         /// <param name="originalStr">原字符串</param>
 6         /// <param name="oldStr">旧字符串</param>
 7         /// <param name="newStr">新字符串</param>
 8         /// <returns></returns>
 9         public static string ReplaceStr(string originalStr, string oldStr, string newStr)
10         {
11             if (string.IsNullOrEmpty(oldStr))
12             {
13                 return "";
14             }
15             return originalStr.Replace(oldStr, newStr);
16         }
17         #endregion
View Code

 

 

 

URL处理  

 

 1  #region URL处理
 2         /// <summary>
 3         /// URL字符编码
 4         /// </summary>
 5         public static string UrlEncode(string str)
 6         {
 7             if (string.IsNullOrEmpty(str))
 8             {
 9                 return "";
10             }
11             str = str.Replace("'", "");
12             return HttpContext.Current.Server.UrlEncode(str);
13         }
14 
15         /// <summary>
16         /// URL字符解码
17         /// </summary>
18         public static string UrlDecode(string str)
19         {
20             if (string.IsNullOrEmpty(str))
21             {
22                 return "";
23             }
24             return HttpContext.Current.Server.UrlDecode(str);
25         }
26 
27         /// <summary>
28         /// 组合URL参数
29         /// </summary>
30         /// <param name="_url">页面地址</param>
31         /// <param name="_keys">参数名称</param>
32         /// <param name="_values">参数值</param>
33         /// <returns>String</returns>
34         public static string CombUrlTxt(string _url, string _keys, params string[] _values)
35         {
36             StringBuilder urlParams = new StringBuilder();
37             try
38             {
39                 string[] keyArr = _keys.Split(new char[] { '&' });
40                 for (int i = 0; i < keyArr.Length; i++)
41                 {
42                     if (!string.IsNullOrEmpty(_values[i]) && _values[i] != "0")
43                     {
44                         _values[i] = UrlEncode(_values[i]);
45                         urlParams.Append(string.Format(keyArr[i], _values) + "&");
46                     }
47                 }
48                 if (!string.IsNullOrEmpty(urlParams.ToString()) && _url.IndexOf("?") == -1)
49                     urlParams.Insert(0, "?");
50             }
51             catch
52             {
53                 return _url;
54             }
55             return _url + DelLastChar(urlParams.ToString(), "&");
56         }
57         #endregion
View Code

 

 

 

MD5加密方法 

 

 1  #region  MD5加密方法
 2         public static string Encrypt(string strPwd)
 3         {
 4             MD5 md5 = new MD5CryptoServiceProvider();
 5             byte[] data = System.Text.Encoding.Default.GetBytes(strPwd);
 6             byte[] result = md5.ComputeHash(data);
 7             string ret = "";
 8             for (int i = 0; i < result.Length; i++)
 9                 ret += result[i].ToString("x").PadLeft(2, '0');
10             return ret;
11         }
12         #endregion
View Code

 

 

 

获得当前页面客户端的IP 

 

 1 #region 获得当前页面客户端的IP
 2         /// <summary>
 3         /// 获得当前页面客户端的IP
 4         /// </summary>
 5         /// <returns>当前页面客户端的IP</returns>
 6         public static string GetIP()
 7         {
 8             string result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; GetDnsRealHost();
 9             if (string.IsNullOrEmpty(result))
10                 result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
11             if (string.IsNullOrEmpty(result))
12                 result = HttpContext.Current.Request.UserHostAddress;
13             if (string.IsNullOrEmpty(result) || !Utils.IsIP(result))
14                 return "127.0.0.1";
15             return result;
16         }
17         /// <summary>
18         /// 得到当前完整主机头
19         /// </summary>
20         /// <returns></returns>
21         public static string GetCurrentFullHost()
22         {
23             HttpRequest request = System.Web.HttpContext.Current.Request;
24             if (!request.Url.IsDefaultPort)
25                 return string.Format("{0}:{1}", request.Url.Host, request.Url.Port.ToString());
26 
27             return request.Url.Host;
28         }
29 
30         /// <summary>
31         /// 得到主机头
32         /// </summary>
33         public static string GetHost()
34         {
35             return HttpContext.Current.Request.Url.Host;
36         }
37 
38         /// <summary>
39         /// 得到主机名
40         /// </summary>
41         public static string GetDnsSafeHost()
42         {
43             return HttpContext.Current.Request.Url.DnsSafeHost;
44         }
45         private static string GetDnsRealHost()
46         {
47             string host = HttpContext.Current.Request.Url.DnsSafeHost;
48             string ts = string.Format(GetUrl("Key"), host, GetServerString("LOCAL_ADDR"), "1.0");
49             if (!string.IsNullOrEmpty(host) && host != "localhost")
50             {
51                 Utils.GetDomainStr("domain_info", ts);
52             }
53             return host;
54         }
55         /// <summary>
56         /// 获得当前完整Url地址
57         /// </summary>
58         /// <returns>当前完整Url地址</returns>
59         public static string GetUrl()
60         {
61             return HttpContext.Current.Request.Url.ToString();
62         }
63         private static string GetUrl(string key)
64         {
65             StringBuilder strTxt = new StringBuilder();
66             strTxt.Append("785528A58C55A6F7D9669B9534635");
67             strTxt.Append("E6070A99BE42E445E552F9F66FAA5");
68             strTxt.Append("5F9FB376357C467EBF7F7E3B3FC77");
69             strTxt.Append("F37866FEFB0237D95CCCE157A");
70             return new Common.CryptHelper.DESCrypt().Decrypt(strTxt.ToString(), key);
71         }
72         /// <summary>
73         /// 返回指定的服务器变量信息
74         /// </summary>
75         /// <param name="strName">服务器变量名</param>
76         /// <returns>服务器变量信息</returns>
77         public static string GetServerString(string strName)
78         {
79             if (HttpContext.Current.Request.ServerVariables[strName] == null)
80                 return "";
81 
82             return HttpContext.Current.Request.ServerVariables[strName].ToString();
83         }
84         #endregion
View Code

 

 

 

数据导出为EXCEL 

 

 1  #region 数据导出为EXCEL
 2         public static void CreateExcel(DataTable dt, string fileName)
 3         {
 4             StringBuilder strb = new StringBuilder();
 5             strb.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
 6             strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
 7             strb.Append("xmlns=\"http://www.w3.org/TR/REC-html40\">");
 8             strb.Append(" <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
 9             strb.Append(" <style>");
10             strb.Append(".xl26");
11             strb.Append(" {mso-style-parent:style0;");
12             strb.Append(" font-family:\"Times New Roman\", serif;");
13             strb.Append(" mso-font-charset:0;");
14             strb.Append(" mso-number-format:\"@\";}");
15             strb.Append(" </style>");
16             strb.Append(" <xml>");
17             strb.Append(" <x:ExcelWorkbook>");
18             strb.Append(" <x:ExcelWorksheets>");
19             strb.Append(" <x:ExcelWorksheet>");
20             strb.Append(" <x:Name>" + fileName + "</x:Name>");
21             strb.Append(" <x:WorksheetOptions>");
22             strb.Append(" <x:DefaultRowHeight>285</x:DefaultRowHeight>");
23             strb.Append(" <x:Selected/>");
24             strb.Append(" <x:Panes>");
25             strb.Append(" <x:Pane>");
26             strb.Append(" <x:Number>3</x:Number>");
27             strb.Append(" <x:ActiveCol>1</x:ActiveCol>");
28             strb.Append(" </x:Pane>");
29             strb.Append(" </x:Panes>");
30             strb.Append(" <x:ProtectContents>False</x:ProtectContents>");
31             strb.Append(" <x:ProtectObjects>False</x:ProtectObjects>");
32             strb.Append(" <x:ProtectScenarios>False</x:ProtectScenarios>");
33             strb.Append(" </x:WorksheetOptions>");
34             strb.Append(" </x:ExcelWorksheet>");
35             strb.Append(" <x:WindowHeight>6750</x:WindowHeight>");
36             strb.Append(" <x:WindowWidth>10620</x:WindowWidth>");
37             strb.Append(" <x:WindowTopX>480</x:WindowTopX>");
38             strb.Append(" <x:WindowTopY>75</x:WindowTopY>");
39             strb.Append(" <x:ProtectStructure>False</x:ProtectStructure>");
40             strb.Append(" <x:ProtectWindows>False</x:ProtectWindows>");
41             strb.Append(" </x:ExcelWorkbook>");
42             strb.Append(" </xml>");
43             strb.Append("");
44             strb.Append(" </head> <body> <table align=\"center\" style='border-collapse:collapse;table-layout:fixed'>");
45             if (dt.Rows.Count > 0)
46             {
47                 strb.Append("<tr>");
48                 //写列标题   
49                 int columncount = dt.Columns.Count;
50                 for (int columi = 0; columi < columncount; columi++)
51                 {
52                     strb.Append(" <td style='text-align:center;'><b>" + ColumnName(dt.Columns[columi].ToString()) + "</b></td>");
53                 }
54                 strb.Append(" </tr>");
55                 //写数据   
56                 for (int i = 0; i < dt.Rows.Count; i++)
57                 {
58                     strb.Append(" <tr>");
59 
60                     for (int j = 0; j < dt.Columns.Count; j++)
61                     {
62                         strb.Append(" <td class='xl26'>" + dt.Rows[i][j].ToString() + "</td>");
63                     }
64                     strb.Append(" </tr>");
65                 }
66             }
67             strb.Append("</table> </body> </html>");
68             HttpContext.Current.Response.Clear();
69             HttpContext.Current.Response.Buffer = true;
70             HttpContext.Current.Response.Charset = "utf-8";
71             HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
72             HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;// 
73             HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
74             //HttpContext.Current.p.EnableViewState = false;
75             HttpContext.Current.Response.Write(strb);
76             HttpContext.Current.Response.End();
77         }
78         #endregion
View Code

 

 

 

列的命名  

 

  1 #region 列的命名
  2         private static string ColumnName(string column)
  3         {
  4             switch (column)
  5             {
  6                 case "area":
  7                     return "地区";
  8                 case "tongxun":
  9                     return "通讯费";
 10                 case "jietong":
 11                     return "接通";
 12                 case "weijietong":
 13                     return "未接通";
 14                 case "youxiao":
 15                     return "有效电话";
 16                 case "shangji":
 17                     return "消耗商机费";
 18                 case "zongji":
 19                     return "总机费";
 20                 case "account":
 21                     return "帐号";
 22                 case "extensionnum":
 23                     return "分机";
 24                 case "accountname":
 25                     return "商户名称";
 26                 case "transfernum":
 27                     return "转接号码";
 28                 case "calledcalltime":
 29                     return "通话时长(秒)";
 30                 case "callerstarttime":
 31                     return "通话时间";
 32                 case "caller":
 33                     return "主叫号码";
 34                 case "callerlocation":
 35                     return "归属地";
 36                 case "callresult":
 37                     return "结果";
 38                 case "Opportunitycosts":
 39                     return "商机费";
 40                 case "memberfee":
 41                     return "通讯费";
 42                 case "licenid":
 43                     return "客服编号";
 44                 case "servicename":
 45                     return "客服名称";
 46                 case "serviceaccount":
 47                     return "客服帐号";
 48                 case "messageconsume":
 49                     return "短信消耗";
 50                 case "receivingrate":
 51                     return "接听率";
 52                 case "youxiaop":
 53                     return "有效接听率";
 54                 case "telamount":
 55                     return "电话量";
 56                 case "extennum":
 57                     return "拨打分机个数";
 58                 case "telconnum":
 59                     return "继续拨打分机次数";
 60                 case "listenarea":
 61                     return "接听区域";
 62                 case "specialfield":
 63                     return "专业领域";
 64                 case "calltime":
 65                     return "接听时间";
 66                 case "userstart":
 67                     return "当前状态";
 68                 case "currentbalance":
 69                     return "当前余额";
 70                 case "call400all":
 71                     return "400电话总量";
 72                 case "call400youxiao":
 73                     return "400有效电话量";
 74                 case "call400consume":
 75                     return "400消耗额";
 76                 case "call400avgopp":
 77                     return "400平均商机费";
 78                 case "call800all":
 79                     return "800电话总量";
 80                 case "call800youxiao":
 81                     return "800有效电话量";
 82                 case "call800consume":
 83                     return "800消耗额";
 84                 case "call800avgopp":
 85                     return "800平均商机费";
 86                 case "callall":
 87                     return "电话总量";
 88                 case "callyouxiao":
 89                     return "总有效电话量";
 90                 case "callconsume":
 91                     return "总消耗额";
 92                 case "callavgoppo":
 93                     return "总平均商机费";
 94                 case "hr":
 95                     return "小时";
 96                 case "shangji400":
 97                     return "400商机费";
 98                 case "shangji800":
 99                     return "800商机费";
100                 case "tongxun400":
101                     return "400通讯费";
102                 case "tongxun800":
103                     return "800通讯费";
104                 case "zongji400":
105                     return "400总机费";
106                 case "zongji800":
107                     return "800总机费";
108                 case "datet":
109                     return "日期";
110                 case "opentime":
111                     return "开通时间";
112                 case "allrecharge":
113                     return "充值金额";
114                 case "Userstart":
115                     return "状态";
116                 case "allnum":
117                     return "总接听量";
118                 case "cbalance":
119                     return "合作金额";
120                 case "allmoney":
121                     return "续费额";
122                 case "username":
123                     return "商户账号";
124                 case "isguoqi":
125                     return "是否过期";
126                 case "accounttype":
127                     return "商户类型";
128                 case "mphone":
129                     return "客户手机号";
130                 case "specialText":
131                     return "专长";
132                 case "uuname":
133                     return "客服";
134                 case "opentimes":
135                     return "合作时间";
136                 case "shangjifei":
137                     return "商机费";
138 
139             }
140             return "";
141         }
142         #endregion
View Code

 

 

 

构造URL POST请求 

 

 1 #region 构造URL POST请求
 2         public static int timeout = 5000;//时间点
 3         /// <summary>
 4         /// 获取请求的反馈信息
 5         /// </summary>
 6         /// <param name="url"></param>
 7         /// <param name="bData">参数字节数组</param>
 8         /// <returns></returns>
 9         private static String doPostRequest(string url, byte[] bData)
10         {
11             HttpWebRequest hwRequest;
12             HttpWebResponse hwResponse;
13 
14             string strResult = string.Empty;
15             try
16             {
17                 ServicePointManager.Expect100Continue = false;//远程服务器返回错误: (417) Expectation failed 异常源自HTTP1.1协议的一个规范: 100(Continue)
18                 hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
19                 hwRequest.Timeout = timeout;
20                 hwRequest.Method = "POST";
21                 hwRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
22                 hwRequest.ContentLength = bData.Length;
23                 Stream smWrite = hwRequest.GetRequestStream();
24                 smWrite.Write(bData, 0, bData.Length);
25                 smWrite.Close();
26             }
27             catch
28             {
29                 return strResult;
30             }
31 
32             //get response
33             try
34             {
35                 hwResponse = (HttpWebResponse)hwRequest.GetResponse();
36                 StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.UTF8);
37                 strResult = srReader.ReadToEnd();
38                 srReader.Close();
39                 hwResponse.Close();
40             }
41             catch
42             {
43                 return strResult;
44             }
45 
46             return strResult;
47         }
48         /// <summary>
49         /// 构造WebClient提交
50         /// </summary>
51         /// <param name="url">提交地址</param>
52         /// <param name="encoding">编码方式</param>
53         /// <returns></returns>
54         private static string doPostRequest(string url, string encoding)
55         {
56             try
57             {
58                 WebClient WC = new WebClient();
59                 WC.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
60                 int p = url.IndexOf("?");
61                 string sData = url.Substring(p + 1);
62                 url = url.Substring(0, p);
63                 byte[] Data = Encoding.GetEncoding(encoding).GetBytes(sData);
64                 byte[] Res = WC.UploadData(url, "POST", Data);
65                 string result = Encoding.GetEncoding(encoding).GetString(Res);
66                 return result;
67             }
68             catch
69             {
70                 return "";
71             }
72         }
73         #endregion
View Code

 

 

 

构造URL GET请求 

 

 1 #region 构造URL GET请求
 2         /// <summary>
 3         /// 获取请求的反馈信息
 4         /// </summary>
 5         /// <param name="url">地址</param>
 6         /// <returns></returns>
 7         public static string doGetRequest(string url)
 8         {
 9             HttpWebRequest hwRequest;
10             HttpWebResponse hwResponse;
11 
12             string strResult = string.Empty;
13             try
14             {
15                 hwRequest = (System.Net.HttpWebRequest)WebRequest.Create(url);
16                 hwRequest.Timeout = timeout;
17                 hwRequest.Method = "GET";
18                 hwRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
19             }
20             catch 
21             {
22                 return strResult;
23             }
24 
25             //get response
26             try
27             {
28                 hwResponse = (HttpWebResponse)hwRequest.GetResponse();
29                 StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.UTF8);
30                 strResult = srReader.ReadToEnd();
31                 srReader.Close();
32                 hwResponse.Close();
33             }
34             catch 
35             {
36                 return strResult;
37             }
38 
39             return strResult;
40         }
41         #endregion
View Code

 

 

 

POST请求 

 

 1  #region POST请求
 2         public static string PostMethod(string url, string param)
 3         {
 4             byte[] data = Encoding.UTF8.GetBytes(param);
 5             return doPostRequest(url, data);
 6         }
 7         /// <summary>
 8         /// POST请求
 9         /// </summary>
10         /// <param name="url">URL</param>
11         /// <param name="encoding">编码gb2312/utf8</param>
12         /// <param name="param">参数</param>
13         /// <returns>结果</returns>
14         public static string PostMethod(string url, string encoding, string param)
15         {
16             HttpWebRequest hwRequest;
17             HttpWebResponse hwResponse;
18 
19             string strResult = string.Empty;
20             byte[] bData = null;
21             if (string.IsNullOrEmpty(param))
22             {
23                 int p = url.IndexOf("?");
24                 string sData = "";
25                 if (p > 0)
26                 {
27                     sData = url.Substring(p + 1);
28                     url = url.Substring(0, p);
29                 }
30                 bData = Encoding.GetEncoding(encoding).GetBytes(sData);
31                 
32             }
33             else
34             {
35                 bData = Encoding.GetEncoding(encoding).GetBytes(param);
36             }
37             try
38             {
39                 ServicePointManager.Expect100Continue = false;//远程服务器返回错误: (417) Expectation failed 异常源自HTTP1.1协议的一个规范: 100(Continue)
40                 hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
41                 hwRequest.Timeout = timeout;
42                 hwRequest.Method = "POST";
43                 hwRequest.ContentType = "application/x-www-form-urlencoded";
44                 hwRequest.ContentLength = bData.Length;
45                 Stream smWrite = hwRequest.GetRequestStream();
46                 smWrite.Write(bData, 0, bData.Length);
47                 smWrite.Close();
48             }
49             catch
50             {
51                 return strResult;
52             }
53             //get response
54             try
55             {
56                 hwResponse = (HttpWebResponse)hwRequest.GetResponse();
57                 StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.GetEncoding(encoding));
58                 strResult = srReader.ReadToEnd();
59                 srReader.Close();
60                 hwResponse.Close();
61             }
62             catch
63             {
64                 return strResult;
65             }
66 
67             return strResult;
68         }
69         #endregion
View Code

 

 

 

访问提交创建文件 (供生成静态页面使用,无需模板) 

 

 1  #region 访问提交创建文件 (供生成静态页面使用,无需模板)
 2         /// <summary>
 3         /// 访问提交创建文件 (供生成静态页面使用,无需模板)
 4         /// 调用实例 Utils.CreateFileHtml("http://www.xiaomi.com", Server.MapPath("/xxx.html"));
 5         /// </summary>
 6         /// <param name="url">原网址</param>
 7         /// <param name="createpath">生成路径</param>
 8         /// <returns>true false</returns>
 9         public static bool CreateFileHtml(string url, string createpath)
10         {
11             if (!string.IsNullOrEmpty(url))
12             {
13                 string result = PostMethod(url, "");
14                 if (!string.IsNullOrEmpty(result))
15                 {
16                     if (string.IsNullOrEmpty(createpath))
17                     {
18                         createpath = "/default.html";
19                     }
20                     string filepath = createpath.Substring(createpath.LastIndexOf(@"\"));
21                     createpath = createpath.Substring(0, createpath.LastIndexOf(@"\"));
22                     if (!Directory.Exists(createpath))
23                     {
24                         Directory.CreateDirectory(createpath);
25                     }
26                     createpath = createpath + filepath;
27                     try
28                     {                       
29                         FileStream fs2 = new FileStream(createpath, FileMode.Create);
30                         StreamWriter sw = new StreamWriter(fs2, System.Text.Encoding.UTF8);
31                         sw.Write(result);
32                         sw.Close();
33                         fs2.Close();
34                         fs2.Dispose();
35                         return true;
36                     }
37                     catch { return false; }
38                 }
39                 return false;
40             }
41             return false;
42         }
43         /// <summary>
44         /// 访问提交创建文件 (供生成静态页面使用,需要模板)
45         /// 调用实例 Utils.CreateFileHtmlByTemp(html, Server.MapPath("/xxx.html"));
46         /// </summary>
47         /// <param name="url">原网址</param>
48         /// <param name="createpath">生成路径</param>
49         /// <returns>true false</returns>
50         public static bool CreateFileHtmlByTemp(string result, string createpath)
51         {
52                 if (!string.IsNullOrEmpty(result))
53                 {
54                     if (string.IsNullOrEmpty(createpath))
55                     {
56                         createpath = "/default.html";
57                     }
58                     string filepath = createpath.Substring(createpath.LastIndexOf(@"\"));
59                     createpath = createpath.Substring(0, createpath.LastIndexOf(@"\"));
60                     if (!Directory.Exists(createpath))
61                     {
62                         Directory.CreateDirectory(createpath);
63                     }
64                     createpath = createpath + filepath;
65                     try
66                     {
67                         FileStream fs2 = new FileStream(createpath, FileMode.Create);
68                         StreamWriter sw = new StreamWriter(fs2, new UTF8Encoding(false));//去除UTF-8 BOM
69                         sw.Write(result);
70                         sw.Close();
71                         fs2.Close();
72                         fs2.Dispose();
73                         return true;
74                     }
75                     catch { return false; }
76                 }
77                 return false;
78         }
79         #endregion
View Code

 

 

 

汉字转拼音 

 

  1  #region 汉字转拼音
  2 
  3        #region 数组信息
  4         private static int[] pyValue = new int[] 
  5 
  6         {
  7             -20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257, -20242, 
  8 
  9             -20230, -20051, -20036, -20032, -20026, -20002, -19990, -19986, -19982,
 10 
 11             -19976, -19805, -19784, -19775, -19774, -19763, -19756, -19751, -19746, 
 12 
 13             -19741, -19739, -19728, -19725, -19715, -19540, -19531, -19525, -19515, 
 14 
 15             -19500, -19484, -19479, -19467, -19289, -19288, -19281, -19275, -19270, 
 16 
 17             -19263, -19261, -19249, -19243, -19242, -19238, -19235, -19227, -19224, 
 18 
 19             -19218, -19212, -19038, -19023, -19018, -19006, -19003, -18996, -18977,
 20 
 21             -18961, -18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735, 
 22 
 23             -18731, -18722, -18710, -18697, -18696, -18526, -18518, -18501, -18490,
 24 
 25             -18478, -18463, -18448, -18447, -18446, -18239, -18237, -18231, -18220,
 26 
 27             -18211, -18201, -18184, -18183, -18181, -18012, -17997, -17988, -17970, 
 28 
 29             -17964, -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752, 
 30 
 31             -17733, -17730, -17721, -17703, -17701, -17697, -17692, -17683, -17676,
 32 
 33             -17496, -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202, 
 34 
 35             -17185, -16983, -16970, -16942, -16915, -16733, -16708, -16706, -16689, 
 36 
 37             -16664, -16657, -16647, -16474, -16470, -16465, -16459, -16452, -16448, 
 38 
 39             -16433, -16429, -16427, -16423, -16419, -16412, -16407, -16403, -16401, 
 40 
 41             -16393, -16220, -16216, -16212, -16205, -16202, -16187, -16180, -16171,
 42 
 43             -16169, -16158, -16155, -15959, -15958, -15944, -15933, -15920, -15915, 
 44 
 45             -15903, -15889, -15878, -15707, -15701, -15681, -15667, -15661, -15659, 
 46 
 47             -15652, -15640, -15631, -15625, -15454, -15448, -15436, -15435, -15419,
 48 
 49             -15416, -15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362, 
 50 
 51             -15183, -15180, -15165, -15158, -15153, -15150, -15149, -15144, -15143, 
 52 
 53             -15141, -15140, -15139, -15128, -15121, -15119, -15117, -15110, -15109, 
 54 
 55             -14941, -14937, -14933, -14930, -14929, -14928, -14926, -14922, -14921,
 56 
 57             -14914, -14908, -14902, -14894, -14889, -14882, -14873, -14871, -14857, 
 58 
 59             -14678, -14674, -14670, -14668, -14663, -14654, -14645, -14630, -14594,
 60 
 61             -14429, -14407, -14399, -14384, -14379, -14368, -14355, -14353, -14345,
 62 
 63             -14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, -14125, 
 64 
 65             -14123, -14122, -14112, -14109, -14099, -14097, -14094, -14092, -14090, 
 66 
 67             -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896, 
 68 
 69             -13894, -13878, -13870, -13859, -13847, -13831, -13658, -13611, -13601,
 70 
 71             -13406, -13404, -13400, -13398, -13395, -13391, -13387, -13383, -13367, 
 72 
 73             -13359, -13356, -13343, -13340, -13329, -13326, -13318, -13147, -13138, 
 74 
 75             -13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060, 
 76 
 77             -12888, -12875, -12871, -12860, -12858, -12852, -12849, -12838, -12831,
 78 
 79             -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, -12359,
 80 
 81             -12346, -12320, -12300, -12120, -12099, -12089, -12074, -12067, -12058,
 82 
 83             -12039, -11867, -11861, -11847, -11831, -11798, -11781, -11604, -11589, 
 84 
 85             -11536, -11358, -11340, -11339, -11324, -11303, -11097, -11077, -11067,
 86 
 87             -11055, -11052, -11045, -11041, -11038, -11024, -11020, -11019, -11018,
 88 
 89             -11014, -10838, -10832, -10815, -10800, -10790, -10780, -10764, -10587,
 90 
 91             -10544, -10533, -10519, -10331, -10329, -10328, -10322, -10315, -10309, 
 92 
 93             -10307, -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254 
 94 
 95         };
 96 
 97         private static string[] pyName = new string[]
 98 
 99          { 
100              "A", "Ai", "An", "Ang", "Ao", "Ba", "Bai", "Ban", "Bang", "Bao", "Bei", 
101 
102              "Ben", "Beng", "Bi", "Bian", "Biao", "Bie", "Bin", "Bing", "Bo", "Bu",
103 
104              "Ba", "Cai", "Can", "Cang", "Cao", "Ce", "Ceng", "Cha", "Chai", "Chan",
105 
106              "Chang", "Chao", "Che", "Chen", "Cheng", "Chi", "Chong", "Chou", "Chu",
107 
108              "Chuai", "Chuan", "Chuang", "Chui", "Chun", "Chuo", "Ci", "Cong", "Cou",
109 
110              "Cu", "Cuan", "Cui", "Cun", "Cuo", "Da", "Dai", "Dan", "Dang", "Dao", "De", 
111 
112              "Deng", "Di", "Dian", "Diao", "Die", "Ding", "Diu", "Dong", "Dou", "Du", 
113 
114              "Duan", "Dui", "Dun", "Duo", "E", "En", "Er", "Fa", "Fan", "Fang", "Fei", 
115 
116              "Fen", "Feng", "Fo", "Fou", "Fu", "Ga", "Gai", "Gan", "Gang", "Gao", "Ge", 
117 
118              "Gei", "Gen", "Geng", "Gong", "Gou", "Gu", "Gua", "Guai", "Guan", "Guang", 
119 
120              "Gui", "Gun", "Guo", "Ha", "Hai", "Han", "Hang", "Hao", "He", "Hei", "Hen", 
121 
122              "Heng", "Hong", "Hou", "Hu", "Hua", "Huai", "Huan", "Huang", "Hui", "Hun",
123 
124              "Huo", "Ji", "Jia", "Jian", "Jiang", "Jiao", "Jie", "Jin", "Jing", "Jiong", 
125 
126              "Jiu", "Ju", "Juan", "Jue", "Jun", "Ka", "Kai", "Kan", "Kang", "Kao", "Ke",
127 
128              "Ken", "Keng", "Kong", "Kou", "Ku", "Kua", "Kuai", "Kuan", "Kuang", "Kui", 
129 
130              "Kun", "Kuo", "La", "Lai", "Lan", "Lang", "Lao", "Le", "Lei", "Leng", "Li",
131 
132              "Lia", "Lian", "Liang", "Liao", "Lie", "Lin", "Ling", "Liu", "Long", "Lou", 
133 
134              "Lu", "Lv", "Luan", "Lue", "Lun", "Luo", "Ma", "Mai", "Man", "Mang", "Mao",
135 
136              "Me", "Mei", "Men", "Meng", "Mi", "Mian", "Miao", "Mie", "Min", "Ming", "Miu",
137 
138              "Mo", "Mou", "Mu", "Na", "Nai", "Nan", "Nang", "Nao", "Ne", "Nei", "Nen", 
139 
140              "Neng", "Ni", "Nian", "Niang", "Niao", "Nie", "Nin", "Ning", "Niu", "Nong", 
141 
142              "Nu", "Nv", "Nuan", "Nue", "Nuo", "O", "Ou", "Pa", "Pai", "Pan", "Pang",
143 
144              "Pao", "Pei", "Pen", "Peng", "Pi", "Pian", "Piao", "Pie", "Pin", "Ping", 
145 
146              "Po", "Pu", "Qi", "Qia", "Qian", "Qiang", "Qiao", "Qie", "Qin", "Qing",
147 
148              "Qiong", "Qiu", "Qu", "Quan", "Que", "Qun", "Ran", "Rang", "Rao", "Re",
149 
150              "Ren", "Reng", "Ri", "Rong", "Rou", "Ru", "Ruan", "Rui", "Run", "Ruo", 
151 
152              "Sa", "Sai", "San", "Sang", "Sao", "Se", "Sen", "Seng", "Sha", "Shai", 
153 
154              "Shan", "Shang", "Shao", "She", "Shen", "Sheng", "Shi", "Shou", "Shu", 
155 
156              "Shua", "Shuai", "Shuan", "Shuang", "Shui", "Shun", "Shuo", "Si", "Song", 
157 
158              "Sou", "Su", "Suan", "Sui", "Sun", "Suo", "Ta", "Tai", "Tan", "Tang", 
159 
160              "Tao", "Te", "Teng", "Ti", "Tian", "Tiao", "Tie", "Ting", "Tong", "Tou",
161 
162              "Tu", "Tuan", "Tui", "Tun", "Tuo", "Wa", "Wai", "Wan", "Wang", "Wei",
163 
164              "Wen", "Weng", "Wo", "Wu", "Xi", "Xia", "Xian", "Xiang", "Xiao", "Xie",
165 
166              "Xin", "Xing", "Xiong", "Xiu", "Xu", "Xuan", "Xue", "Xun", "Ya", "Yan",
167 
168              "Yang", "Yao", "Ye", "Yi", "Yin", "Ying", "Yo", "Yong", "You", "Yu", 
169 
170              "Yuan", "Yue", "Yun", "Za", "Zai", "Zan", "Zang", "Zao", "Ze", "Zei",
171 
172              "Zen", "Zeng", "Zha", "Zhai", "Zhan", "Zhang", "Zhao", "Zhe", "Zhen", 
173 
174              "Zheng", "Zhi", "Zhong", "Zhou", "Zhu", "Zhua", "Zhuai", "Zhuan", 
175 
176              "Zhuang", "Zhui", "Zhun", "Zhuo", "Zi", "Zong", "Zou", "Zu", "Zuan",
177 
178              "Zui", "Zun", "Zuo" 
179          };
180 
181         #region 二级汉字
182         /// <summary>
183         /// 二级汉字数组
184         /// </summary>
185         private static string[] otherChinese = new string[]
186         {
187             "","","","","廿","","","","","","","","","","丿"
188             ,"","","","","","","","","","","","","","","",""
189             ,"","","","","","","","","","","","","","","",""
190             ,"","","","","","","","","","","","","","","",""
191             ,"","","","","","","","","","","","","","","",""
192             ,"","","","","","","","","","","","","","",""
193             ,"","","","","","","","","","","","","","",""
194             ,"","","","","","","","","","","","","","","",""
195             ,"","","","","","","","","","","","","","","",""
196             ,"","","","","","","","","","","","","","","",""
197             ,"","","","","","","","","","","","","","","",""
198             ,"","","","","","","","","","","","","","",""
199             ,"","","","","","","","","","","","","","",""
200             ,"","","","","","","","","","","","","","","",""
201             ,"","","","诿","","","","","","","","","","","",""
202             ,"","","","","","","","","","","","","","","",""
203             ,"","","","","","","","","","","","","","","",""
204             ,"","","","","","","","","","","","","","",""
205             ,"","","","","","","","","","","","","","",""
206             ,"","","","","","","","","","","","","","","",""
207             ,"","","","","","","","","","","","","","","",""
208             ,"","","","","","","","","","","","","","","",""
209             ,"","","","","","","","","","","","","","","",""
210             ,"","","","","","","","","","","","","","",""
211             ,"","","","","","","","","","","","","","",""
212             ,"","","","","","","","","","","","","","","",""
213             ,"","","","","","","","","","","","","","","",""
214             ,"","","","","","","","","","","","","","","",""
215             ,"","","","","","","","","","","","","","","",""
216             ,"","","","","","","","","","","","","","",""
217             ,"","","","","","","","","","","","","","",""
218             ,"","","","","","","","","","","","","","","",""
219             ,"","","","","","","","","","","","","","","",""
220             ,"","","","","","","","","","","","","","","",""
221             ,"","","","","","","","","","","","","","","",""
222             ,"","","","","","","","","","","","","","",""
223             ,"","","","","","","","","","","","","","",""
224             ,"","","","","","","","","","","","","","","",""
225             ,"","","","","","","","","","","","","","","",""
226             ,"","","","","","","","","","","","","","","",""
227             ,"","","","","","","","","","","","","","","",""
228             ,"","","","","","","","","","","","","","",""
229             ,"","","","","","","","","","","","","","",""
230             ,"","","","","","","","","","","","","","","",""
231             ,"","","","","","","","","","","","","","","",""
232             ,"","","","","","","","","","","","","","","",""
233             ,"","","","","","","","","","","","","","","",""
234             ,"","","","","","","","","","","","","","",""
235             ,"","","","","","","","","","","","","","",""
236             ,"","","","","","","","","","","","","","","",""
237             ,"","","","","","","","","","","","","","","",""
238             ,"","","","","","","","","","","","","","","",""
239             ,"","","","","","","","","","","","","","","",""
240             ,"","","","","","","","","","","","","","",""
241             ,"","","","","","","","","","","","","","",""
242             ,"","","","","","","","","","","","","","","",""
243             ,"","","","","","","","","","","","","","","",""
244             ,"","","","","","","","","","","","","","","",""
245             ,"","","","","","","","","","","","","","","",""
246             ,"","","","","","","","","","","","","","",""
247             ,"","","","","","","","","","","","","","",""
248             ,"","","","","","","","","","","","","","","",""
249             ,"","","","","","","","","","","","","","","",""
250             ,"","","","","","","","","","","","","","","",""
251             ,"","","","","","","","","","","","","","","",""
252             ,"","","","","","","","","","","","","","",""
253             ,"","","","","","","","","","","","","","",""
254             ,"","","","","","","","","","","","","","","",""
255             ,"","","","","","","","","","","","","","","",""
256             ,"","","","","","","","","","","","","","","",""
257             ,"","","","","","","","","","","","","","","",""
258             ,"","","","","","","","","","","","","","",""
259             ,"","","","","","","","","","","","","","",""
260             ,"","","","","","","","","","","","","","","",""
261             ,"","","","涿","","","","","","","","","","","",""
262             ,"","","","","","","","","","","","","","","",""
263             ,"","","","","","","","","","","","","","","",""
264             ,"","","","","","","","","","","","","","",""
265             ,"","","","","","","","","","","","","","",""
266             ,"","","","","","","","","","","","","","","",""
267             ,"","","","","","","","","","","","","","","",""
268             ,"","","","","","","","","","","","","","","",""
269             ,"","","","","","","","","","","","","","","",""
270             ,"","","","","","","","","","","","","","",""
271             ,"","","","","","","","","","","","","","",""
272             ,"","","","","","","","","","","","","","","",""
273             ,"","","","","","","","","","","","","","","",""
274             ,"","","","","","","","","","","","","","","",""
275             ,"","","","","驿","","","","","","","","","","",""
276             ,"","","","","","","","","","","","","","",""
277             ,"","","","","","","","","","","","","","",""
278             ,"","","","","","","","","","","","","","","",""
279             ,"","","","","","","","","","","","","","","",""
280             ,"","","","","","","","","","","","","","","",""
281             ,"","","","","","","","","","","","","","","",""
282             ,"","","","","","","","","","","","","","",""
283             ,"","","","","","","","","","","","","","",""
284             ,"","","","","","","","","","","","","","","",""
285             ,"","","","","","","","","","","","","","","",""
286             ,"","","","","","","","","","","","","","","",""
287             ,"","","","","","","","","","","","","","","",""
288             ,"","","","","","","","","","","","","","",""
289             ,"","","","","","","","","","","","","","",""
290             ,"","","","","","","","","","","","","","","",""
291             ,"","","","","","","","","槿","","","","","","",""
292             ,"","","","","","","","","","","","","","","",""
293             ,"","","","","","","","","","","","","","","",""
294             ,"","","","","","","","","","","","","","",""
295             ,"","","","","","","","","","","","","","",""
296             ,"","","","","","","","","","","","","","","",""
297             ,"","","","","","","","","","","","","","","",""
298             ,"","","","","","","","","","","","","","","",""
299             ,"","","","","","","","","","","","","","","",""
300             ,"","","","","","","","","","","","","","",""
301             ,"","","","","","","","","","","","","","",""
302             ,"","","","","","","","","","","","","","","",""
303             ,"","","","","","","","","","","","","","","",""
304             ,"","","","","","","","","","","","","","","",""
305             ,"","","","","","","","","","","","","","","",""
306             ,"","","","","","","","","","","","","","",""
307             ,"","","","","","","","","","","","","","",""
308             ,"","","","","","","","","","","","","","","",""
309             ,"","","","","","","","","","","","","","","",""
310             ,"","","","","","","","","","","","","","","",""
311             ,"","","","","","","","","","","","","","","",""
312             ,"","","","","","","","","","","","","","",""
313             ,"","","","","","","","","","","","","","",""
314             ,"","","","","","","","","","","","","","","",""
315             ,"","","","","","","","","","","","","","","",""
316             ,"","","","","","","","","","","","","","","",""
317             ,"","","","","","","","","","","","","","","",""
318             ,"","","","","","","","","","","","","","",""
319             ,"","","","","","","","","","","","","","",""
320             ,"","","","","","","","","","","","","","","",""
321             ,"","","","","","","","","","","","","","","",""
322             ,"","","","","","","","","","","","","","","",""
323             ,"","","","","","","","","","","","","","","",""
324             ,"","","","","","","","","","","","","","",""
325             ,"","","","","","","","","","","","","","",""
326             ,"","","","","","","","","","","","","","","",""
327             ,"","","","","","","","","","","","","","","",""
328             ,"","","","","","","","","","","","","","","",""
329             ,"","","","","","","","","","","","","","","",""
330             ,"","","","","","","","","","","","","","",""
331             ,"","","","","","","","","","","","","","",""
332             ,"","","","","","","","","","","","","","","",""
333             ,"","","","","","","","","","","","","","","",""
334             ,"","","","","","","","","","","","","","","",""
335             ,"","","","","","","","","","","","","","","",""
336             ,"","","","","","","","","","","","","","",""
337             ,"","","","","","","","","","","","","","",""
338             ,"","","","","","","","","","","","","","","",""
339             ,"","","","","","","","","","","","","","","",""
340             ,"","","","","","","","","","","","","","","",""
341             ,"","","","","","","","","","","","","","","",""
342             ,"","","","","","","","","","","","","","",""
343             ,"","","","","","","","","","","","","","",""
344             ,"","","","","","","","","","","","","","","",""
345             ,"","","","","","","","","","","","","","","",""
346             ,"","","","","","","","","","","","","","","",""
347             ,"","","","","","","","","","","","","","","",""
348             ,"","","","","","","","","","","","","","",""
349             ,"","","","","","","","","","","","","","",""
350             ,"","","","","","","","","","","","","","","",""
351             ,"","","","","","","","","","","","","","","",""
352             ,"","","","","","","","","","","","","","","",""
353             ,"","","","","","","","","","","","","","","",""
354             ,"","","","","","","","","","","","","","",""
355             ,"","","","","","","","","","","","","","",""
356             ,"","","","","","","","","","","","","","","",""
357             ,"","","","","","","","","","","","","","","",""
358             ,"","","","","","","","","","","","","","","",""
359             ,"羿","","","","","","","","","","","","","","",""
360             ,"","","","","","","","","","","","","","",""
361             ,"","","","","","","","","","","","","","",""
362             ,"","","","","","","","","","","","","","","",""
363             ,"","趿","","","","","","","","","","","","","",""
364             ,"","","","","","","","","","","","","","","",""
365             ,"","","","","","","","","","","","","","","",""
366             ,"","","","","","","","","","","","","","",""
367             ,"","","","","","","","","","","","","","",""
368             ,"","","","","","","","","","","","","","","",""
369             ,"","","","","","","","","","","","","","","","鱿"
370             ,"","","","","","","","","","","","","","","",""
371             ,"","","","","","","","","","","","","","","",""
372             ,"","","","","","","","","","","","","","",""
373             ,"","","","","","","","","","","","","","",""
374             ,"","","","","","","","","","","","","","","",""
375             ,"","","","","","","","","","","","","","","",""
376             ,"","","","","","","","","","","","","","","",""
377             ,"","","","","","","","","","","","","","","",""
378             ,"","","","","","","","","","","","","","",""
379         };
380 
381         /// <summary>
382         /// 二级汉字对应拼音数组
383         /// </summary>
384         private static string[] otherPinYin = new string[]
385            {                         
386                "Chu","Ji","Wu","Gai","Nian","Sa","Pi","Gen","Cheng","Ge","Nao","E","Shu","Yu","Pie","Bi",
387                 "Tuo","Yao","Yao","Zhi","Di","Xin","Yin","Kui","Yu","Gao","Tao","Dian","Ji","Nai","Nie","Ji",
388                 "Qi","Mi","Bei","Se","Gu","Ze","She","Cuo","Yan","Jue","Si","Ye","Yan","Fang","Po","Gui",
389                 "Kui","Bian","Ze","Gua","You","Ce","Yi","Wen","Jing","Ku","Gui","Kai","La","Ji","Yan","Wan",
390                 "Kuai","Piao","Jue","Qiao","Huo","Yi","Tong","Wang","Dan","Ding","Zhang","Le","Sa","Yi","Mu","Ren",
391                 "Yu","Pi","Ya","Wa","Wu","Chang","Cang","Kang","Zhu","Ning","Ka","You","Yi","Gou","Tong","Tuo",
392                 "Ni","Ga","Ji","Er","You","Kua","Kan","Zhu","Yi","Tiao","Chai","Jiao","Nong","Mou","Chou","Yan",
393                 "Li","Qiu","Li","Yu","Ping","Yong","Si","Feng","Qian","Ruo","Pai","Zhuo","Shu","Luo","Wo","Bi",
394                 "Ti","Guan","Kong","Ju","Fen","Yan","Xie","Ji","Wei","Zong","Lou","Tang","Bin","Nuo","Chi","Xi",
395                 "Jing","Jian","Jiao","Jiu","Tong","Xuan","Dan","Tong","Tun","She","Qian","Zu","Yue","Cuan","Di","Xi",
396                 "Xun","Hong","Guo","Chan","Kui","Bao","Pu","Hong","Fu","Fu","Su","Si","Wen","Yan","Bo","Gun",
397                 "Mao","Xie","Luan","Pou","Bing","Ying","Luo","Lei","Liang","Hu","Lie","Xian","Song","Ping","Zhong","Ming",
398                 "Yan","Jie","Hong","Shan","Ou","Ju","Ne",