Azavea.NijPredictivePolicing.Common.FileUtilities.PathEnsure C# (CSharp) Method

PathEnsure() public static method

Given a basepath and a list of names of subdirectories, creates the subdirectories if necessary. Throws exceptions on error.
public static PathEnsure ( string basepath ) : string
basepath string A path to a directory that already exists
return string
        public static string PathEnsure(string basepath, params string[] chunks)
        {
            if (string.IsNullOrEmpty(basepath))
                return basepath;

            if ((chunks != null) && (chunks.Length > 0))
            {
                for (int i = 0; i < chunks.Length; i++)
                {
                    basepath = Path.Combine(basepath, chunks[i]);
                }
            }

            //CreateDirectory creates all necessary directories
            if (!Directory.Exists(basepath))
                Directory.CreateDirectory(basepath);

            return basepath;
        }

Usage Example

        /// <summary>
        /// Wrapper around PathEnsure() that doesn't throw exceptions.  Returns "" on error and prints an error message to _log
        /// </summary>
        /// <param name="basepath"></param>
        /// <param name="chunks"></param>
        /// <returns></returns>
        public static string SafePathEnsure(string basepath, params string[] chunks)
        {
            try
            {
                //I'd rather do this here, than directly in the constructor or class definition.
                //The file system loves to throw exceptions, and I'd rather see em than a app 'exit'!
                //In Soviet Russia, computer throws things at YOU!!

                return(FileUtilities.PathEnsure(basepath, chunks));
            }
            catch (Exception ex)
            {
                _log.Error("Error constructing path", ex);
            }
            return(string.Empty);
        }