#1
  1. No Profile Picture
    Participant
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Location
    Hawaii
    Posts
    376
    Rep Power
    17

    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:

    LOG Code:
    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:

    C# Code:
    public string scanFile(string filePath)
            {
                try
                {
                    /****** ERROR IS THROWN LINE BELOW ******/
                    TextReader reader = new StreamReader(filePath);
                    string line = "";
                    while ((line = reader.ReadLine()) != null)
                    {
                        .....do lots of stuff
                    }
                    if (yada yada)
                    {
                        return report;
                    }
                    else
                    {
                        return null;
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
     
            }


    It is already a mapped drive, saving some characters, but it still is really long for some directories.

    Ideas? :chomp:

    SK
  2. #2
  3. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    27
    Rep Power
    0
    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.
    Code:
                FileInfo fi = new FileInfo(filePath);
                TextReader tr = new StreamReader( fi.OpenRead()) as TextReader;
  6. #4
  7. No Profile Picture
    Participant
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Location
    Hawaii
    Posts
    376
    Rep Power
    17
    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.

    Thanks,

    SK
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    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

    **EDIT**

    Another link that might be of interest to you.

IMN logo majestic logo threadwatch logo seochat tools logo