一种在 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 次浏览

开启你的 职业生涯

完成课程以获得认证

立即开始
广告
© . All rights reserved.