C# Program to get the name of the file from the absolute path


Introduction

Let us try to understand the C# program to get the name of the file from the absolute path. We will see the usage of the GetFileName method which comes under the File class to get the name of the file, and another method GetFileNameWithoutExtension which will return the file name of the specified path string without extension. To get the relative path from two absolute paths, we will make use of the Uri class in C#. The Uri class provides several methods for working with URIs, including MakeRelativeUri, which we will use in our code.

A file is a location in C# that is used to store some information. A collection of files is called a directory. A file in C# comes under the System.IO namespace which also considers various other classes which are used to perform operations on files. A path is another class that comes under the same namespace. There are two kinds of paths, Absolute path & relative path. The absolute path includes all information required to locate a file or directory on a system. An example of an absolute path is C:\Program Files\Google Chrome\filename.exe.

The relative path tells us the file's path with respect to the current directory on which the user is working. Considering the similar example mentioned above if the main executable is in C:\Program Files, the relative path to filename.exe is Google Chrome\filename.exe.

GetFileName Method

This is the method under file class in C# which is used to get the name of a file from an absolute path. The path.GetFileName is used to perform the operation. It takes an absolute path as input and returns the file name and the extension of the specified path. The absolute path which is being used as an input is in the form of a string.

Syntax

public static string GetFileName(string path)

here the path is the string from which we have to obtain the name of the file.

In the example above GetFileName is a method of the path class. This method will return the characters which are after the last directory character separator in the path name. If that is not present then it will return a NULL value. If the last character of the path is a directory or volume separator character then it is also going to return a NULL value.

Similarly, if the path is null, then it will return a NULL value.

If the file name is “C:\mydir\filename.exe” then the output is filename.exe.

Algorithm

The algorithm below will give a step-by-step procedure to get the name of the file by using File.GetFileName() method.

For eg if we have to find the name of the file then we can understand using the following algorithm −

Step 1  Create a string that will store the name of the file path, remember this is an absolute Path.

Step 2  Create a string filename that will store null initially.

Step 3  We will store the output of the GetFileName method in the string filename.

Step 4  As the name of the filename in the above code is “myfile” then the output will also be the same.

Step 5  If in any case, the path is null then the returned output will also be NULL.

Step 6  By using the GetFileName method, we can return the name of the file from the absolute path.

Example

using System;
using System.IO;
using System.Text;

class FileName {
   static void Main(string[] args) {
      //declaring the string path.
      string path = "E://Jinku Hu//Starting Over//Csharp//myfile.md";
      
      //declaring the string filename.
      string filename = null;
      
      //we will be storing the value in the filename variable.
      filename = Path.GetFileName(path);
      Console.WriteLine(filename);
      Console.ReadLine();
   }
}

Output

myfile.md

Time Complexity

In the code mentioned above we can observe that there are no loops or nested loops inside the main function, so time complexity is simply O(1).

GetFileNameWithoutExtension Method

The method explained previously was used to return the name of the file using the GetFileName method. Now we will be understanding how to return the name of the file without an extension. Suppose the name of the path is “C:\Program Files\Google Chrome\filename.exe”, then the returned output is “filename”.It takes the absolute path as input and returns the name of the file as output. The absolute path which is being used as an input is in the form of a string. Syntax- “public static string GetFileName(string path)”, here the path is the string from which we have to obtain the name of the file without the extension.

In the example above GetFileName is a method of the path class. This method will return the characters which are after the last directory character separator in the path name without the extension. If that is not present then it will return a NULL value. If the last character of the path is a directory or volume separator character then it is also going to return a NULL value.

Algorithm

In this algorithm we will understand the step-by-step method to return the name of the file without extension by using the GetFileNameWithoutExtension method.

Step 1  Create a string that will store the name of the absolute path.

Step 2  Create another string named filename which will be returned as output.

Step 3 − The string filename will store null as its initial value.

Step 4  The output generated using the GetFileNameWithoutExtension method will be stored in a string filename.

Step 5  If the file is null then the output will also be the same.

Step 6 − Thus we can return the name of the file without extension using the GetFileNameWithoutExtension method.

Example

using System;
using System.IO;
using System.Text;

class FileName {
   static void Main(string[] args) {
      //declare the string path first.
      string path = "C:\Program Files\Google Chrome\filename.exe";
      
      //declare the string filename and store null initially.
      string filename = null;
      filename = Path.GetFileNameWithoutExtension(path);
      
      //store the answer in the variable filename.
      Console.WriteLine(filename);
      Console.ReadLine();
   }
}

Output

filename.exe

Time Complexity

In the code mentioned above we can observe that there has been the usage of only a method that directly generates output and stores it in the respective variable. There has been no usage of any loop or any kind of complex recursive function. That simply makes up the time complexity of O(1).

Conclusion

In this article, we have extensively discussed the C# Program to get the name of the file from the absolute path with extension using the GetFileName method and also without extension using the GetFileNameWithoutExtension method.

We hope that this article has helped you enhance your knowledge regarding C#.

Updated on: 21-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements