Given a filesystem path, is there a shorter way to extract the filename without its extension?

后端 未结 11 976
面向向阳花
面向向阳花 2020-11-22 09:31

I program in WPF C#. I have e.g. the following path:

C:\\Program Files\\hello.txt

and I want to extract hello

11条回答
  •  一个人的身影
    2020-11-22 09:49

    string filepath = "C:\\Program Files\\example.txt";
    FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(filepath);
    FileInfo fi = new FileInfo(filepath);
    Console.WriteLine(fi.Name);
    
    //input to the "fi" is a full path to the file from "filepath"
    //This code will return the fileName from the given path
    
    //output
    //example.txt
    

提交回复
热议问题