I'm looking at handling longer file paths in my windows application.
Currently, I have a text box (edit box) in which a user can type an absolute file path. I then read that typed file path, using GetWindowText
, into a string declared like so: TCHAR FilePath[MAX_PATH];
Obviously, here i'm relying on the MAX_PATH
constant which limits me to 260 chars. So to handle longer file/paths names could I just extend my TCHAR array like this: TCHAR FilePath[32767];
.
Or is there a better way? Could I use a variable length array? (TCHAR FilePath[];
is this even possible in C++? - sorry i'm pretty new to this).
Thank you in advanced!
Here's the whole code snippet of what i mentioned above:
TCHAR FilePath[MAX_PATH];
ZeroMemory(&FilePath, sizeof(FilePath));
GetWindowText(hWndFilePath, FilePath, MAX_PATH);
If you are typing more than 260 characters, then you can either extract the file into the root folder to reduce the characters, or enable the long path support in the system via Registry Editor. You can also go for a third-party ZIP file extraction tool if the troubleshooting methods do not work.
Navigate to Local Computer Policy > Computer Configuration > Administrative Templates > System > Filesystem OR continue to > NTFS. Then double-click the Enable NTFS long paths option and enable it.
There are a number of limitations with respect to file paths on Windows. By default, paths cannot be longer than 260 characters, which is what the MAX_PATH
constant is for.
However, you can access longer paths - with certain limitations - by prefixing the path with "\\?\". However, the limitations of using the "\\?\" prefix usually outweighs the benefit:
LoadLibrary
will always fail on a path that is longer than 260 characters)To be honest, point #2 is the real killer: you open yourself up to all sorts of trouble when using the "\\?\" prefix and you basically have to re-implement the Win32 canonicalization rules yourself if you go that route.
Therefore, my recommendation is to just stick with the 260 limitation. At least until there's better platform support for longer paths.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With