예제 #1
0
        public static string ToUncPath(string path)
        {
            if (IsDevice(path))
            {
                return(LongPathExtension.GetFullPath(path));
            }

            if (IsPartiallyQualified(path))
            {
                path = LongPathExtension.GetFullPath(path);
                if (IsDevice(path))
                {
                    return(path);
                }
                else
                {
                    return(ExtendedPathPrefix + path);
                }
            }

            //// Given \\server\share in longpath becomes \\?\UNC\server\share
            if (path.StartsWith(UncPathPrefix, StringComparison.OrdinalIgnoreCase))
            {
                return(LongPathExtension.GetFullPath(path.Insert(2, UncExtendedPrefixToInsert)));
            }

            return(LongPathExtension.GetFullPath(ExtendedPathPrefix + path));
        }
예제 #2
0
        public static void Delete(string path)
        {
#if DOTNET5_4
            File.Delete(path);
#else
            path = LongPathExtension.ToUncPath(path);
            if (!NativeMethods.DeleteFileW(path))
            {
                NativeMethods.ThrowExceptionForLastWin32ErrorIfExists();
            }
#endif
        }
예제 #3
0
        public static void Delete(string path, bool recursive = false)
        {
#if DOTNET5_4
            Directory.Delete(path, recursive);
#else
            path = LongPathExtension.ToUncPath(path);
            if (recursive == true)
            {
                string[] dirs  = GetDirectories(path, SearchOption.AllDirectories);
                string[] files = GetFiles(path, SearchOption.AllDirectories);

                Array.Sort(dirs, (string strA, string strB) => {
                    if (strA.Length > strB.Length)
                    {
                        return(-1);
                    }
                    if (strA.Length < strB.Length)
                    {
                        return(1);
                    }
                    return(string.Compare(strB, strA));
                });

                Array.Sort(files, (string strA, string strB) => {
                    if (strA.Length > strB.Length)
                    {
                        return(-1);
                    }
                    if (strA.Length < strB.Length)
                    {
                        return(1);
                    }
                    return(string.Compare(strB, strA));
                });

                foreach (var subPath in files)
                {
                    LongPathFileExtension.Delete(subPath);
                }

                foreach (var subDir in dirs)
                {
                    LongPathDirectoryExtension.Delete(subDir);
                }
            }
            LongPathDirectoryExtension.Delete(path);
#endif
        }