CK.Core.FileUtil.NormalizePathSeparator C# (CSharp) Method

NormalizePathSeparator() static public method

Canonicalizes the path: all '/' and '\' are mapped to Path.DirectorySeparatorChar (and Path.AltDirectorySeparatorChar will also be transformed).
static public NormalizePathSeparator ( string path, bool ensureTrailingBackslash ) : string
path string The path to standardize (must be not be null). It is trimmed and if the path is empty, the empty string is returned.
ensureTrailingBackslash bool /// Ensures that the normalized path will end with a . /// It should be true for path to directories because we consider that a directory path SHOULD end with /// the slash as often as possible. /// When is empty, this is not applied to preserve the fact that the string is empty. ///
return string
        static public string NormalizePathSeparator( string path, bool ensureTrailingBackslash )
        {
            if( path == null ) throw new ArgumentNullException( "path" );
            path = path.Trim();
            if( path.Length == 0 ) return path;
            if( Path.DirectorySeparatorChar != '/' && Path.AltDirectorySeparatorChar != '/' )
                path = path.Replace( '/', Path.DirectorySeparatorChar );
            if( Path.DirectorySeparatorChar != '\\' && Path.AltDirectorySeparatorChar != '\\' )
                path = path.Replace( '\\', Path.DirectorySeparatorChar );
            path = path.Replace( Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar );
            if( ensureTrailingBackslash && path[path.Length - 1] != Path.DirectorySeparatorChar )
            {
                path += Path.DirectorySeparatorChar;
            }
            return path;
        }

Usage Example

        static string NormalizeRootLogPath(string value)
        {
            if (!Path.IsPathRooted(value))
            {
#if NET451 || NET46
                value = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, value);
#else
                value = Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.ApplicationBasePath;
#endif
            }
            value = FileUtil.NormalizePathSeparator(value, true);
            return(value);
        }