0

I need to create files with path exceeding the MAX_PATH limit. What I expected to work is to shorten the already existing directory name like this:

public static String GetShortPathName(String longPath)
{
    StringBuilder shortPath = new StringBuilder(longPath.Length + 1);
    if (0 == GetShortPathName(longPath, shortPath, shortPath.Capacity))
    {
         throw new Exception("Path shortenning failed");
    }

        return shortPath.ToString();
    }

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);

and then use the result to create the new file like:

 using (FileStream writeStream = File.Create(shortPath + newFile))
 ...

But it still throws PathTooLongException like before. Any idea why?

I know I can use Delimon Lib or other lib, but I just cannot figure out why it does not work.

Thanks a lot.

Tomas
  • 449
  • 5
  • 11
  • Try some debugging. Inspect what your program does, what values are returned. Try to understand why it behaves as it does. Don't be helpless. – David Heffernan Apr 06 '16 at 21:51
  • If you wish to call [GetShortPathName](https://msdn.microsoft.com/en-us/library/windows/desktop/aa364989.aspx) with a pathname exceeding **MAX_PATH** characters, you'll have to call the Unicode version and use a "\\?\" prefix for the long path. The result may still exceed **MAX_PATH** characters. – IInspectable Apr 07 '16 at 09:38
  • I explicitly imported the function as [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetShortPathNameW")], but the result is the same. The path looks perfectly shortened and even with the appended filename the length is 125 chars only. It gets shortened even without "\\?\" prefix. So it is definitely shorter than MAX_PATH but it still throws the exception... – Tomas Apr 07 '16 at 18:56
  • So did you do some debugging? Have you inspected the values of the paths? Any chance that you could share this info? – David Heffernan Apr 08 '16 at 07:28
  • Sure. The result of `GetShortPathName` is d:\t\GRAMOD~1\08KLAS~1\BEETHO~1\BEETHO~1.23F\123456~1\ . Filename is "Beethoven XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXu". So the strings are shorter than **MAX_PATH**. – Tomas Apr 08 '16 at 15:21
  • Still the exception is thrown at at System.IO.PathHelper.Append(Char value) at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) at File.Create(String path) – Tomas Apr 08 '16 at 15:22
  • Possible duplicate of [Best way to resolve file path too long exception](http://stackoverflow.com/questions/8745215/best-way-to-resolve-file-path-too-long-exception) – andlabs Apr 11 '16 at 00:28
  • @andlabs: The conclusion of the mentioned post is "The basic idea of the solutions listed below is always the same: Reduce the path-length in order to have path-length + name-length < MAX_PATH". That's exactly what the code does, but the exception is still thrown... – Tomas Apr 11 '16 at 07:52

0 Answers0