What is the maximum literal address for a local file?

I thort it was 255 but when tried I found it only == 248 CHAR full LITERAL filename & path string length!!!

How do I determine what the user can enter before submitting it to the OS as the intended filename




Answer this question

What is the maximum literal address for a local file?

  • IS dude

    The limitation depends on the OS, the character set used and the format of the file name. The defined maximum is normally considered to be approximately 260 (MAX_PATH in Win32) but it could be less. The Unicode version using a full object name is supposedly 32K total with each part limited to approximately 255 but you won't be able to use that format in .NET.

    Irrelevant you shouldn't hard code the value as it could change over time. There is no property or method in .NET to determine the maximal value because it varies.

    Michael Taylor - 1/4/07
    http://p3net.mvps.org


  • silence421

    Yup thanks Peter they use a literal so I can too but I need to find out how much of that literal has been used up so far, any ideas on how I should do that

  • psmNATx

    Hi Micheal,

    thanks for that. lol

    You say 'Irrelevant you shouldn't hard code the value as it could change over time. There is no property or method in .NET to determine the maximal value because it varies.' and that does make sense but can you please inform me what the call would be to enable this kind of coding

    systemlength = ! getMAXSystemFilenameLength(); That sort of call is what I need to be able to do so I would have to be

    using {} R what to enable a call like that .

    Like this call: using System.IO;

    then I could basically allow user entry of (systemlength-currentlength) couldn't I




  • Ramanakumar

    I think you're trying to make this too complicated just to handle a boundary condition. Assume the maximum file length is 260 and the maximum component (directory) length is 248. Define static fields, create public properties or whatever to hide these details. Create a class to hold them. Within this same class create a static method that validates the file name length. It would validate the entire string length in addition to the component length. Assuming that your tree is built up one "directory" at a time then all you need to validate is the newly added name's length and the total file name length.

    For mapping a tree node back to the path you can use the FullPath property provided each node in your tree maps to a directory. You'll have to handle the root node specially. Nevertheless given the following tree:

    c:
    Dir1
    Dir2
    Dir3

    FullPath = c:\Dir1\Dir2\Dir3

    Alternatively store the full path in the Tag property and just use it instead. Never the less you'll have to decide the best way to map your nodes to the actual path. I believe that is OT for this post.

    Ultimately all you're trying to do is prevent one exception out of possibly many from occurring. If you try to create some elaborate system that works around the .NET limit or relies on the volume information then you are simply adding complexity to an otherwise rare situation. I'd keep it simple and just do basic length validation and let the framework deal with the rest.

    Michael Taylor - 1/8/07
    http://p3net.mvps.org


  • Mike Hildner

    Yes Peter, thanks  but I really need to be able to find out the machine's specific maximum path&file name (as they all differ) so I can restrict the user of my intended program from making an 'invalid filename! 'Brrrump!'  I liked Michael Taylor's suggestion of 'You can use the GetVolumeInformation Win32 API to retrieve the maximum length ' but would love to know the syntax I should use as C# is so specific ain't it!  

    Like so many methods but you have to be aware of the methods to use them right lol



  • Norbert.Bender

    You could define a constant MAX_PATH and set it to 260 then you subtract the current length of the filename to get the number of characters left. It is possible that the MAX_PATH is set 256 in Windows 95, 98 and Windows Me (not sure though) but as mentioned here before you cannot create files using the Base Class Library that exceed 260 characters and a maximum of 248 characters for a folder name.



  • Ed.S.

    Well Gabriel and Peter I find myself back to square one! Here's a straightforward catagorical question for you both.

    How do I translate userDatalength = systemMaxFilename.length-currentFilename.length;

    Basically I just need the jargon to convert my logical idea into GeekSpeak!

    A sad lacking in the Visual Studio 2005 Express edition me thinks.



  • Cam Evenson

    Again thanks Gabriel but how do i determine the 'current length of the filename' to 'subtract'

    Is there a certain class or method that would hold that data so I get extract it please

    I guess I will use the 260char literal max as the others would not be very many yet would they!



  • BrentB

    What I'm saying is it doesn't matter what path lengths the system can handle, the framework is hard-coded to only handle 260 character path names (and 248 character file names). It doesn't matter if you pinvoke a win32 function to get more detail, the .NET framework won't let you deal with anything over 260 anyway.



  • Pavan Contractor

    Um actually Gabriel my users will basically be creating the name of the file dependant on whereabouts in the tree the wish to place it which will determine how big the their actual filename can be but I don't want them to get that annoying !Brrrump! sound.

    The selected text from the current window is a prefix to what they can then specify as the addendum for themselves.

    I will automatically be calculating drive and subfolders as part of the literal 260 total allowed. lol



  • James Boman

    Actually NTFS allows path names with a length of up to 32K but the underlying API's are limited to path names of a length of 260 bytes. As mentioned before there are some Unicode versions of API functions that allow 32K path lengths. However when you use these API's you have no guarantee that the shell can handle these files...

  • vasudupe

    You can use the GetVolumeInformation Win32 API to retrieve the maximum length of an individual component of a file name. The maximum path length total is defined in a C header file. The Path class has an internal field that contains the value but it is hard coded to 260. Therefore your best option is to define your own public read-only field that uses 260 and use that in all your code. If MS would have exposed the maximum length as a property then you would have been able to do that but they didn't. BTW the Path class also internally defines the maximum length of a directory name to be 248 characters.

    Michael Taylor - 1/5/07
    http://p3net.mvps.org


  • RameshK

    DOSrelic wrote:

    Again thanks Gabriel but how do i determine the 'current length of the filename' to 'subtract'

    Is there a certain class or method that would hold that data so I get extract it please

    I guess I will use the 260char literal max as the others would not be very many yet would they!

    As I have understand it your users can enter filenames in a textbox Can they enter the drive as well and subfolders Once you have the string you get the length of the string with the Length property of the string.

    @"c:\temp\readme.txt".Length



  • Snow Moon

    The .NET framework does not support filenames larger than 260 characters, regardless of OS.

  • What is the maximum literal address for a local file?