Home > Article > Backend Development > C# program to get file name from absolute path
Let us try to understand C# program to get file name from absolute path. We will see the usage of GetFileName method under File class which is used to get the file name and another method GetFileNameWithoutExtension which will return the file name of the specified path string without extension. To get a relative path from two absolute paths, we will use the Uri class in C#. The Uri class provides several methods for working with URIs, including MakeRelativeUri, which we'll use in our code.
A file is a location used in C# to store some information. A collection of files is called a directory. In C#, files belong to the System.IO namespace, which also includes other classes for performing operations on files. Path is another class under the same namespace. There are two types of paths, absolute paths and relative paths. An absolute path includes all the information needed to locate a file or directory on your system. An example of an absolute path is C:\Program Files\Google Chrome\filename.exe.
The relative path tells us the path of the file relative to the current directory the user is working in. Consider the similar example mentioned above, if the main executable is located in C:\Program Files, the relative path to filename.exe is Google Chrome\filename.exe.
This is a method under the file class in C#, used to get the file name from the absolute path. path.GetFileName is used to perform this operation. It takes an absolute path as input and returns the filename and extension of the specified path. The absolute path used as input is in string form.
public static string GetFileName(string path)
The path here is the string we need to get the file name from.
In the above example, GetFileName is a method of the path class. This method returns the characters after the last directory character separator in the pathname. If it does not exist, then it will return a NULL value. It also returns a NULL value if the last character of the path is a directory or volume separator.
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 result is filename.exeThe algorithm below will give the step-by-step process of getting the file name using the File.GetFileName() method.
For example, if we need to find the name of a file, we can use the following algorithm to understand:
Step 1 − Create a string to store the name of the file path. Remember that this is an absolute path.
Step 2 − Create an initially empty string file name.
Step 3 −We will store the output of the GetFileName method in the string file name.
Step 4 − Since the file name in the above code is "myfile", the output will be the same.
Step 5 − If the path is null in any case, 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.
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(); } }
myfile.md
In the above mentioned code, we can observe that there are no loops or nested loops inside the main function, so the time complexity is simply O(1).
The method explained before is to use the GetFileName method to return the name of the file. Now we will see how to return the file name without extension. Assuming the name of the path is "C:\Program Files\Google Chrome\filename.exe", then the returned output is "filename". It accepts absolute paths as input and returns filenames as output. The absolute path entered is in the form of a string. Syntax - "public static string GetFileName(string path)", where path is the string we want to get the file name without extension.
In the above example, GetFileName is a method of the path class. This method will return the characters after the last directory character separator in the pathname, without extension. If it does not exist, then it will return a NULL value. It also returns a NULL value if the last character of the path is a directory or volume separator.
在此算法中,我们将了解使用 GetFileNameWithoutExtension 方法返回不带扩展名的文件名的分步方法。
第 1 步 − 创建一个字符串来存储绝对路径的名称。
步骤2 − 创建另一个名为filename的字符串,该字符串将作为输出返回。
第三步 − 字符串变量filename将以null作为其初始值。
第四步 − 使用GetFileNameWithoutExtension方法生成的输出将存储在一个字符串filename中。
步骤 5 − 如果文件为空,则输出也将相同。
第 6 步 − 因此我们可以使用 GetFileNameWithoutExtension 方法返回不带扩展名的文件名。
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(); } }
filename.exe
在上述代码中,我们可以观察到只使用了一种直接生成输出并将其存储在相应变量中的方法。没有使用任何循环或任何类型的复杂递归函数。这仅仅构成了时间复杂度为O(1)。
在本文中,我们广泛讨论了使用GetFileName方法从绝对路径中获取带有扩展名的文件名,以及使用GetFileNameWithoutExtension方法获取不带扩展名的文件名的C#程序。
我们希望本文能够帮助您增强有关 C# 的知识。
The above is the detailed content of C# program to get file name from absolute path. For more information, please follow other related articles on the PHP Chinese website!