C# `FullName` Property: Retrieving File and Directory Paths
Learn how to use the C# `FullName` property to efficiently obtain the complete path of files and directories using `FileInfo` and `DirectoryInfo` objects. This tutorial provides clear examples and explains its practical applications in file and directory manipulation.
Using the C# `FullName` Property
This article explains the C# `FullName` property, commonly used with file and directory objects (`FileInfo` and `DirectoryInfo`). It provides a convenient way to get the complete path of a file or directory.
Understanding the `FullName` Property
The `FullName` property returns the full path of a file or directory, including its name and all parent directory names. This is particularly useful when you need to work with files or directories on your system.
Syntax
To access the `FullName` property, you first create an instance of either the `FileInfo` or `DirectoryInfo` class and then access the property using dot notation:
// For FileInfo
FileInfo fileInfo = new FileInfo("path/to/file.txt");
string fullPath = fileInfo.FullName;
// For DirectoryInfo
DirectoryInfo dirInfo = new DirectoryInfo("path/to/directory");
string fullPath = dirInfo.FullName;
Example: Getting the Full Path of a File
This C# example demonstrates how to get the full path of a file:
using System;
using System.IO;
public class FullNameExample {
public static void Main(string[] args) {
string filePath = "C:\\SampleFiles\\example.txt";
FileInfo fileInfo = new FileInfo(filePath);
string fullPath = fileInfo.FullName;
Console.WriteLine($"Full path: {fullPath}");
}
}
Explanation
The example first specifies the file path. A `FileInfo` object is then created using this path. The `FullName` property is accessed from the `FileInfo` object and its value (the full path) is printed to the console.