PathTooLongException problem/question

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
 
J

Justin

Thanks Chris. I had tried exploring the PathTooLongException, but
didn't find anything useful. The e.Message gives the same message that
prints out, and the InnerException gives nothing because it's null. I
know the information exists somewhere because as I said, I can see the
offending file in the Locals window as I'm debugging the code. Thanks
for the reply.
Justin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top