Get the full path, directory, extension, and file name of the file from the C# path

class Program
    {
        static void Main(string[] args)
        {

            //Get the directory of the currently running program
            string fileDir = Environment.CurrentDirectory;
            Console.WriteLine("Current program directory: "+fileDir);
            
            // a file directory
            string filePath = "C:\\JiYF\\BenXH\\BenXHCMS.xml";
            Console.WriteLine("The file's directory: "+filePath);

            string str = "Get the full path of the file:" + Path.GetFullPath(filePath); //-->C:\JiYF\BenXH\BenXHCMS.xml
            Console.WriteLine(str);
            str = "Get the directory where the file is located:" + Path.GetDirectoryName(filePath); //-->C:\JiYF\BenXH
            Console.WriteLine(str);
            str = "The name of the obtained file contains a suffix:" + Path.GetFileName(filePath); //-->BenXHCMS.xml
            Console.WriteLine(str);
            str = "Get the file name without a suffix:" + Path.GetFileNameWithoutExtension(filePath); //-->BenXHCMS
            Console.WriteLine(str);
            str = "Get the suffix extension name of the path:" + Path.GetExtension(filePath); //-->.xml
            Console.WriteLine(str);
            str = "Get the root directory of the path:" + Path.GetPathRoot(filePath); //-->C:\
            Console.WriteLine(str);
            Console.ReadKey();

        }
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324748284&siteId=291194637