peteraritchie / LongPath

drop-in library to support long paths in .NET
GNU Lesser General Public License v3.0
112 stars 43 forks source link

Problems with reserved DOS names #56

Open wischi-chr opened 7 years ago

wischi-chr commented 7 years ago

CON, AUX, LPT, etc. were reserved names in MsDos. For compatibility Windows Explorer still prevents you from creating files or directories with those names. But in fact Windows internally does support it (like it supports long filenames even tho the Explorer does not).

Would be great if those names would be handled correctly. The following example demonstrates that. Pri.LongPath is not able to create a directory named "aux".

using System;
using System.Runtime.InteropServices;
using Pri.LongPath;

namespace MetaTree
{
    class Program
    {
        //Make sure temp already exists!
        private readonly static string temp = @"D:\Temp\";

        static void Main(string[] args)
        {
            //[System.ArgumentException]: The UNC path should be of the form \\server\share.
            Directory.CreateDirectory(temp + "aux");

            //With the WinAPI a prefix is needed, but it works
            CreateDirectory(@"\\?\" + temp + "aux", IntPtr.Zero);

            //Removes the aux Folder
            //WARNING: The "aux" folder can not be removed with the windows explorer
            RemoveDirectory(@"\\?\" + temp + "aux");
        }

        [DllImport("kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool CreateDirectory(string lpPathName, IntPtr lpSecurityAttributes);

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        static extern bool RemoveDirectory(string lpPathName);
    }
}