DirectoryInfo does not return full path
-
Hi All. I have a slight problem in that my DirectoryInfo does not return the full path. Does anybody know if there is a limitation to the DirectoryInfo? The path to the files is something like this:
C:\\Folder1\\Folder2\\Folder3\\Folder4\\Folder5
My code is as follows:private void getAllFiles(string sExt, string sFolder)
{
DirectoryInfo di = new DirectoryInfo(sFolder);
FileInfo[] fi = di.GetFiles(sExt, SearchOption.AllDirectories);
//List<FileInfo> fi = new List<FileInfo>(di.EnumerateFiles(sExt, SearchOption.AllDirectories));
foreach (FileInfo file in fi)
{
AddNewFile(Path.Combine(di.ToString(), file.ToString()));
counter++;
}
lblTotalFiles.Text = counter.ToString();
//SetText(counter.ToString());
}FileInfo seems to get the location of the file as it shows me the file it picked up but when the path of DirectoryIfo is passed on it excludes Folder5 thus the object I am passing it to doesn't find the file in question. Is there anyway I can fix this?
Excellence is doing ordinary things extraordinarily well.
-
Hi All. I have a slight problem in that my DirectoryInfo does not return the full path. Does anybody know if there is a limitation to the DirectoryInfo? The path to the files is something like this:
C:\\Folder1\\Folder2\\Folder3\\Folder4\\Folder5
My code is as follows:private void getAllFiles(string sExt, string sFolder)
{
DirectoryInfo di = new DirectoryInfo(sFolder);
FileInfo[] fi = di.GetFiles(sExt, SearchOption.AllDirectories);
//List<FileInfo> fi = new List<FileInfo>(di.EnumerateFiles(sExt, SearchOption.AllDirectories));
foreach (FileInfo file in fi)
{
AddNewFile(Path.Combine(di.ToString(), file.ToString()));
counter++;
}
lblTotalFiles.Text = counter.ToString();
//SetText(counter.ToString());
}FileInfo seems to get the location of the file as it shows me the file it picked up but when the path of DirectoryIfo is passed on it excludes Folder5 thus the object I am passing it to doesn't find the file in question. Is there anyway I can fix this?
Excellence is doing ordinary things extraordinarily well.
Kwagga wrote:
FileInfo[] fi = di.GetFiles(sExt, SearchOption.AllDirectories); .... Path.Combine(di.ToString(), file.ToString())
These lines may not be doing what you assume. SearchOption.AllDirectories tells GetFiles to search the starting directory and all subdirectories. The ToString() method of FileInfo's created by GetFiles returns just the filename without any path information. Your use of Path.Combine creates the correct full path only for files in the starting directory. If your aim is to pass the full path of each file to the AddNewFile method just use the FileInfo.FullName property without Path.Combine. i.e.
AddNewFile(file.FullName);
Alan. -
Kwagga wrote:
FileInfo[] fi = di.GetFiles(sExt, SearchOption.AllDirectories); .... Path.Combine(di.ToString(), file.ToString())
These lines may not be doing what you assume. SearchOption.AllDirectories tells GetFiles to search the starting directory and all subdirectories. The ToString() method of FileInfo's created by GetFiles returns just the filename without any path information. Your use of Path.Combine creates the correct full path only for files in the starting directory. If your aim is to pass the full path of each file to the AddNewFile method just use the FileInfo.FullName property without Path.Combine. i.e.
AddNewFile(file.FullName);
Alan.