September 7th, 2010, 10:17 PM
C# Error: The specified path, file name, or both are too long
I have a C# Windows Form application that searches a file for a particular string. When it gets to files that are in directories that are too long it throws this error:
The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
Here is my function (simplified / psuedo), which called in a loop, reading a list of fully qualified file names:
public string scanFile(string filePath)
/****** ERROR IS THROWN LINE BELOW ******/
TextReader reader = new StreamReader(filePath);
string line = "";
while ((line = reader.ReadLine()) != null)
.....do lots of stuff
if (yada yada)
catch (Exception e)
It is already a mapped drive, saving some characters, but it still is really long for some directories.
September 8th, 2010, 12:15 AM
Can't you temporarily SUBST a large portion of the path?
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.
September 8th, 2010, 10:20 AM
Do you get the same problem when you use a System.IO.FileInfo object?
You can open the file stream from that object and accomplish the same thing you are trying to do.
FileInfo fi = new FileInfo(filePath);
TextReader tr = new StreamReader( fi.OpenRead()) as TextReader;
September 8th, 2010, 02:33 PM
Thanks for the replies.
Not sure what you mean by SUBST, the path is a fixed string, eventually in the code it will have to resolve to the FQDN. Unless you have an example, I guess I don't understand what you mean, sorry.
As far as the FileInfo, I have not tried that, but I will, hopefully that works.
November 15th, 2012, 11:04 AM
The maximum length for a path in Windows used to be 260 characters. Recent versions allow paths up to about but not necessarily exactly 32,767 characters.
I'm assuming that your app is throwing a PathTooLongException. So, with that said, I'm attaching a link to a detailed MSDN discussion on this issue.
Naming paths and file names
Another link that might be of interest to you.