Getting File name from the String

后端 未结 5 579
醉话见心
醉话见心 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:41

    Use the Path.GetFileName() Method

    (Edited) sample from the MSDN-page:

    string fileName = @"C:\xxxx\xxxx\xxxx\abc.pdf";
    string path = @"C:\xxxx\xxxx\xxxx\";
    string path2 = @"C:\xxxx\xxxx\xxxx";
    
    string result;
    
    result = Path.GetFileName(fileName);
    Console.WriteLine("GetFileName('{0}') returns '{1}'", 
        fileName, result);
    
    result = Path.GetFileName(path);
    Console.WriteLine("GetFileName('{0}') returns '{1}'", 
        path, result);
    
    result = Path.GetFileName(path2);
    Console.WriteLine("GetFileName('{0}') returns '{1}'", 
        path2, result);
    

    This code produces output similar to the following:

    GetFileName('C:\xxxx\xxxx\xxxx\abc.pdf') returns 'abc.pdf'
    GetFileName('C:\xxxx\xxxx\xxxx\') returns ''
    GetFileName('C:\xxxx\xxxx\xxxx') returns 'xxxx'
    

提交回复
热议问题