C# 程序在 C# 中获取文件名

csharpprogrammingserver side programming更新于 2025/9/16 5:24:00

在字符串中设置路径名 −

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";          // 获取扩展名          Console.WriteLine("Extension: "+Path.GetExtension(myPath));          // 获取路径          Console.WriteLine("File Path: "+Path.GetFileName(myPath));       }    } }

输出

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

相关文章