c#.

יטרנר

New member
c#.

מה הבעיה בפיתרון שלי? ניסיתי באמצעות path & extension למצוא קבצים, לשלוח אותם למבנה נתונים ולהדפיסם. במקום זה יוצא לי פירוק של כתובת הקובץ, כלומר: path- c\project\ & extension-*.txt יוצא לי: path:c:\, name-project, extension-doc ישמח אותי כל כך אם מישהו ימצא היכן טעיתי, אני כבר הרבה זמן מנסה לפתור זאת...אלף תודות הפיתרון שלי: struct fileData { //declare on 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) { //we can also 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[str.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); } static void Main(string[] args) { ////targil 4 Console.WriteLine("please enter a path"); string path = Console.ReadLine(); Console.WriteLine("please enter an extension"); string extension = Console.ReadLine(); fileData[] arr2 = GetFileData(path, extension); for (int i = 0; i < arr2.Length; i++) { arr2.Print(); } {/// <summary> /// search for files from specific path and extension /// </summary> /// <param name="path">the path we'd like to search in</param> /// <param name="extension">the extension of the files we look for</param> /// <returns> an array of matched files in a fileData[] value type</returns> public static fileData[] GetFileData(string path, string extension) { string[] files = Directory.GetFiles(path, extension, SearchOption.AllDirectories); fileData[] arr = new fileData[files.Length]; for (int i = 0; i < arr.Length; i++) { arr.Load(files); // arr.Print(); // } return arr; } } }
 
למעלה