Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get the filename only from the given path string in C#

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 933
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: