Suppose we have a string with full path with filename and we want to extract only the filename from the given path, then we can use Path.GetFileName method to extract only the file name.
Syntax: filename= Path.GetFileName(path);
Where path is parameter passed to the GetFileName method and filename is value returned by this method. If the last character of path is a directory or separator character, this method returns String.Empty. If path is null, this method returns null.
Eg: Below is the example of using GetFileName method in C#:
string strPath1 = @"C:\FolderName\filename.ext";
string strPath12= @"C:\FolderName\";
string strResultFileName;
strResultFileName = Path.GetFileName(strPath1);
Response.Write("Result is " + strResultFileName);
// Output is : Result is filename.ext
strResultFileName = Path.GetFileName(strPath2);
Response.Write(Result is " + strResultFileName);
// Output is : Result is
0 Comment(s)