Creates a directory at the given path, if it's not existing - CSharp File IO

CSharp examples for File IO:Directory

Description

Creates a directory at the given path, if it's not existing

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from  ww w  . j  a  va  2  s .  c  o  m

public class Main{
        /// <summary>
        /// Creates a directory at the given path, if it's not existing
        /// </summary>
        /// <param name="Path">Path to the directory</param>
        /// <returns>True if the directory already exists</returns>
        public static bool CreateDirectoryIfNotExists(string Path)
        {
            bool returnValue = false;

            try
            {
                string directoryPath = System.IO.Path.GetDirectoryName(Path);

                if (System.IO.Directory.Exists(directoryPath))
                {
                    returnValue = true;
                }
                else
                {
                    System.IO.Directory.CreateDirectory(directoryPath);
                    returnValue = false;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Der Pfad konnte nicht ?berpr?ft werden oder der Ordner konnte nicht angelegt werden.", ex);
            }

            return returnValue;
        }
}

Related Tutorials