文件操作工具,需者自取
Posted 牧师的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件操作工具,需者自取相关的知识,希望对你有一定的参考价值。
先来展示下做到了什么?
首先要导入一个文件夹下的所有文件,要不你怎么操作呢?或者,如果本地有xml文档,可以加载xml文档内容。
图片上的描述已经够看了,我就不费事在这里多写些什么了。
我又仔细的看了一下,貌似也不需要语言上解释什么了,唉,那就这样吧。
================================================================================================
昨天花了三四个小时做的,今天又简单的修改了一下,希望会有人喜欢。
XMLHelper 和 TextHelper 几乎可以说是万能通用工具。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Xml; 6 7 namespace 文章操作工具 8 { 9 public class XMLHelper 10 { 11 public static void SerializeToXml<T>(string filePath, T obj) 12 { 13 using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath)) 14 { 15 System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T)); 16 xs.Serialize(writer, obj); 17 } 18 } 19 20 public static T DeserializeFromXml<T>(string filePath) 21 { 22 if (!System.IO.File.Exists(filePath)) 23 throw new ArgumentNullException(filePath + " not Exists"); 24 25 using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath)) 26 { 27 System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T)); 28 return (T)xs.Deserialize(reader); 29 } 30 31 } 32 } 33 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 7 namespace 文章操作工具 8 { 9 public class TextHelper 10 { 11 public static System.Text.Encoding GetType(string filename) 12 { 13 FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); 14 System.Text.Encoding r = GetType(fs); 15 fs.Close(); 16 return r; 17 } 18 19 public static System.Text.Encoding GetType(FileStream fs) 20 { 21 /* 22 Unicode 23 ------------------ 24 255 254 25 26 ====================== 27 UnicodeBigEndian 28 ------------------- 29 254 255 30 31 ====================== 32 UTF8 33 ------------------- 34 34 228 35 34 229 36 34 230 37 34 231 38 34 232 39 34 233 40 239 187 41 42 ====================== 43 ANSI 44 ------------------- 45 34 176 46 34 177 47 34 179 48 34 180 49 34 182 50 34 185 51 34 191 52 34 194 53 34 196 54 34 198 55 34 201 56 34 202 57 34 205 58 34 206 59 34 208 60 34 209 61 34 210 62 34 211 63 34 213 64 196 167 65 202 213 66 206 228 67 */ 68 BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default); 69 byte[] ss = r.ReadBytes(3); 70 int lef = ss[0]; 71 int mid = ss[1]; 72 int rig = ss[2]; 73 r.Close(); 74 /* 文件头两个字节是255 254,为Unicode编码; 75 文件头三个字节 254 255 0,为UTF-16BE编码; 76 文件头三个字节 239 187 191,为UTF-8编码;*/ 77 if (lef == 255 && mid == 254) 78 { 79 return Encoding.Unicode; 80 } 81 else if (lef == 254 && mid == 255 && rig == 0) 82 { 83 return Encoding.BigEndianUnicode; 84 } 85 else if (lef == 254 && mid == 255) 86 { 87 return Encoding.BigEndianUnicode; 88 } 89 else if (lef == 239 && mid == 187 && rig == 191) 90 { 91 return Encoding.UTF8; 92 } 93 else if (lef == 239 && mid == 187) 94 { 95 return Encoding.UTF8; 96 } 97 else if (lef == 196 && mid == 167 98 || lef == 206 && mid == 228 99 || lef == 202 && mid == 213) 100 { 101 return Encoding.Default; 102 } 103 else 104 { 105 if (lef == 34) 106 { 107 if (mid < 220) return Encoding.Default; 108 else return Encoding.UTF8; 109 } 110 else 111 { 112 if (lef < 220) return Encoding.Default; 113 else return Encoding.UTF8; 114 } 115 } 116 } 117 } 118 }
================================================================================================
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Xml.Serialization; 7 8 namespace 文章操作工具 9 { 10 public class BookInfo 11 { 12 [XmlAttributeAttribute] 13 public long length { set; get; } 14 15 [XmlAttributeAttribute] 16 public string bookname { set; get; } 17 18 //[XmlAttributeAttribute] 19 //public string author { set; get; } 20 21 [XmlAttributeAttribute] 22 public string path { set; get; } 23 24 [XmlAttributeAttribute] 25 public string type { set; get; } 26 public string fullname { get { return path + "/" + bookname; } } 27 } 28 }
================================================================================================
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.IO; 7 using System.Linq; 8 using System.Text; 9 using System.Text.RegularExpressions; 10 using System.Threading.Tasks; 11 using System.Windows.Forms; 12 13 namespace 文章操作工具 14 { 15 public partial class frmMain : Form 16 { 17 private List<BookInfo> booklist = new List<BookInfo>(1000); 18 private List<BookInfo> bindlist = new List<BookInfo>(1000); 19 private readonly string xmlName = "BookInfo.XML"; 20 public frmMain() 21 { 22 InitializeComponent(); 23 24 this.dataGridView1.AutoGenerateColumns = false; 25 this.bookname.DataPropertyName = "bookname"; 26 this.path.DataPropertyName = "path"; 27 this.length.DataPropertyName = "length"; 28 this.author.DataPropertyName = "author"; 29 this.type.DataPropertyName = "type"; 30 31 this.txtPageSize.Text = this.pagesize.ToString(); 32 } 33 34 private void btn_Input_Info_Click(object sender, EventArgs e) 35 { 36 ConsoleWriteLine("导入信息 : " + tex_Path.Text + Environment.NewLine, Color.Green); 37 if (Directory.Exists(tex_Path.Text)) 38 { 39 AllButtonEnable(this, false); 40 BackgroundWorker b = new BackgroundWorker(); 41 b.DoWork += (s, ea) => 42 { 43 Cycle(tex_Path.Text); 44 }; 45 b.RunWorkerCompleted += (s, ea) => 46 { 47 AllButtonEnable(this, true); 48 }; 49 b.RunWorkerAsync(); 50 } 51 } 52 53 private void AllButtonEnable(Control pctl, bool enable) { 54 55 tex_Path.Enabled = enable; 56 AllButtonEnable2(pctl, enable); 57 } 58 59 private void AllButtonEnable2(Control pctl, bool enable) 60 { 61 if (pctl is ButtonBase) 62 { 63 pctl.Enabled = enable; 64 } 65 else 66 { 67 foreach (Control ctl in pctl.Controls) 68 { 69 AllButtonEnable2(ctl, enable); 70 } 71 } 72 } 73 74 private void tex_Path_Click(object sender, EventArgs e) 75 { 76 FolderBrowserDialog g = new FolderBrowserDialog(); 77 g.SelectedPath = Directory.GetDirectoryRoot(AppDomain.CurrentDomain.BaseDirectory); 78 g.ShowNewFolderButton = false; 79 if (g.ShowDialog() == System.Windows.Forms.DialogResult.OK) 80 { 81 if (g.SelectedPath != null && Directory.Exists(g.SelectedPath)) 82 { 83 tex_Path.Text = g.SelectedPath; 84 } 85 } 86 } 87 88 private bool Cycle(string path) 89 { 90 int fileCount = 0; 91 int folderCount = 0; 92 93 if (path == null || path == "") 94 { 95 return false; 96 } 97 98 string[] files = null; 99 try 100 { 101 files = Directory.GetFiles(path); 102 } 103 catch (Exception ex) 104 { 105 ConsoleWriteLine("Directory.GetFiles(" + path + ") Exception : " + ex.StackTrace + Environment.NewLine, Color.Red); 106 } 107 108 if (files != null && (fileCount = files.Length) > 0) 109 { 110 foreach (string file in files) 111 { 112 try 113 { 114 FileInfo fi = new FileInfo(file); 115 if (fi.Length == 0) 116 { 117 ConsoleWriteLine("delete 0 size file : " + fi.FullName + Environment.NewLine); 118 fi.Attributes = fi.Attributes & ~(FileAttributes.Archive | FileAttributes.ReadOnly | FileAttributes.Hidden); 119 fi.Delete(); 120 fileCount--; 121 } 122 else 123 { 124 BookInfo info = new BookInfo 125 { 126 bookname = fi.Name, 127 path = fi.DirectoryName, 128 length = fi.Length, 129 type = fi.Extension 130 }; 131 this.booklist.Add(info); 132 } 133 } 134 catch (Exception ex) 135 { 136 ConsoleWriteLine("FileInfo.Delete(" + file + ") Exception : " + ex.StackTrace + Environment.NewLine, Color.Red); 137 } 138 } 139 } 140 141 string[] folders = null; 142 try 143 { 144 folders = Directory.GetDirectories(path); 145 } 146 catch (Exception ex) 147 { 148 ConsoleWriteLine("Directory.GetDirectories(" + path + ") Exception : " + ex.StackTrace + Environment.NewLine, Color.Red); 149 } 150 151 if (folders != null && (folderCount = folders.Length) > 0) 152 { 153 foreach (string folder in folders) 154 { 155 if (Cycle(folder)) 156 { 157 try 158 { 159 DirectoryInfo di = new DirectoryInfo(folder); 160 ConsoleWriteLine("delete Empty path " + di.FullName + Environment.NewLine); 161 di.Attributes = di.Attributes & ~(FileAttributes.Archive | FileAttributes.ReadOnly | FileAttributes.Hidden); 162 di.Delete(); 163 folderCount--; 164 } 165 catch (Exception ex) 166 { 167 ConsoleWriteLine("DirectoryInfo.Delete(" + path + ") Exception : " + ex.StackTrace + Environment.NewLine, Color.Red); 168 } 169 } 170 } 171 } 172 173 //ConsoleWriteLine("搜索 " + path + " 完毕" + Environment.NewLine); 174 return (folderCount <= 0 && fileCount <= 0); 175 } 176 177 private void ConsoleWriteLine(string msg) 178 { 179 if (!rtx_show.InvokeRequired) 180 { 181 rtx_show.AppendText(msg); 182 rtx_show.ScrollToCaret(); 183 } 184 else 185 { 186 rtx_show.Invoke(new MethodInvoker(delegate { ConsoleWriteLine(msg); })); 187 } 188 } 189 190 private void ConsoleWriteLine(string msg, Color cc) 191 { 192 if (!rtx_show.InvokeRequired) 193 { 194 var v = rtx_show.ForeColor; 195 rtx_show.ForeColor = cc; 196以上是关于文件操作工具,需者自取的主要内容,如果未能解决你的问题,请参考以下文章