Getting File name from the String

后端 未结 5 570
醉话见心
醉话见心 2021-01-20 11:29

Could you help me for finding the file name from the string. Now i have one string of content like \"C:\\xxxx\\xxxx\\xxxx\\abc.pdf\". But i want only the file name ie. abc.p

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-20 11:40

    Sytem.IO.FileInfo is also quite cool: In your case you can do

    FileInfo fi = new FileInfo("C:\xxxx\xxxx\xxxx\abc.pdf");
    string name = fi.Name; // it gives you abc.pdf
    

    Then you can have several other pieces of information:
    does the file really exist? fi.Exists gives you the answer
    what's its extension? see fi.Extension
    what's the name of its directory? see fi.Directory
    etc.

    Have a look at all the members of FileInfo you may find something interesting for your needs

提交回复
热议问题