先來展示下做到了什麼? 首先要導入一個文件夾下的所有文件,要不你怎麼操作呢?或者,如果本地有xml文檔,可以載入xml文檔內容。 圖片上的描述已經夠看了,我就不費事在這裡多寫些什麼了。 我又仔細的看了一下,貌似也不需要語言上解釋什麼了,唉,那就這樣吧。 昨天花了三四個小時做的,今天又簡單的修改了一下 ...
先來展示下做到了什麼?
首先要導入一個文件夾下的所有文件,要不你怎麼操作呢?或者,如果本地有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 }XMLHelper
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 }TextHelper
================================================================================================
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 }BookInfo
================================================================================================
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 rtx_show.AppendText(msg); 197 rtx_show.ForeColor = v; 198 rtx_show.ScrollToCaret(); 199 } 200 else 201 { 202 rtx_show.Invoke(new MethodInvoker(delegate { ConsoleWriteLine(msg, cc); })); 203 } 204 } 205 206 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 207 { 208 DataGridView dgv = (DataGridView)sender; 209 210 if (dgv.Columns[e.ColumnIndex].Name == "delete") 211 { 212 try 213 { 214 if (MessageBox.Show("確認要刪除嗎?", "警告", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK) 215 { 216 BookInfo info = (BookInfo)dgv.Rows[e.RowIndex].DataBoundItem; 217 string fullname = info.fullname; 218 this.booklist.Remove(info); 219 this.bindlist.Remove(info); 220 bindData(); 221 FileInfo fi = new FileInfo(fullname); 222 fi.Attributes = fi.Attributes & ~(FileAttributes.Archive | FileAttributes.ReadOnly | FileAttributes.Hidden); 223 fi.Delete(); 224 ConsoleWriteLine("delete file : " + fullname + Environment.NewLine, Color.Green); 225 } 226 } 227 catch (Exception ex) 228 { 229 ConsoleWriteLine("FileInfo.Delete(" + bookname + ") Exception : " + ex.StackTrace + Environment.NewLine, Color.Red); 230 } 231 } 232 233 if (dgv.Columns[e.ColumnIndex].Name == "load") 234 { 235 try 236 { 237 BookInfo info = (BookInfo)dgv.Rows[e.RowIndex].DataBoundItem; 238 string fullname = info.fullname; 239 if (isLeft) 240 { 241 textBox1.Text = File.ReadAllText(fullname, TextHelper.GetType(fullname)); 242 } 243 else 244 { 245 textBox2.Text = File.ReadAllText(fullname, TextHelper.GetType(fullname)); 246 } 247 isLeft = !isLeft; 248 ConsoleWriteLine("load file : " + fullname + Environment.NewLine, Color.Green); 249 } 250 catch (Exception ex) 251 { 252 ConsoleWriteLine("FileInfo.Delete(" + bookname + ") Exception : " + ex.StackTrace + Environment.NewLine, Color.Red); 253 } 254 } 255 } 256 257 private bool isLeft = true; 258 259 private void btnSearch_Click(object sender, EventArgs e) 260 { 261 AllButtonEnable(this, false); 262 string bookname = txtName.Text; 263 ConsoleWriteLine("search file : " + bookname + Environment.NewLine, Color.Green); 264 try 265 { 266 if (!string.IsNullOrEmpty(bookname)) 267 { 268 this.bindlist = this.booklist.FindAll((i) => { return i.bookname.Contains(bookname); }); 269 } 270 else 271 { 272 this.bindlist = this.booklist; 273 } 274 275 this.curpage = 1