This project has moved. For the latest updates, please go here.

Create a directory as Directory.CreateDirectory does.

Sep 19, 2012 at 6:07 AM
Edited Sep 19, 2012 at 6:08 AM

I wrote a method. I would like to share with others, because probably others would want this feature, too.Add this to LongPathDirectory.cs.

        /// <summary>
        ///     Creates the specified directory.
        /// </summary>
        /// <param name="path">
        ///     A <see cref="String"/> containing the path of the directory to create.
        /// </param>
        /// <remarks>
        ///     Note: Unlike <see cref="LongPathDirectory.Create(System.String)"/>, this method creates 
        ///     all missing parent directories in <paramref name="path"/>.
        /// </remarks>
        public static void CreateAll(string path)
        {
            int sep=0;
            string curDir;
            do
            {
                sep = path.IndexOf(Path.DirectorySeparatorChar, sep);
                if (sep == -1)
                {
                    curDir = path;
                }
                else
                {
                    sep++;
                    curDir = path.Substring(0, sep);
                }
                if (Exists(curDir) == false)
                {
                    Create(curDir);
                }
            } while (sep > -1);
        }
Sep 20, 2012 at 2:30 AM

Thank you for providing the free library but after spending a few hours with it, I found it is missing too many methods. After implementing some of the methods by myself, again and again I encountered with another barriers. I took a look at AlphaFS, and it seems it has almost everything I need. I will switch to that library. 

By the way, here is the method I have implemented. Might be useful to someone...

 ====== In the NativeMethods==============

        [DllImport("kernel32.dll",SetLastError = true,  CharSet = CharSet.Unicode)]
        public static extern int GetLongPathName(string path, StringBuilder longPath,
            int longPathLength);
===========================
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Experimental.IO.Interop;
using System.Text;

namespace Microsoft.Experimental.IO
{
    public static class LongPath
    {
        public static string GetDirectoryName(string longPath)
        {
            int sep = longPath.LastIndexOf(Path.DirectorySeparatorChar);
            if (sep == -1)
                sep = 0;
            return longPath.Substring(0, sep);
        }

        public static string GetPureDirectoryName(string longPath)
        {
            int firstSep = -1, secondSep = -1;

            secondSep = longPath.LastIndexOf(Path.DirectorySeparatorChar);
            if (secondSep > 0)
            {
                secondSep--;
                firstSep = longPath.LastIndexOf(Path.DirectorySeparatorChar, secondSep);
            }
            return longPath.Substring(firstSep + 1, secondSep - firstSep);
        }

        public static string GetFileName(string longPath)
        {
            int firstSep = -1;

            firstSep = longPath.LastIndexOf(Path.DirectorySeparatorChar);
            return longPath.Substring(firstSep+1);
        }

        public static string GetLongPath(string shortenedPath)
        {
            const int NTFSMaxPath = 32* 1024;

            StringBuilder longPath = new StringBuilder(NTFSMaxPath);
            NativeMethods.GetLongPathName(shortenedPath, longPath, longPath.Capacity);
            return longPath.ToString();
        }

        public static string GetFileNameWithoutExtension(string name)
        {
            int sep = -1, dot = -1;

            dot = name.LastIndexOf('.');
            if (dot > 0)
            {
                dot--;
                sep = name.LastIndexOf(Path.DirectorySeparatorChar, dot);
            }
            return name.Substring(sep + 1, dot - sep);
        }
    }
}