none
How to Programmatically Get MAX_PATH for a PC RRS feed

  • Question

  • My C#, .NET 4.7 application builds new file paths in a process which renames directories.  I have read that MAX_PATH for Windows 10 is 256 characters.  I have also read that it is 260 characters.  And... I have read that it can be changed to a greater limitation in the Registry.

    I do not want to throw an exception MAX_PATH is exceeded.  My strategy is to alert the user before that occurs. So, how can I to programmatically find the true value of MAX_PATH from the current runtime environment?


    Rob E.

    Friday, January 3, 2020 8:13 PM

Answers

  • I have read that MAX_PATH for Windows 10 is 256 characters.  I have also read that it is 260 characters.  And... I have read that it can be changed to a greater limitation in the Registry.

    Where have you read that? It will help us help you if you tell us where so we can explain it in context.

    Do you understand what C++ macros are? The real MAX_PATH is a macro in Windows API headers for C++; specifically the following line in minwindef.h:

    #define MAX_PATH          260

    So the concept of finding it from the current runtime environment seems nebulous to me.

    Likely it is a mistake to limit the question to MAX_PATH. You probably want to ask a more general question about the maximum path length. So you can do a search as in this Search and then you should find Naming Files, Paths, and Namespaces that seems helpful.



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by REvans611 Saturday, January 4, 2020 11:10 PM
    Saturday, January 4, 2020 5:24 AM
  • when you want to know if long path is enabled, you just have to read the value of LongPathsEnabled in the registry key HKLM\SYSTEM\CurrentControlSet\Control\FileSystem

    As this key is mentioned in the link you posted yourself:
    is your question how to read from the registry in C#?
    • Marked as answer by REvans611 Saturday, January 4, 2020 11:10 PM
    Saturday, January 4, 2020 4:01 PM

All replies

  • I have read that MAX_PATH for Windows 10 is 256 characters.  I have also read that it is 260 characters.  And... I have read that it can be changed to a greater limitation in the Registry.

    Where have you read that? It will help us help you if you tell us where so we can explain it in context.

    Do you understand what C++ macros are? The real MAX_PATH is a macro in Windows API headers for C++; specifically the following line in minwindef.h:

    #define MAX_PATH          260

    So the concept of finding it from the current runtime environment seems nebulous to me.

    Likely it is a mistake to limit the question to MAX_PATH. You probably want to ask a more general question about the maximum path length. So you can do a search as in this Search and then you should find Naming Files, Paths, and Namespaces that seems helpful.



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by REvans611 Saturday, January 4, 2020 11:10 PM
    Saturday, January 4, 2020 5:24 AM
  • Where have you read that? It will help us help you if you tell us where so we can explain it in context.

    How about this link for starters: MAX_PATH.

    Your suggested link, Naming Files, Paths, and Namespaces, in the sections entitled "Maximum Path Length Limitation" and "Enable Long Paths in Windows 10, Version 1607, and Later" both do a better job of explaining.

    I am hoping there exists some nifty C# method that would access an environment variable which would reveal the whether long paths has been enabled.



    Rob E.



    • Edited by REvans611 Saturday, January 4, 2020 2:22 PM clarification
    Saturday, January 4, 2020 1:50 PM
  • You should refer to the authoritative Microsoft documentation in the link to "Naming Files, Paths and Namespaces" that Sam Hobbs provided in his post

    It clearly states "In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters."

    You should also recognize that there is also maximum limit to the size of the components that comprise a fully qualified path.  This is also discussed in the above referenced link.

    Saturday, January 4, 2020 2:14 PM
  • The obvious thing would be to check the registry entry right? There is the Registry class under Microsoft.Win32 right?

    But please remember that this will not technically change MAX_PATH. Also as an interesting note, MAX_PATH is 260 because it is made up of the drive specifier (C:\ = 3 characters) the maximum path component is 256 characters and it is terminated by a null character. So 256 + 3 + 1 = 260.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Edited by Darran Rowe Saturday, January 4, 2020 2:46 PM
    Saturday, January 4, 2020 2:44 PM
  • when you want to know if long path is enabled, you just have to read the value of LongPathsEnabled in the registry key HKLM\SYSTEM\CurrentControlSet\Control\FileSystem

    As this key is mentioned in the link you posted yourself:
    is your question how to read from the registry in C#?
    • Marked as answer by REvans611 Saturday, January 4, 2020 11:10 PM
    Saturday, January 4, 2020 4:01 PM
  • is your question how to read from the registry in C#?

    Nope.  That would have been an entirely different question, but your suggestion to read the value of LongPathsEnabled is the apparent answer to my original post.  Like I said previously, "I am hoping there exists some nifty C# method that would access an environment variable which would reveal the whether long paths has been enabled."

    Well, it seems I hoped in vain, and now I will learn how to query the Registry.



    Rob E.

    Saturday, January 4, 2020 11:10 PM
  • when you want to know if long path is enabled, you just have to read the value of LongPathsEnabled in the registry key HKLM\SYSTEM\CurrentControlSet\Control\FileSystem

    Is that the solution documented by Microsoft? Developers often use the first solution that works but if there is an official (documented) way of doing something then using an undocumented solution, even if it works, is a mistake. If we use the documented solution then Microsoft will support it.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, January 4, 2020 11:17 PM
  • Using query windows long path enabled - Google Search I found winapi - How to programmatically check if the current process is long path aware on Windows? - Stack Overflow and there is very much discussion there. Before reading, anchor yourself firmly to your desk because that thread is one of those that will get your head spinning.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, January 4, 2020 11:26 PM
  • Before reading, anchor yourself firmly to your desk because that thread is one of those that will get your head spinning.

    Just when I thought it was okay to go back into the water!  And to complicate things, my .NET 4.7 app is soon to be converted to .NET Core so that I can submit it to Microsoft Store; not that it is germane to this post.

    My WPF application has a lot of System IO, much of which renames directories and renames files.  It's important to determine if the newly revised paths are too long.



    Rob E.

    Sunday, January 5, 2020 12:02 AM