public static void GetFilebyExt_BFS(Queue <string> queue_result, string root, string[] ext)//tìm kiếm theo chiều rộng { var path = root; string[] next = null; try { next = Directory.GetDirectories(path); //lấy ra toàn bộ folder con } catch { } try { next = Directory.GetFiles(path); //lấy ra toàn bộ file trong folder đấy } catch { } if (next != null) { foreach (string item in next) //kiếm tra trong các file tên có chứa đuôi cần tìm { if (XPath.IsEqualExt(item, ext)) { queue_result.Enqueue(item); } } } }
//Chuyển file doc sang rtf để đọc //path là lưu đường dấn private static void DocToRtf(string filePath) { //Creating the instance of Word Application if (chuyendoiRTF != null) { chuyendoiRTF(null, EventArgs.Empty); } Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application(); object Unknown = Type.Missing; if (XPath.IsEqualExt(filePath, new string[] { ".doc", ".docx" })) { try { //lấy địa chỉ file word object Source = filePath; //địa chỉ file rtf tạo ra tạm object Target = XPath.pathfile_rft; //nếu file tồn tại thì xóa đi if (File.Exists(Target.ToString())) { File.Delete(Target.ToString()); } // Use for the parameter whose type are not known or // say Missing // Source document open here // Additional Parameters are not known so that are // set as a missing type newApp.Documents.Open(ref Source, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown); // Specifying the format in which you want the output file object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF; //Changing the format of the document newApp.ActiveDocument.SaveAs(ref Target, ref format, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown); // for closing the application newApp.Quit(ref Unknown, ref Unknown, ref Unknown); } catch (Exception) { } finally { // for closing the application newApp.Quit(ref Unknown, ref Unknown, ref Unknown); } } else { return; } }
public XTextInfo(string path, string keyword) { this.Picture = XImage.LoadImagebyExt(path); this.name = XPath.GetFileNameWithoutExtension(path); this.path = path; this.keyword = keyword; // this.lineresult = XTxt.GetLineHaveKeyWord(path, keyword); }
//path là đường dấn của file đổi tên, newname là tên mới ko gồm đuôi public static void Rename(string path, string newname) { if (File.Exists(path)) { string rootpath = XPath.DirRoot_Info(path); string ext = XPath.GetExtention(path); string newpath = rootpath + "\\" + newname + ext; Move(path, newpath); } }
void AddFileToListBox(string path) { XInfo listitem = new XInfo(XImage.LoadImagebyExt(path), XPath.GetFileNameWithoutExtension(path), path); listBox_timkiem.Invoke((Action)(() => { listBox_timkiem.BeginUpdate(); listBox_timkiem.Items.Add(listitem); listBox_timkiem.EndUpdate(); })); }
//Lấy ảnh từ resources dựa theo đuôi của đường dẫn public static Image LoadImagebyExt(string path) { //lấy ra đuôi của đường dẫn if (System.IO.File.Exists(path)) { if (System.IO.File.GetAttributes(path).HasFlag(System.IO.FileAttributes.Directory)) { return(Properties.Resources.folder); } } string ext = XPath.GetExtention(path); switch (ext) { case ".txt": return(Properties.Resources.txt); case ".pdf": return(Properties.Resources.pdf); case ".doc": return(Properties.Resources.doc); case ".docx": return(Properties.Resources.docx); case ".xls": return(Properties.Resources.xls); case ".xlsx": return(Properties.Resources.xlsx); case ".exe": return(Properties.Resources.exe); case ".jpeg": return(Properties.Resources.jpeg); case ".png": return(Properties.Resources.png); default: break; } return(Properties.Resources.file); }
//Kiểm tra xem path(đường dẫn) có thỏa mãn với bộ lọc có sẵn không public bool IsSatisfy(string path) { bool check = true; if (this.ext != null) { if (XPath.IsEqualExt(path, this.ext)) { check = true; } else { return(false); } } if (this.createTimeFrom != null || this.lenghtMax != 0) { System.IO.FileInfo temp = new System.IO.FileInfo(path); if (this.createTimeFrom != null) { if (temp.CreationTime <= this.createTimeTo && temp.CreationTime >= this.createTimeFrom) { check = true; } else { return(false); } } if (!(System.IO.File.GetAttributes(path).HasFlag(System.IO.FileAttributes.Directory))) { if (this.lenghtMax != 0) { if (temp.Length <= this.lenghtMax && temp.Length >= this.lenghtMin) { check = true; } else { return(false); } } } } return(check); }
//so sánh tên file trong path vs nhiều từ public static bool IsEqualName(string path, string[] keyword) { bool check = false; //chuyển tất cả kí tự về kí tự thường //lấy ra từng đuôi một để so sánh for (int i = 0; i < keyword.Length; i++) { //keyword[i] = XTxt.RemoveSign4VietnameseString(keyword[i]); //if (XTxt.RemoveSign4VietnameseString(XPath.GetFileNameWithoutExtension(path)).IndexOf(keyword[i], StringComparison.CurrentCultureIgnoreCase)>=0) return true;// nếu tm 1 đuôi trong chỗ đuỗi thi thoát if (XPath.GetFileNameWithoutExtension(path).IndexOf(keyword[i], StringComparison.CurrentCultureIgnoreCase) >= 0) { return(true); // nếu tm 1 đuôi trong chỗ đuỗi thi thoát } } return(check); }
//Trả về danh sách path thỏa mãn vào hàng đợi chứa 1 từ trong số các từ tìm kiếm public static void GetAll_BFS(Queue <string> queue_result, string root, string[] keyword, XFilter boloc)//tìm kiếm theo chiều rộng { Queue <string> pending = new Queue <string>(); pending.Enqueue(root); while (pending.Count != 0) { var path = pending.Dequeue(); string[] next = null; try { next = XFolder.GetDirectories(path).ToArray(); //lấy ra toàn bộ folder con foreach (var subdir in next) { pending.Enqueue(subdir); //cho vào trong stackif (next != null) all += next.Count(); } } catch { } if (next != null) { foreach (string item in next) //kiếm tra trong các folder tên có chứa chuối cần tìm { if (boloc.IsSatisfy(item) && XPath.IsEqualName(item, keyword)) { queue_result.Enqueue(item); } } } try { next = XFolder.GetFiles(path).ToArray(); //lấy ra toàn bộ file trong folder đấy } catch { } if (next != null) { foreach (string item in next) //kiếm tra trong các file tên có chứa chuối cần tìm { if (boloc.IsSatisfy(item) && XPath.IsEqualName(item, keyword)) { queue_result.Enqueue(item); } } } } }
//so sánh đuôi file trong path vs ext,ext chứa đuôi cần so sánh public static bool IsEqualExt(string path, string ext) { ext = ext.ToLower(); if (!ext.Contains(".")) { ext = "." + ext; } if (XPath.GetExtention(path) == null) { return(false); } else if (XPath.GetExtention(path).Equals(ext, StringComparison.CurrentCultureIgnoreCase)) { return(true); } else { return(false); } }
//so sánh đuôi file trong path vs ext,ext chứa các đuôi cần so sánh public static bool IsEqualExt(string path, string[] ext) { if (XPath.GetExtention(path) == null) { return(false); } bool check = false; //lấy ra từng đuôi một để so sánh for (int i = 0; i < ext.Length; i++) { //thêm dấu chấm vs chuyển thành chữ thường đề phòng if (!ext[i].Contains(".")) { ext[i] = "." + ext[i]; } if (XPath.GetExtention(path).Equals(ext[i], StringComparison.CurrentCultureIgnoreCase)) { return(true); // nếu tm 1 đuôi trong chỗ đuỗi thi thoát } } return(check); }
public static void GetAllFilebyExt_BFS(Queue <string> queue_result, string root, string[] ext)//tìm kiếm theo chiều rộng { //load danh sách theo chiêu rộng Queue <string> pending = new Queue <string>(); pending.Enqueue(root); while (pending.Count != 0) { var path = pending.Dequeue(); string[] next = null; try { next = Directory.GetDirectories(path); //lấy ra toàn bộ folder con foreach (var subdir in next) { pending.Enqueue(subdir); //cho vào trong stack } } catch { } try { next = Directory.GetFiles(path); //lấy ra toàn bộ file trong folder đấy } catch { } if (next != null) { foreach (string item in next) //kiếm tra trong các file tên có chứa đuôi cần tìm { if (XPath.IsEqualExt(item, ext)) { queue_result.Enqueue(item); } } } } }
//Trả về danh sách path thỏa mãn vào hàng đợi chứa 1 từ trong số các từ tìm kiếm public static void GetOne_BFS(Queue <string> queue_result, string root, string[] keyword, XFilter boloc)//tìm kiếm theo chiều rộng { var path = root; string[] next = null; try { next = XFolder.GetDirectories(path).ToArray(); //lấy ra toàn bộ folder con } catch { } if (next != null) { foreach (string item in next) //kiếm tra trong các folder tên có chứa chuối cần tìm { if (boloc.IsSatisfy(item) && XPath.IsEqualName(item, keyword)) { queue_result.Enqueue(item); } } } try { next = XFolder.GetFiles(path).ToArray(); //lấy ra toàn bộ file trong folder đấy } catch { } if (next != null) { foreach (string item in next) //kiếm tra trong các file tên có chứa chuối cần tìm { if (boloc.IsSatisfy(item) && XPath.IsEqualName(item, keyword)) { queue_result.Enqueue(item); } } } }
//Trả về danh sách path chứa đuôi thỏa mãn các từ trong ext ở trong folder có đường dẫn là root public static IEnumerable <string> GetFilebyExt_DFS(string root, string[] ext)//tìm kiếm theo chiều sâu { //load danh sách theo chiêu sâu Stack <string> pending = new Stack <string>(); pending.Push(root); while (pending.Count != 0) { var path = pending.Pop(); string[] next = null; try { next = Directory.GetDirectories(path); //lấy ra toàn bộ folder con foreach (var subdir in next) { pending.Push(subdir); //cho vào trong stack } } catch { } try { next = Directory.GetFiles(path); //lấy ra toàn bộ file trong folder đấy } catch { } if (next != null) { foreach (string item in next) //kiếm tra trong các file tên có chứa đuôi cần tìm { if (XPath.IsEqualExt(item, ext)) { yield return(item); } } } } }
//Move folder ở sourfolder sang địa chỉ destPath static public void MoveFolderTo(string sourceFolder, string destPath) { string destFolder = destPath + "\\" + XPath.GetFileNameWithoutExtension(sourceFolder); MoveFolder(sourceFolder, destFolder); }
public XInfo(string path) { Picture = XImage.LoadImagebyExt(path); Name = XPath.GetFileNameWithoutExtension(path); this.Path = path; }
//so sánh tên file trong path vs từ search public static bool IsEqualName(string path, string search) { //search = XTxt.RemoveSign4VietnameseString(search); return(XPath.GetFileNameWithoutExtension(path).IndexOf(search, StringComparison.CurrentCultureIgnoreCase) >= 0); //return XTxt.RemoveSign4VietnameseString(XPath.GetFileNameWithoutExtension(path)).IndexOf(search,StringComparison.CurrentCultureIgnoreCase) >= 0; }