新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

c#如何获取路径中的文件名,c# 获取文件夹路径

发布时间:2024-03-18 20:04:38

c#如何获得路径中的文件名

要获得路径中的文件名,可使用System.IO.Path类中的GetFileName方法。示例以下:

using System;

class Program
{
    static void Main()
    {
        string path = @"C:UsersUsernameDocumentsexample.txt";
        string fileName = System.IO.Path.GetFileName(path);
        
        Console.WriteLine(fileName); // 输出 example.txt
    }
}

上面的代码中,我们定义了一个路径字符串path,然后使用Path.GetFileName方法获得路径中的文件名,并将其存储在fileName变量中。最后,我们通过Console.WriteLine输出文件名。