J
Justin
Hi. I'm writing a little c# program to list all the files in a
selected directory longer than a given size. The problem is that when
it finds a file that is too long for Windows, I get the
PathTooLongException error. I try to handle the exception, but can't
do anything because, well, the file is too long. I can see what and
where the offending file is in the Locals window, so .NET can obviously
read the file path, but I can't write that to any variable. Any
suggestions? (And how certain files that are too long get there in the
first place is another question for another day...) Following is an
outline of my code:
* user selects a directory from a treeview
* code calls the AddFilesToList method which iterates through the files
in the chosen directory
* when files are longer than e.g. 100 characters the file name is
written to an arraylist
* the subfolders of the chosen directory are iterated through each
recursively calling AddFilesToList
And the AddFilesToList method. (For some reason the spaces at the
start of lines aren't coming out, so I hope this is worth something.)
private void AddFilesToList(DirectoryInfo dir)
{
FileInfo[] files = dir.GetFiles();
DirectoryInfo[] subdirs = dir.GetDirectories();
foreach(FileInfo fi in files)
{
try
{
if(fi.FullName.Length >
Convert.ToInt32(txtFileLengthCutoff.Text))
{
Debug.WriteLine("try file: " + fi.FullName);
longFiles.Add(fi.FullName); // this is the arraylist
}
}
catch (System.IO.PathTooLongException e)
{
Debug.WriteLine("catch file: " + fi.FullName);
// string temp = fi.DirectoryName;
// temp = fi.FullName;
longFiles.Add(fi.FullName);
}
}
foreach (DirectoryInfo di in subdirs)
{
AddFilesToList(di);
}
}
Thanks for any help,
Justin
selected directory longer than a given size. The problem is that when
it finds a file that is too long for Windows, I get the
PathTooLongException error. I try to handle the exception, but can't
do anything because, well, the file is too long. I can see what and
where the offending file is in the Locals window, so .NET can obviously
read the file path, but I can't write that to any variable. Any
suggestions? (And how certain files that are too long get there in the
first place is another question for another day...) Following is an
outline of my code:
* user selects a directory from a treeview
* code calls the AddFilesToList method which iterates through the files
in the chosen directory
* when files are longer than e.g. 100 characters the file name is
written to an arraylist
* the subfolders of the chosen directory are iterated through each
recursively calling AddFilesToList
And the AddFilesToList method. (For some reason the spaces at the
start of lines aren't coming out, so I hope this is worth something.)
private void AddFilesToList(DirectoryInfo dir)
{
FileInfo[] files = dir.GetFiles();
DirectoryInfo[] subdirs = dir.GetDirectories();
foreach(FileInfo fi in files)
{
try
{
if(fi.FullName.Length >
Convert.ToInt32(txtFileLengthCutoff.Text))
{
Debug.WriteLine("try file: " + fi.FullName);
longFiles.Add(fi.FullName); // this is the arraylist
}
}
catch (System.IO.PathTooLongException e)
{
Debug.WriteLine("catch file: " + fi.FullName);
// string temp = fi.DirectoryName;
// temp = fi.FullName;
longFiles.Add(fi.FullName);
}
}
foreach (DirectoryInfo di in subdirs)
{
AddFilesToList(di);
}
}
Thanks for any help,
Justin