חיפוש קבצים בc#

יטרנר

New member
חיפוש קבצים בc#

שלום לכולם, ניסיתי לקבל שם של קובץ ולהדפיס (במידה ויש) את כל הקבצים שרשומים על שם זה בסיומות נתמכות.לדוג', dan.txt dan.doc ניסיתי לעשות זאת וזה לא עבד לי. אודה לכם מאדדדדד אם תוכלו לסייע לי בזה. הבעיה נמצאת בשאלה 2 בתפריט:
using System; using System.Collections.Generic; using System.Text; namespace Project_A { struct FileData { //declare three variables in a struct. public string path; public string Name; public string Extension; //initializing the variables of the struct using a file path. public void Load(string filePath) { //another alternative is use code snippet for Path: Path.GetExtension\Path.GetFileName\ Path.GetFileNameWithoutExtension; string[] str = filePath.Split('.'); Extension = str[str.Length - 1]; string[] str1 = filePath.Split('\\'); Name = str1[str1.Length - 1]; for (int i = 0; i < str1.Length - 1; i++) { path += str1 + '\\'; } } //print the values of the variables public void Print() { Console.WriteLine("path:{0}\r\nname:{1}\r\nextension:{2}\r\n", path, Name, Extension); } } } using System.Collections.Generic; using System.Text; using System.IO; namespace Project_A { class Program { static void Main(string[] args) { Console.WriteLine("Please enter a full file name and path which includes supported extensions"); string FileTypes = Console.ReadLine(); string[] my_extensions = GetFileTypes(FileTypes); Console.WriteLine("Please enter a path"); string folder = Console.ReadLine(); FileData[] arr2 = GetFileData(folder, my_extensions); if (arr2.Length > 0) { //print the file names. for (int i = 0; i < arr2.Length; i++) { arr2.Print(); } } //else //{ // //The array is empty. // Console.WriteLine("No files were found."); //} break; } } /// <summary> /// reads a file with extensions and creats a list of extensions /// </summary> /// <param name="FileTypes">gets a file types</param> /// <returns> an array which includes a list of extensions</returns> public static string[] GetFileTypes(string FileTypes) { StreamReader sr = new StreamReader(FileTypes); string rd = sr.ReadToEnd(); string[] str = rd.Split(';'); return str; } /// <summary> /// gets a list of files which end with supported extensions &nbsp
 

יטרנר

New member
העתקתי קוד שגוי, הנה הקוד

Console.WriteLine("Please enter a path"); string folder = Console.ReadLine(); FileData[] arr2 = GetFileData(folder, my_extensions); if (arr2.Length > 0) { //print the file names. for (int i = 0; i < arr2.Length; i++) { arr2.Print(); } } //else //{ // //The array is empty. // Console.WriteLine("No files were found."); //} break;
/// <summary> /// gets a list of files which end with supported extensions /// </summary> /// <param name="path">the path we'd like to search in</param> /// <param name="extensions">an array with a list of extensions</param> /// <returns>file data in FileData[] type</returns> public static FileData[] GetFileData(string path, string[] extensions) { int count = 0; for (int i = 0; i < extensions.Length; i++) { string[] files = Directory.GetFiles(path, extensions, SearchOption.AllDirectories); count += files.Length; //Console.WriteLine("{0} {1} files", files.Length, extensions); } //for checking if it works well: //Console.WriteLine("----------"); //Console.WriteLine("{0} files total",count); FileData[] arr = new FileData[count]; for (int i = 0; i < extensions.Length; i++) { string[] files1 = Directory.GetFiles(path, extensions, SearchOption.AllDirectories); for (int j = 0; j < files1.Length; j++) { arr[j].Load(files1[j]); } } return arr; }
using System; using System.Collections.Generic; using System.Text; namespace Project_A { struct FileData { //declare three variables in a struct. public string path; public string Name; public string Extension; //initializing the variables of the struct using a file path. public void Load(string filePath) { //another alternative is use code snippet for Path: Path.GetExtension\Path.GetFileName\ Path.GetFileNameWithoutExtension; string[] str = filePath.Split('.'); Extension = str[str.Length - 1]; string[] str1 = filePath.Split('\\'); Name = str1[str1.Length - 1]; for (int i = 0; i < str1.Length - 1; i++) &nbsp​
 
למעלה