w3resource

C#: File name (including extension) from a given path


Get File Name from Path

Write a C# Sharp program to get the file name (including extension) from a given path.

Sample Solution:

C# Sharp Code:

using System;
using System.Linq;

namespace exercises
{
    class Program
    {
        // Main method where the program execution begins
        static void Main(string[] args)
        {
            string file_path;

            // Initialize file_path variable with a file path and call the test function
            file_path = "c:/csharp/ex/test.cpp";
            Console.WriteLine(test(file_path));  // Output: test.cpp

            // Change file_path and call the test function again
            file_path = "c:/movies/abc.mp4";
            Console.WriteLine(test(file_path));  // Output: abc.mp4

            // Change file_path to a different path and call the test function
            file_path = "test.txt";
            Console.WriteLine(test(file_path));  // Output: test.txt
        }

        // Function to extract the file name from a file path
        public static string test(string file_path)
        {
            // Split the file path using '/' as the separator and extract the last part (file name)
            return file_path.Split('/').Last();
        }
    }
}

Sample Output:

test.cpp
abc.mp4
test.txt

Flowchart:

Flowchart: C# Sharp Exercises - File name (including extension) from a given path.

Sample Solution-1:

C# Sharp Code:

using System;
using System.IO;  // Importing the System.IO namespace for file path operations

namespace exercises
{
    class Program
    {
        // Main method where the program execution begins
        static void Main(string[] args)
        {
            string file_path;

            // Initialize file_path variable with a file path and call the test function
            file_path = "c:/csharp/ex/test.cpp";
            Console.WriteLine(test(file_path));  // Output: test.cpp

            // Change file_path and call the test function again
            file_path = "c:/movies/abc.mp4";
            Console.WriteLine(test(file_path));  // Output: abc.mp4

            // Change file_path to a different path and call the test function
            file_path = "test.txt";
            Console.WriteLine(test(file_path));  // Output: test.txt
        }

        // Function to extract the file name from a file path using Path.GetFileName method
        public static string test(string file_path)
        {
            // Using Path.GetFileName method to extract the file name from the file path
            return Path.GetFileName(file_path);
        }
    }
}

Sample Output:

test.cpp
abc.mp4
test.txt

Flowchart:

Flowchart: C# Sharp Exercises - File name (including extension) from a given path.

For more Practice: Solve these Related Problems:

  • Write a C# program to extract only the file extension from a given file path.
  • Write a C# program to extract the directory structure from a file path, excluding the file itself.
  • Write a C# program to validate whether a path string contains a valid file format (e.g., .pdf, .txt).
  • Write a C# program to extract the filename and remove its extension.

Go to:


PREV : Check Number in Array.
NEXT : Multiply Array Elements by Length.

C# Sharp Code Editor:



What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.