How to ensure there is trailing directory separator in paths?

You can easily ensure the behaviour you desire by using TrimEnd:

var baseDir = AppDomain.CurrentDomain.BaseDirectory
                  .TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;

To be optimally efficient (by avoiding extra allocations), check that the string doesn’t end with a \ before making changes, since you won’t always need to:

const string sepChar = Path.DirectorySeparatorChar.ToString();
const string altChar = Path.AltDirectorySeparatorChar.ToString();

var baseDir = AppDomain.CurrentDomain.BaseDirectory;
if (!baseDir.EndsWith(sepChar) && !baseDir.EndsWith(altChar))
{
    baseDir += sepChar;
}

Leave a Comment

error code: 521