1、过滤特殊字符
public class CharService:IDisposable { private List<char> _invalidChars; public CharService() { _invalidChars = new List<char>(); _invalidChars.AddRange(Path.GetInvalidFileNameChars()); _invalidChars.AddRange(Path.GetInvalidPathChars()); } /// <summary> /// 特殊字符过滤 /// </summary> /// <param name="str"></param> /// <returns></returns> public string SplitStringInvaild(string str,bool isLower=true) { str = ToDBC(str); StringBuilder sb = new StringBuilder(); foreach (char c in str) { if (_invalidChars.Contains(c)) continue; if(isLower) sb.Append(ToLower(c)); else sb.Append(c); } str = sb.ToString().Trim(); if (isLower) { if (str.Substring(str.Length-1,1)==".") { str = str.Substring(0, str.Length - 1); } } return str; } public static char ToLower(char c) { if (('A' <= c) && (c <= 'Z')) { c = (char)(c | ' '); } return c; } public static String ToDBC(String input) { char[] c = input.ToCharArray(); for (int i = 0; i < c.Length; i++) { if (c[i] == 12288) { c[i] = (char)32; continue; } if (c[i] > 65280 && c[i] < 65375) c[i] = (char)(c[i] - 65248); } return new String(c); } public void Dispose() { _invalidChars = null; } }
2、将DataTable数据保存为.csv文件
private bool DataTableToCsv(System.Data.DataTable tb, string FileName) { bool Export = false; TextWriter writer = null; try { string text = String.Empty; // 使用指定编码和缓冲区大小,为指定路径上的指定文件初始化 StreamWriter 类的新实例。 // 如果该文件存在,则可以将其覆盖或向其追加。如果该文件不存在,则此构造函数将创建一个新文件。 writer = new StreamWriter(FileName, false, Encoding.UTF8); // 取得列的个数 int count = tb.Columns.Count; string[] fields = new string[count]; // 循环添加列名 for (int i = 0; i < count; i++) { DataColumn column = tb.Columns[i]; fields[i] = column.ColumnName; } text = string.Join(",", fields); writer.WriteLine(text); // 遍历行 foreach (DataRow row in tb.Rows) { // 给每一行添加列单元 for (int i = 0; i < count; i++) { if (row[i] == null) { fields[i] = String.Empty; continue; } if (row[i] == DBNull.Value) { fields[i] = String.Empty; continue; } string field = row[i].ToString(); // CSV格式有规定,比如,如果字段中有逗号、双引号、回车换行符、前或后置空格, // 必须把字段用双引号扩起来,如果文本中有双引号,必须用两个双引号替换 field = field.Replace(""", """"); if (field.IndexOf(',') >= 0) { fields[i] = string.Format(""{0}"", field); continue; } if (field.IndexOf(' ') >= 0) { fields[i] = string.Format(""{0}"", field); continue; } if (field.IndexOf(' ') >= 0) { fields[i] = string.Format(""{0}"", field); continue; } if (field.IndexOf('"') >= 0) { fields[i] = string.Format(""{0}"", field); continue; } if (field != field.Trim()) { fields[i] = string.Format(""{0}"", field); continue; } fields[i] = (i == 3) ? DateTime.Parse(row[i].ToString()).ToString("yyyy/MM/dd HH:mm:ss") : row[i].ToString(); } text = string.Join(",", fields); writer.WriteLine(text); } _logServ.Debug("导出成功"); //Console.WriteLine("导出成功", "提示"); Export = true; } catch (Exception ex) { _logServ.Error("导出失败" + ex.Message); // Console.WriteLine("导出失败" + ex.Message, "提示"); Export = false; } finally { if (writer != null) { writer.Close(); } } return Export; }
3、+过滤掉非法字符
/// <summary> /// 过滤掉非法字符和点字符 /// </summary> /// <param name="directoryName"></param> /// <returns></returns> public String DirectoryNameFilter(String directoryName) { string invalidChars = "\/:*?"<>|."; //自定义非法字符(比系统的多了个.) foreach (char c in invalidChars) { directoryName.Replace(c.ToString(), string.Empty); } return directoryName; } /// <summary> /// 过滤掉非法字符 /// </summary> /// <param name="name"></param> /// <returns></returns> public String NameFilter(String name) { string invalidChars = "\/:*?"<>|"; //自定义非法字符(比系统的多了个.) foreach (char c in invalidChars) { name.Replace(c.ToString(), string.Empty); } return name; }
4、计算文件夹大小
#region 计算文件夹大小 /// <summary> /// 计算文件夹大小 /// </summary> /// <param name="dirPath">文件夹</param> /// <returns></returns> public long CalculateDirSize(string dirPath) { long sumSize = 0; try { if (!Directory.Exists(dirPath)) { DirectoryInfo dirInfo = new DirectoryInfo(dirPath); FileInfo[] files = dirInfo.GetFiles(); foreach (FileInfo fInfo in files) { sumSize += fInfo.Length; } DirectoryInfo[] dirs = dirInfo.GetDirectories(); foreach (DirectoryInfo dInfo in dirs) { sumSize += CalculateDirSize(dInfo.FullName); } } } catch (Exception ex) { LogHelper.Instance.Error("计算文件夹大小(" + dirPath + ")出错:" + ex.Message); } return sumSize; }