Question

I am using a SaveFileDialog and have to set long string(longFileName) in FileName. String longFileName is known at Runtime.

If I set

saveFileDialog.FileName = longFileName ;

then I get System.IO.PathTooLongException.

How can I do it?

Was it helpful?

Solution

In short, you can't; NTFS has a maximum filename size of 256.

My advice would be shorten your filename.

OTHER TIPS

Although you can have file names longer than 260 characters, you can run into some weirdness with the API. Also, .net doesn't directly support it.

If you want more information, here is a link to the first of three posts on the BCL Team Blog about the issue: http://blogs.msdn.com/bclteam/archive/2007/02/13/long-paths-in-net-part-1-of-3-kim-hamilton.aspx

The answer lies in the following:

Q: What is the maximum number of characters a filename can be?

A: This depends on if the file is being created on a FAT or NTFS partition. The maximum filename length on a NTFS partition is 256 characters, and 11 characters on FAT (8 character name, . , 3 character extension). NTFS filenames keep their case, whereas FAT filenames have no concept of case (however the case is ignored when performing a search etc on NTFS). There is the new VFAT which also has 256 character filenames.

Source

Basically, the OS determines the max file size, not your app. If a better file system is released in the future that supports more characters you could always make your software forward compatible by allowing it, and letting the user know when the path is too long for now. However, with the current progress of technology, you cannot use a file name greater than 256 characters.

You could try the short path version of the file name by using:

  [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  public static extern int GetShortPathName(
     [MarshalAs(UnmanagedType.LPTStr)] System.String path,
     [MarshalAs(UnmanagedType.LPTStr)] System.Text.StringBuilder shortPath,
     System.Int32 shortPathLength);

Then the save file dialog would look something like this:

     string long_path = @"C:\Program Files\Microsoft Visual Studio 8\Common7\Packages\Debugger\1033\vsdebugui.dll";
     StringBuilder s = new StringBuilder(long_path.Length);
     int short_path_length = GetShortPathName(long_path, s, long_path.Length);
     string short_path = s.ToString();
     SaveFileDialog save_d = new SaveFileDialog();
     save_d.FileName = short_path;
     save_d.ShowDialog();

You can't.

Remember that paths in Windows must be less than 256 characters. (Unless you use a special trick)

Why do you want such a long filename in the first place?

As others have mentioned, there is no good way around this limitation. However the problem is NOT with NTFS, its in Win32. Using SFU you can make some ridiculously long paths if you really want to, but it makes interop with native Win32 stuff interesting.

You might want to try prepending the path with \\?\. I've never tried this with .NET though.

Add \\?\ to the beginning of the path. So D:\<very_long_path> would become \\?\D:\<very_long_path>. See for reference "Maximum Path Length Limitation" in http://msdn.microsoft.com/en-us/library/aa365247.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top