一些比较好的方法或类,适用于C#

在开发过程有我收集了一些比较好的方法

  1 using System; 
  2 using System.Collections.Generic; 
  3 using System.Configuration; 
  4 using System.IO; 
  5 using System.Linq; 
  6 using System.Net; 
  7 using System.Text; 
  8 using System.Text.RegularExpressions; 
  9 using System.Web; 
 10 using System.Web.Configuration; 
 11 using System.Web.Security; 
 12 
 13 namespace Acesoft.Common 
 14 { 
 15 public static class Tool 
 16 { 
 17 public const string vbCrlf = "vbCrlf"; 
 18 public static string AppPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString(); 
 19 public static string Theme = ReadAppSitting("Theme"); 
 20 #region MD5加密 
 21 /// <summary> 
 22 /// MD5加密 
 23 /// </summary> 
 24 /// <param name="Pwd">要加密的字符串</param> 
 25 /// <param name="salt">区别符</param> 
 26 /// <returns>返回加密后的MD5字符</returns> 
 27 
 28 public static string Md5(string Pwd, string salt = "king") 
 29 { 
 30 string result = FormsAuthentication.HashPasswordForStoringInConfigFile(Pwd + salt, "MD5"); 
 31 return FormsAuthentication.HashPasswordForStoringInConfigFile(result, "MD5"); 
 32 } 
 33 #endregion 
 34 
 35 #region 制作随机字母和数字 
 36 /// <summary> 
 37 /// 制作随机字母和数字 
 38 /// </summary> 
 39 /// <param name="Length">得到的随机字符串长度</param> 
 40 /// <param name="type">0为小写字母数字混合,1为纯数字,2为纯小字母,3为大小写混合</param> 
 41 /// <returns></returns> 
 42 public static string MakeRandomNumber(int Length, int type = 0) 
 43 { 
 44 char[] constant = { 
 45 '0','1','2','3','4','5','6','7','8','9', 
 46 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', 
 47 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' 
 48 }; 
 49 System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62); 
 50 Random rd = new Random(); 
 51 int max = 36; 
 52 int min = 0; 
 53 switch (type) 
 54 { 
 55 case 1: 
 56 max = 10; 
 57 break; 
 58 case 2: 
 59 min = 10; 
 60 break; 
 61 case 3: 
 62 min = 10; 
 63 max = 62; 
 64 break; 
 65 } 
 66 
 67 for (int i = 0; i < Length; i++) 
 68 { 
 69 newRandom.Append(constant[rd.Next(min, max)]); 
 70 } 
 71 return newRandom.ToString(); 
 72 } 
 73 #endregion 
 74 
 75 #region 读取文本文件 
 76 /// <summary> 
 77 /// 读取文本文件 
 78 /// </summary> 
 79 /// <param name="txtPath">文件路径</param> 
 80 /// <param name="code">文件编码,默认是"GB2312"</param> 
 81 /// <returns></returns> 
 82 public static string ReadTxt(string txtPath, string code = "GB2312") 
 83 { 
 84 string txt = ""; 
 85 try 
 86 { 
 87 txtPath = txtPath.Replace("/", "\\"); 
 88 if (File.Exists(txtPath) == true) 
 89 { 
 90 FileStream fs = new FileStream(txtPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
 91 StreamReader sr = new StreamReader(fs, Encoding.GetEncoding(code)); 
 92 txt = sr.ReadToEnd(); 
 93 sr.Close(); 
 94 fs.Dispose(); 
 95 } 
 96 return txt; 
 97 } 
 98 catch 
 99 { 
100 return ""; 
101 } 
102 } 
103 #endregion 
104 
105 #region 创建并写入txt文件 
106 
107 /// <summary> 
108 /// 创建并写入txt文件 
109 /// </summary> 
110 /// <param name="txtPath">文件路径</param> 
111 /// 要写入的内容 
112 /// <param name="code">文件编码格式,默认为GB2312</param> 
113 /// <returns></returns> 
114 public static bool WriteTxt(string txtPath, string Constr, string code = "GB2312") 
115 { 
116 try 
117 { 
118 txtPath = txtPath.Replace("/", "\\"); 
119 txtPath = txtPath.Replace("$split$", "|"); 
120 if (GetBody(Constr, "<meta[^<]*charset=([^<]*)\"").ToLower() == "utf-8") code = "UTF-8"; 
121 if (Directory.Exists(Path.GetDirectoryName(txtPath)) == false) Directory.CreateDirectory(Path.GetDirectoryName(txtPath)); 
122 StreamWriter sw = new StreamWriter(txtPath, false, Encoding.GetEncoding(code)); 
123 sw.Write(Constr); 
124 sw.Close(); 
125 return true; 
126 } 
127 catch 
128 { return false; } 
129 } 
130 #endregion 
131 
132 #region 获取文件大小 
133 /// <summary> 
134 /// 获取文件大小 
135 /// </summary> 
136 /// <param name="txtPath">文件地址</param> 
137 /// <param name="_appPath">网站目录地址</param> 
138 /// <returns></returns> 
139 public static long GetFileSize(string txtPath, string _appPath = "") 
140 { 
141 try 
142 { 
143 if (string.IsNullOrEmpty(_appPath)) 
144 { 
145 _appPath = AppPath; 
146 } 
147 txtPath = _appPath + txtPath.Replace("/", "\\"); 
148 if (File.Exists(txtPath) == true) 
149 { 
150 FileStream fs = new FileStream(txtPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
151 long len = fs.Length; 
152 fs.Dispose(); 
153 return len; 
154 } 
155 return 0; 
156 } 
157 catch 
158 { 
159 return 0; 
160 } 
161 } 
162 #endregion 
163 
164 #region 写入注册表 
165 /// <summary> 
166 /// 写入注册表 
167 /// </summary> 
168 /// <param name="para">注册表键名</param> 
169 /// <param name="paraVale">注册表键值</param> 
170 /// <param name="paraDir">注册表目录名</param> 
171 /// <returns></returns> 
172 public static bool WriteKey(string para, string paraVale, string paraDir = "northsnow") 
173 { 
174 try 
175 { 
176 Microsoft.Win32.RegistryKey Key1 = Microsoft.Win32.Registry.CurrentUser; 
177 Microsoft.Win32.RegistryKey Key2 = Key1.OpenSubKey(paraDir, true); 
178 if (Key2 == null) 
179 Key2 = Key1.CreateSubKey(paraDir); //如果键不存在就创建它 
180 Key2.SetValue(para, paraVale); 
181 return true; 
182 } 
183 catch 
184 { return false; } 
185 } 
186 #endregion 
187 
188 #region 读取指定注册表的键值 
189 /// <summary> 
190 /// 读取指定注册表的键值 
191 /// </summary> 
192 /// <param name="para">注册表键名</param> 
193 /// <param name="paraDir">注册表键名目录</param> 
194 /// <returns></returns> 
195 public static string ReadKey(string para, string paraDir = "northsnow") 
196 { 
197 try 
198 { 
199 Microsoft.Win32.RegistryKey Key1 = Microsoft.Win32.Registry.CurrentUser; 
200 Microsoft.Win32.RegistryKey Key2 = Key1.OpenSubKey(paraDir, true); 
201 if (Key2 == null) 
202 return ""; 
203 else 
204 return Key2.GetValue(para).ToString(); 
205 } 
206 catch 
207 { return ""; } 
208 } 
209 #endregion 
210 
211 #region 获取单个汉字的首拼音 
212 /// <summary> 
213 /// 获取单个汉字的首拼音 
214 /// </summary> 
215 /// <param name="myChar">需要转换的字符</param> 
216 /// <returns>转换结果</returns> 
217 private static string getSpell(string myChar) 
218 { 
219 byte[] arrCN = System.Text.Encoding.Default.GetBytes(myChar); 
220 if (arrCN.Length > 1) 
221 { 
222 int area = (short)arrCN[0]; 
223 int pos = (short)arrCN[1]; 
224 int code = (area << 8) + pos; 
225 int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 }; 
226 for (int i = 0; i < 26; i++) 
227 { 
228 int max = 55290; 
229 if (i != 25) max = areacode[i + 1]; 
230 if (areacode[i] <= code && code < max) 
231 { 
232 return System.Text.Encoding.Default.GetString(new byte[] { (byte)(65 + i) }); 
233 } 
234 } 
235 return "_"; 
236 } 
237 else return myChar.ToUpper(); 
238 } 
239 #endregion 
240 
241 #region 获取字符串中汉字的首或全拼音 
242 /// <summary> 
243 /// 获取字符串中汉字的首或全拼音 
244 /// </summary> 
245 /// <param name="para">需要转换的汉字字符串</param> 
246 /// <param name="spellType">只转换得到首字母参为true,全拼为false</param> 
247 /// <returns>转换结果</returns> 
248 public static string GetSpell(string para, bool spellType = true) 
249 { 
250 string Str = ""; 
251 para = Regex.Replace(para, " ", ""); 
252 para = Regex.Replace(para, " ", ""); 
253 for (int i = 0; i < para.Length; i++) 
254 { 
255 if (spellType == true) 
256 Str += getSpell(para[i].ToString()); 
257 else 
258 Str += NPinyin.Pinyin.GetPinyin(para[i]); 
259 } 
260 return Str; 
261 } 
262 #endregion 
263 
264 #region 正则单个过滤替换 
265 /// <summary> 
266 /// 正则单个过滤替换 
267 /// </summary> 
268 /// <param name="para">要过滤或者要替换的源字段串</param> 
269 /// <param name="ruleString">过滤或者替换规则</param> 
270 /// <param name="replaceString">替换成的内容</param> 
271 /// <returns></returns> 
272 private static string myFilter(string para, string ruleString, string replaceString = "") 
273 { 
274 try 
275 { 
276 Regex rgx = new Regex(ruleString, RegexOptions.IgnoreCase); 
277 return rgx.Replace(para, replaceString); 
278 } 
279 catch 
280 { 
281 return para; 
282 } 
283 } 
284 #endregion 
285 
286 #region 正则过滤替换 
287 /// <summary> 
288 /// 正则过滤替换 
289 /// </summary> 
290 /// <param name="para">要过滤或者要替换的源字段串</param> 
291 /// <param name="ruleString">过滤或者替换规则多个用vbCrlf分隔</param> 
292 /// <param name="replaceString">替换成的内容多个用vbCrlf分隔</param> 
293 /// <returns></returns> 
294 public static string MyFilter(string para, string ruleString, string replaceString = "") 
295 { 
296 
297 string[] RuleArray = Regex.Split(ruleString, vbCrlf); 
298 string[] ReplaceArray = Regex.Split(replaceString, vbCrlf); 
299 
300 for (int i = 0; i < RuleArray.Length; i++) 
301 { 
302 if (replaceString == "") 
303 para = myFilter(para, RuleArray[i]); 
304 else 
305 { 
306 string item = ""; 
307 if (i < ReplaceArray.Length) 
308 item = ReplaceArray[i]; 
309 para = myFilter(para, RuleArray[i], item); 
310 } 
311 
312 } 
313 return para; 
314 } 
315 #endregion 
316 
317 #region 截取正则规则内容 
318 /// <summary> 
319 /// 截取正则规则内容 
320 /// </summary> 
321 /// <param name="para">要截取的源字符串</param> 
322 /// <param name="ruleString">正则规则</param> 
323 /// <param name="filterStr">过滤规则</param> 
324 /// <returns></returns> 
325 public static string GetBody(string para, string ruleString, string filterStr = null) 
326 { 
327 
328 try 
329 { 
330 MatchCollection Mc = Regex.Matches(para, ruleString, RegexOptions.IgnoreCase); 
331 if (Mc.Count > 0) 
332 { 
333 if (string.IsNullOrEmpty(filterStr)) 
334 return Mc[0].Groups[1].Value; 
335 else 
336 return MyFilter(Mc[0].Groups[1].Value, filterStr.Replace("\r\n", vbCrlf)); 
337 } 
338 else 
339 return ""; 
340 } 
341 catch 
342 { 
343 return ""; 
344 } 
345 } 
346 #endregion 
347 
348 #region 按正则取内容返回数据为数组 
349 /// <summary> 
350 /// 按正则取内容返回数据为数组 
351 /// </summary> 
352 /// <param name="para">要取内容的源文件</param> 
353 /// <param name="ruleString">正则规则</param> 
354 /// <returns></returns> 
355 public static string[] GetArray(string para, string ruleString) 
356 { 
357 IList<string> Result = new List<string>(); 
358 try 
359 { 
360 MatchCollection Mc = Regex.Matches(para, ruleString, RegexOptions.IgnoreCase); 
361 foreach (Match ma in Mc) 
362 { 
363 Result.Add(ma.Groups[1].Value); 
364 
365 } 
366 return Result.ToArray(); 
367 } 
368 catch 
369 { 
370 return Result.ToArray(); 
371 } 
372 
373 } 
374 #endregion 
375 
376 #region 正则获取多个值,近回值为二维的List型 
377 /// <summary> 
378 /// 正则获取多个值,近回值为二维的List型 
379 /// </summary> 
380 /// <param name="para">要取内容的源文件</param> 
381 /// <param name="ruleString">正则规则</param> 
382 /// <returns></returns> 
383 public static List<List<string>> GetMultiArray(string para, string ruleString) 
384 { 
385 List<List<string>> Result = new List<List<string>>(); 
386 
387 try 
388 { 
389 MatchCollection Mc = Regex.Matches(para, ruleString, RegexOptions.IgnoreCase); 
390 foreach (Match Ma in Mc) 
391 { 
392 List<string> RowList = new List<string>(); 
393 for (int i = 1; i < Ma.Groups.Count; i++) 
394 { 
395 RowList.Add(Ma.Groups[i].Value); 
396 } 
397 Result.Add(RowList); 
398 } 
399 return Result; 
400 } 
401 catch 
402 { 
403 return Result; 
404 } 
405 
406 } 
407 #endregion 
408 
409 #region 获取网页源码 
410 /// <summary> 
411 /// 获取网页源码 
412 /// </summary> 
413 /// <param name="url">要获取网页的地址</param> 
414 /// <param name="RefererUrl">网页的refer</param> 
415 /// <param name="charSet">网页的字符编码,不知道是什么编码时输入空</param> 
416 /// <returns></returns> 
417 public static string getWebCodes(string Url, string RefererUrl = "http://www.baidu.com", string charSet = "GB2312") 
418 { 
419 string strHTML = ""; 
420 try 
421 { 
422 WebClient myWebClient = new WebClient(); 
423 myWebClient.Headers.Add(HttpRequestHeader.Referer, RefererUrl); 
424 Stream myStream = myWebClient.OpenRead(Url); 
425 StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding(charSet)); 
426 strHTML = sr.ReadToEnd().ToString(); 
427 myStream.Close(); 
428 return strHTML; 
429 } 
430 catch 
431 { 
432 return strHTML; 
433 } 
434 } 
435 #endregion 
436 
437 public static string FromChinaz(string url) 
438 { 
439 string ChinazUrl = "http://tool.chinaz.com/Tools/PageCode.aspx?q=" + url.Replace("http://", ""); 
440 string WebCodes = getWebCodes(ChinazUrl, "http://tool.chinaz.com/Tools/PageCode.aspx", "utf-8"); 
441 WebCodes = GetBody(WebCodes, "<textarea name=\"code\" id=\"htmltext\" class=\"xml\" style=\"width:95%;height:500px;\">((.|\\n)+?)</textarea>"); 
442 WebCodes = HttpUtility.HtmlDecode(WebCodes); 
443 return WebCodes; 
444 } 
445 
446 
447 #region 取指定文件的后缀名 
448 /// <summary> 
449 /// 取指定文件的后缀名 
450 /// </summary> 
451 /// <param name="PicName">文件名或文件路径</param> 
452 /// <returns></returns> 
453 public static string GetPicType(string PicName) 
454 { 
455 return Path.GetExtension(PicName); 
456 } 
457 #endregion 
458 
459 #region 取指定地址的域名,返回结果中在域名前加了http:// 
460 /// <summary> 
461 /// 取指定地址的域名,返回结果中在域名前加了http:// 
462 /// </summary> 
463 /// <param name="Url">指定的地址</param> 
464 /// <returns></returns> 
465 public static string GetDomain(string Url) 
466 { 
467 try 
468 { 
469 Uri MyUrl = new Uri(Url); 
470 return "http://" + MyUrl.Host; 
471 } 
472 catch 
473 { 
474 return ""; 
475 } 
476 } 
477 #endregion 
478 
479 #region 复制文件夹(及文件夹下所有子文件夹和文件) 
480 /// <summary> 
481 /// 复制文件夹(及文件夹下所有子文件夹和文件) 
482 /// </summary> 
483 /// <param name="sourcePath">待复制的文件夹路径</param> 
484 /// <param name="destinationPath">目标路径</param> 
485 public static void CopyDirectory(String sourcePath, String destinationPath) 
486 { 
487 DirectoryInfo info = new DirectoryInfo(sourcePath); 
488 Directory.CreateDirectory(destinationPath); 
489 foreach (FileSystemInfo fsi in info.GetFileSystemInfos()) 
490 { 
491 String destName = Path.Combine(destinationPath, fsi.Name); 
492 
493 if (fsi is System.IO.FileInfo) //如果是文件,复制文件 
494 File.Copy(fsi.FullName, destName, true); 
495 else //如果是文件夹,新建文件夹,递归 
496 { 
497 Directory.CreateDirectory(destName); 
498 CopyDirectory(fsi.FullName, destName); 
499 } 
500 } 
501 } 
502 #endregion 
503 
504 #region 删除文件夹(及文件夹下所有子文件夹和文件) 
505 /// <summary> 
506 /// 删除文件夹(及文件夹下所有子文件夹和文件) 
507 /// </summary> 
508 /// <param name="directoryPath"></param> 
509 public static void DeleteFolder(string directoryPath) 
510 { 
511 foreach (string d in Directory.GetFileSystemEntries(directoryPath)) 
512 { 
513 if (File.Exists(d)) 
514 { 
515 FileInfo fi = new FileInfo(d); 
516 if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1) 
517 fi.Attributes = FileAttributes.Normal; 
518 File.Delete(d); //删除文件 
519 } 
520 else 
521 DeleteFolder(d); //删除文件夹 
522 } 
523 Directory.Delete(directoryPath); //删除空文件夹 
524 } 
525 #endregion 
526 
527 #region 把内容替换为纯文本 
528 /// <summary> 
529 /// 把内容替换为纯文本 
530 /// </summary> 
531 /// <param name="para">要处理的字符串</param> 
532 /// <returns></returns> 
533 public static string FilterTxtContent(string para) 
534 { 
535 if (para == "") 
536 return ""; 
537 para = MyFilter(para, @" " + vbCrlf + "'" + vbCrlf + "\r\n" + vbCrlf + "<.+?>" + vbCrlf + @"\s*", " " + vbCrlf + ""); 
538 return para; 
539 } 
540 #endregion 
541 
542 
543 #region 将相对地址转换为绝对地址 
544 /// <summary> 
545 /// 将相对地址转换为绝对地址 
546 /// </summary> 
547 /// <param name="PrimitiveUrl">要转换的相对地址</param> 
548 /// <param name="ConsultUrl">当前网页地址</param> 
549 /// <returns></returns> 
550 public static string DefiniteUrl(string PrimitiveUrl, string ConsultUrl) 
551 { 
552 try 
553 { 
554 Uri baseUri = new Uri(ConsultUrl); 
555 Uri myUri = new Uri(baseUri, PrimitiveUrl); 
556 return myUri.AbsoluteUri; 
557 } 
558 catch 
559 { return ""; } 
560 } 
561 #endregion 
562 
563 
564 
565 #region 过滤域名 
566 /// <summary> 
567 /// 过滤域名 
568 /// </summary> 
569 /// <param name="ConStr">要过滤的内容</param> 
570 /// <param name="Url">过滤的地址</param> 
571 /// <returns></returns> 
572 private static string FilterDomain(string ConStr, string Url) 
573 { 
574 try 
575 { 
576 Uri MyUri = new Uri(Url); 
577 string Domain = MyUri.Host; 
578 ConStr = MyFilter(ConStr, "http://" + Domain + vbCrlf + Domain); 
579 return ConStr; 
580 } 
581 catch 
582 { 
583 return ConStr; 
584 } 
585 
586 } 
587 #endregion 
588 
589 #region 删除指定文件 
590 /// <summary> 
591 /// 删除指定文件 
592 /// </summary> 
593 /// <param name="filePath">文件路径</param> 
594 /// <returns>成功返回True,失败返回False</returns> 
595 public static bool DelFile(string filePath) 
596 { 
597 try 
598 { 
599 FileInfo file = new FileInfo(filePath); 
600 if (file.Exists == true) 
601 { 
602 File.Delete(filePath); 
603 return true; 
604 } 
605 else 
606 { 
607 return false; 
608 } 
609 
610 } 
611 catch 
612 { 
613 return false; 
614 } 
615 } 
616 #endregion 
617 
618 
619 #region 时间戳转为C#格式时间 
620 /// <summary> 
621 /// 时间戳转为C#格式时间 
622 /// </summary> 
623 /// <param name=”timeStamp”></param> 
624 /// <returns></returns> 
625 public static DateTime GetTime(string timeStamp) 
626 { 
627 DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); 
628 long lTime = long.Parse(timeStamp + "0000000"); 
629 TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow); 
630 } 
631 #endregion 
632 
633 #region DateTime时间格式转换为Unix时间戳格式 
634 /// <summary> 
635 /// DateTime时间格式转换为Unix时间戳格式 
636 /// </summary> 
637 /// <param name=”time”></param> 
638 /// <returns></returns> 
639 public static int ConvertDateTimeInt(System.DateTime time) 
640 { 
641 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); 
642 return (int)(time - startTime).TotalSeconds; 
643 } 
644 #endregion 
645 
646 #region 创建文件夹 
647 /// <summary> 
648 /// 创建文件夹 
649 /// </summary> 
650 /// <param name="DirPath">文件夹路径</param> 
651 public static bool CreateFolder(string DirPath) 
652 { 
653 try 
654 { 
655 if (Directory.Exists(Path.GetDirectoryName(DirPath)) == false) 
656 { 
657 Directory.CreateDirectory(Path.GetDirectoryName(DirPath)); 
658 return true; 
659 } 
660 else 
661 return false; 
662 } 
663 catch 
664 { 
665 return false; 
666 } 
667 } 
668 #endregion 
669 
670 #region 读取web.config APPSiteting的值 
671 /// <summary> 
672 /// 读取web.config APPSiteting的值 
673 /// </summary> 
674 /// <param name="name"></param> 
675 /// <returns></returns> 
676 public static string ReadAppSitting(string name) 
677 { 
678 try 
679 { 
680 return WebConfigurationManager.AppSettings[name]; 
681 } 
682 catch (Exception) 
683 { 
684 
685 return ""; 
686 } 
687 } 
688 #endregion 
689 
690 #region 修改添加web.config APPSiteting的值 
691 /// <summary> 
692 /// 修改添加web.config APPSiteting的值 
693 /// </summary> 
694 /// <param name="key">键名</param> 
695 /// <param name="value">键值</param> 
696 public static bool ModifyAppSetings(string key, string value) 
697 { 
698 try 
699 { 
700 Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~"); 
701 AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings"); 
702 if (appSettingsSection != null) 
703 { 
704 if (appSettingsSection.Settings[key] != null) 
705 appSettingsSection.Settings[key].Value = value; 
706 else 
707 { 
708 appSettingsSection.Settings.Add(key, value); 
709 } 
710 configuration.Save(); 
711 return true; 
712 } 
713 else 
714 return false; 
715 } 
716 catch (Exception) 
717 { 
718 
719 return false; 
720 } 
721 } 
722 #endregion 
723 
724 #region 获取Bootstrap分页 
725 /// <summary> 
726 /// 获取Bootstrap分页 
727 /// </summary> 
728 /// <param name="RsCount">总记录数</param> 
729 /// <param name="PageSize">分页大小</param> 
730 /// <param name="CurrentPage">当前页</param> 
731 /// <param name="PageUrl">当前地址</param> 
732 /// <returns></returns> 
733 public static string GetPageStr(int RsCount, int PageSize, int CurrentPage, string PageUrl) 
734 { 
735 string PageStr = ""; 
736 if (RsCount > 0) 
737 { 
738 
739 int PageCount = (int)System.Math.Ceiling((double)RsCount / PageSize); 
740 int k = CurrentPage; 
741 int m = 1, n = 10; 
742 if (k <= 5) 
743 { 
744 if (PageCount <= 10) 
745 n = PageCount; 
746 else 
747 n = 10; 
748 } 
749 else 
750 { 
751 m = k - 5; 
752 n = k + 5; 
753 if (n > PageCount) 
754 { 
755 n = PageCount; 
756 m = n - 10; 
757 if (m < 1) m = 1; 
758 } 
759 } 
760 string SubPageUrl = PageUrl + "/"; 
761 
762 PageStr = PageStr + "<li><a href=\"" + PageUrl + "\">首页</a></li>"; 
763 string LastPage = ""; 
764 if (CurrentPage == 1) 
765 LastPage = "<li class=\"disabled\"><a href=\"#\">«</a></li>"; 
766 else 
767 LastPage = "<li><a href=\"" + SubPageUrl + (CurrentPage - 1) + "\">«</a></li>"; 
768 PageStr = PageStr + LastPage; 
769 for (int t = m; t <= n; t++) 
770 { 
771 if (t == CurrentPage) 
772 PageStr = PageStr + "<li class=\"active\"><a href=\"#\">" + t + "<span class=\"sr-only\">(current)</span></a></li>"; 
773 else 
774 PageStr = PageStr + "<li><a href=\"" + SubPageUrl + t + "\">" + t + "</a></li>"; 
775 
776 } 
777 string NextPage = ""; 
778 if (CurrentPage == PageCount) 
779 NextPage = "<li class=\"disabled\"><a href=\"#\">»</a></li>"; 
780 else 
781 NextPage = "<li><a href=\"" + SubPageUrl + (CurrentPage + 1) + "\">»</a></li>"; 
782 PageStr = PageStr + NextPage + "<li><a href=\"" + SubPageUrl + PageCount + "\">尾页</a></li>"; 
783 
784 } 
785 return PageStr; 
786 } 
787 #endregion 
788 
789 #region 获取Metro分页 
790 
791 public static string GetMetroPageStr(int RsCount, int PageSize, int CurrentPage, string PageUrl) 
792 { 
793 string PreFix = GetPicType(PageUrl); 
794 string split = "_"; 
795 if (!string.IsNullOrEmpty(PreFix)) 
796 { 
797 PageUrl = PageUrl.Replace(PreFix, ""); 
798 } 
799 else 
800 { 
801 split = "/"; 
802 } 
803 int PageCount = (int)System.Math.Ceiling((double)RsCount / PageSize); 
804 int k = CurrentPage; 
805 int m = 1, n = 10; 
806 if (k <= 5) 
807 { 
808 if (PageCount <= 10) 
809 n = PageCount; 
810 else 
811 n = 10; 
812 } 
813 else 
814 { 
815 m = k - 5; 
816 n = k + 5; 
817 if (n > PageCount) 
818 { 
819 n = PageCount; 
820 m = n - 10; 
821 if (m < 1) m = 1; 
822 } 
823 } 
824 string SubPageUrl = PageUrl; 
825 string PageStr = ""; 
826 PageStr = PageStr + "<li class=\"first\"><a href=\"" + PageUrl + PreFix + "\"><i class=\"icon-first-2\"></i></a></li>"; 
827 string LastPage = ""; 
828 if (CurrentPage == 1) 
829 LastPage = "<li class=\"prev disabled\"><a><i class=\"icon-previous\"></i></a></li>"; 
830 else 
831 LastPage = "<li class=\"prev\"><a href=\"" + SubPageUrl + split + (CurrentPage - 1) + PreFix + "\"><i class=\"icon-previous\"></i></a></li>"; 
832 PageStr = PageStr + LastPage; 
833 for (int t = m; t <= n; t++) 
834 { 
835 if (t == CurrentPage) 
836 PageStr = PageStr + "<li class=\"active\"><a href=\"#\"><i>" + t + "</i></a></li>"; 
837 else 
838 { 
839 if (t == 1) 
840 { 
841 PageStr = PageStr + "<li><a href=\"" + SubPageUrl + PreFix + "\"><i>" + t + "</i></a></li>"; 
842 } 
843 else 
844 { 
845 PageStr = PageStr + "<li><a href=\"" + SubPageUrl + split + t + PreFix + "\"><i>" + t + "</i></a></li>"; 
846 } 
847 
848 } 
849 
850 } 
851 string NextPage = ""; 
852 if (CurrentPage == PageCount) 
853 NextPage = "<li class=\"next disabled\"><a><i class=\"icon-next\"></i></a></li>"; 
854 else 
855 NextPage = "<li class=\"next\"><a href=\"" + SubPageUrl + split + (CurrentPage + 1) + PreFix + "\"><i class=\"icon-next\"></i></a></li>"; 
856 PageStr = PageStr + NextPage + "<li class=\"last\"><a href=\"" + SubPageUrl + split + PageCount + PreFix + "\"><i class=\"icon-last-2\"></i></a></li>"; 
857 return PageStr; 
858 } 
859 #endregion 
860 
861 #region 产生指定范围的随机数 
862 /// <summary> 
863 /// 产生指定范围的随机数 
864 /// </summary> 
865 /// <param name="low">最小数</param> 
866 /// <param name="uper">最大数</param> 
867 /// <param name="k">参数</param> 
868 /// <returns></returns> 
869 public static int MakeRnd(int low, int uper, int k = 10) 
870 { 
871 Random rd = new Random(k); 
872 return rd.Next(low, uper); 
873 } 
874 #endregion 
875 
876 public static List<string> GetFiles(string path) 
877 { 
878 List<string> dir = Directory.GetDirectories(path).ToList(); 
879 List<string> files = Directory.GetFiles(path).ToList(); 
880 List<string> fileNames = new List<string>(); 
881 foreach (string file in files) { fileNames.Add(Path.GetFileName(file)); } 
882 foreach (string item in dir) 
883 { 
884 List<string> subFileNames = GetFiles(item); 
885 fileNames.AddRange(subFileNames); 
886 } 
887 return fileNames; 
888 } 
889 
890 } 
891 
892 } 

 

posted on 2015-02-13 10:47  波澜不惊super  阅读(975)  评论(0编辑  收藏  举报

导航