一種在 C# 中獲取檔名的方法


將路徑名設為字串 -

string myPath = "D:\
ew\quiz.txt";

現在,使用 GetFileName() 方法獲取檔案的檔名 -

Path.GetFileName(myPath)

以下是完成的程式碼 -

示例

 即時演示

using System;
using System.IO;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         string myPath = "D:\
ew\quiz.txt";          // get extension          Console.WriteLine("Extension: "+Path.GetExtension(myPath));          // get path          Console.WriteLine("File Path: "+Path.GetFileName(myPath));       }    } }

輸出

Extension: .txt
File Path: D:
ew\quiz.txt

更新於: 22-06-2020

577 次瀏覽

開啟你的 職業生涯

完成課程以獲得認證

立即開始
廣告