Remove first directory of path (if one exists). e.g. "maps\\my maps\\he.map" becomes "my maps\\he.map" - CSharp File IO

CSharp examples for File IO:Directory

Description

Remove first directory of path (if one exists). e.g. "maps\\my maps\\he.map" becomes "my maps\\he.map"

Demo Code


using System.Text;
using System.Linq;
using System.IO;// w  ww .  j a v a  2  s  .co  m
using System.Collections.Generic;
using System.Collections;
using System;

public class Main{
        /// <summary>
        /// Remove first directory of path (if one exists).
        /// e.g. "maps\\mymaps\\hehe.map" becomes "mymaps\\hehe.map"
        /// Also used to cut first folder off, especially useful for relative
        /// paths. e.g. "maps\\test" becomes "test"
        /// </summary>
        static public string RemoveFirstDirectory(string path)
        {
            int i = path.IndexOf("\\");
            if (i >= 0 && i < path.Length)
                // Return rest of path
                return path.Substring(i + 1);
            // No first directory found, just return original path
            return path;
        } // RemoveFirstDirectory(path)
}

Related Tutorials