Path Class in C#
In this post, you will learn how to work with the Path class so that you can use that same knowledge and thus evolve.
- this class (belonging to the System.IO library) provides various static methods to obtain information about a certain file or directory.
GetFileName() |
Returns the filename and its extension |
GetFileNameWithoutExtension() |
Returns the filename without the extension |
GetExtension() |
Returns only the file extension |
GetFullPath() |
Returns the entire specified path (absolute path) |
- when you use const when creating an attribute, you are declaring a constant field which after this declaration cannot be modified.
using System;
using System.IO;
namespace Base {
class PathClass {
private const string File = "File.txt";
public void Run() {
Console.WriteLine($"File name with extension: {Path.GetFileName(File)}");
Console.WriteLine($"File name without extension: {Path.GetFileNameWithoutExtension(File)}");
Console.WriteLine($"File extension: {Path.GetExtension(File)}");
Console.WriteLine($"Absolute path to file: {Path.GetFullPath(File)}");
}
}
}
/*
File name with extension: File.txt
File name without extension: File
File extension: .txt
Absolute path to file: ...\File.txt
*/
Don’t forget to watch the video and you can always read this post in Portuguese.