02-12-2011, 22:11
|
|
|
חבר מתאריך: 10.06.11
הודעות: 34
|
|
צריך עזרה קטנה ב-C#
אני רושם class שסורק רקורסיבי path מסויים ומחזיר את כל הקבצים באותו ה-path לתוך SortedDictionary.
כשאני רושם את המתודה שסורקת ומחזירה את התוצאה ככה שהיא לא מקבלת פרמטרים בחתימה היא לא עובדת לי, מה שקורה זה שאני עושה בדיקה וסורק תיקיה מסויימת שבה 2 קבצים ועוד תיקיה אחת בתוכה עם עוד קובץ, בסה"כ 3 קבצים.
את התיקיה הראשית עם 2 הקבצים זה מזהה ומכניסה אותם ל-
קוד:
SortedDictionary<string, FileInfo>
מה שקורה זה אח"כ ב-foreach השני היא מזהה את התיקיה השנייה שבתוך התיקיה הראשית ואז יוצאת החוצה מהמתודה ואני לא מצליח להבין למה.
זה ה-class:
קוד:
public class recursion
{
public string mPath {get; private set;}
public DirectoryInfo mDirectory { get; private set; }
public SortedDictionary<string, FileInfo> mFilesFromPath;
public recursion(string path)
{
mPath = path;
mDirectory = new DirectoryInfo(mPath);
mFilesFromPath = new SortedDictionary<string, FileInfo>();
GetFiles();
}
public SortedDictionary<string, FileInfo> GetFiles()
{
FileInfo[] files;
files = mDirectory.GetFiles("*.*");
foreach (FileInfo file in files)
{
mFilesFromPath.Add(file.FullName, file);
}
DirectoryInfo[] dirs = mDirectory.GetDirectories("*.*");
foreach (DirectoryInfo directory in dirs)
{
GetFiles();
}
return mFilesFromPath;
}
}
קוד:
ה-main:
static void Main(string[] args)
{
string path = (@"C:\Users\Administrator\Desktop\temp");
recursion rec = new recursion(path);
Console.ReadLine();
}
לעומת זאת עם אני רושם את אותה המתודה ככה שהיא מקבלת בחתימה שלה DirectoryInfo מסויים זה עובד
קוד:
class:
public class recursion
{
public string mPath;
public SortedDictionary<string, FileInfo> mAllFilesWithinPath;
public recursion(string path)
{
mPath = path;
DirectoryInfo mDirectory = new DirectoryInfo(mPath);
mAllFilesWithinPath = new SortedDictionary<string, FileInfo>();
GetFiles(mDirectory);
}
public SortedDictionary<string, FileInfo> GetFiles(DirectoryInfo dirc)
{
List<string> allFiles = new List<string>();
FileInfo[] files;
files = dirc.GetFiles("*.*");
foreach (FileInfo file in files)
{
try
{
mAllFilesWithinPath.Add(file.FullName, file);
}
catch (Exception)
{
}
}
DirectoryInfo[] dirs = dirc.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
GetFiles(dir);
}
return mAllFilesWithinPath;
}
}
main:
קוד:
static void Main(string[] args)
{
string path = @"D:\Downloads";
recursion rec = new recursion(path);
Task getFilesTask = new Task(() => rec.GetFiles(new DirectoryInfo(path)));
Console.ReadLine();
}
אשמח אם מישהו יוכל להסביר לי מה פה הבעיה שלי.
|