溫馨提示×

c#獲取文件名的方法是什么

c#
小億
133
2024-02-21 17:14:27
欄目: 編程語言

在C#中獲取文件名的方法通常使用System.IO命名空間中的Path類。具體來說,可以使用Path類中的GetFileName方法來獲取文件的文件名。示例如下:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = @"C:\Users\user\Desktop\example.txt";

        string fileName = Path.GetFileName(filePath);

        Console.WriteLine("File Name: " + fileName);
    }
}

上面的示例代碼中,首先定義了一個文件路徑filePath,然后使用Path.GetFileName方法獲取文件名,最后輸出文件名到控制臺。

0