C# Program To Demonstrate The Use Of Fullname Property

C# stands out as a robust and versatile programming language widely employed by developers for crafting diverse applications. A crucial aspect of mastering C# involves understanding the properties present in different classes and leveraging them to enhance program efficiency.

In this article, we will explore the FullName attribute in C# and showcase its functionality with syntax examples, code snippets, and results.

Understanding the FullName Property

  • In C#, the FullName property is frequently found alongside classes involved in file and directory manipulation. It is employed to obtain the complete path of a file or directory along with its name.
  • This property makes getting the complete location of a file or directory easy for developers who may need to work with elements from the file system.

Accessing the FullName property involves instantiating a class like FileInfo or DirectoryInfo, followed by utilizing dot notation.

Here's a generic representation of the syntax:

// For FileInfo

Example

FileInfo fileInfo = new FileInfo("path/to/file.txt");
string fileFullName = fileInfo.FullName;

// For DirectoryInfo

Example

DirectoryInfo directoryInfo = new DirectoryInfo("path/to/directory");
string directoryFullName = directoryInfo.FullName;
  • Instantiate the respective class (FileInfo or DirectoryInfo).
  • Supply the file or directory path as a parameter to the constructor.
  • Retrieve the FullName property using dot notation.
  • Illustrative Code with Explanation

An illustration will clarify the utilization of the FullName attribute. To demonstrate, we will develop a simple C# console program to exhibit how the FullName property can be used to fetch the complete file path.

Example

using System;
using System.IO;
 class Program
{
   static void Main ()
    {
        // Specify the file path
        string filePath = "C:\\SampleFiles\\example.txt";
 
        // Instantiate a FileInfo object
        FileInfo fileInfo = new FileInfo(filePath);
 
        // Retrieve the FullName property to obtain the full path
        string fileFullName = fileInfo.FullName;
 
        // Display the outcome
        Console.WriteLine($"Full path of the file: {fileFullName}");
    }
}

Output:

Output

Full path of the file: C:\SampleFiles\example.txt

Explanation:

The

  • Path Specification

The process begins with specifying a file path ("C:\SampleFiles\example.txt"), which indicates the precise location of a particular file within the system.

  • Creating a New Instance of the FileInfo Class

An object of the FileInfo class, fileInfo, is instantiated with the designated file path. This class plays a crucial role in furnishing comprehensive details regarding the specified file.

  • Accessing the FullName Property

The software accesses the FullName attribute of the FileInfo instance. This attribute provides the entire file path, including both the folder and file name, for the specified file.

  • Assigning Strings

The complete pathway is stored in a string variable named fileFullName.

  • Console Display

The software communicates the result by using Console.WriteLine to display the complete file path on the console.

  • Showing the Outcome

When the program is run, it will display the full file path ("C:\SampleFiles\example.txt") on the console.

Within this C# code snippet, the demonstration showcases how the FileInfo class and its FullName attribute are utilized to access and exhibit the entire file path. The process commences by defining a specific file path and then creating a new instance of the FileInfo class named fileInfo, associated with the given path. fileInfo serves as the means to extract in-depth details regarding the specified file.

The main focus of this process is to retrieve the FullName attribute from the FileInfo object, which provides the complete path containing both the directory and file name. Following this, the data is stored in a string variable called fileFullName. The script wraps up by displaying the obtained full path using the Console.WriteLine function.

During program execution, the console displays the complete file path, offering developers a concise method to access comprehensive file details. This brief yet effective code excerpt acts as a fundamental example of utilizing C#'s FileInfo class proficiently, demonstrating the language's capability to streamline interactions with file operations.

In essence, the provided C# code showcases an illustrative example of utilizing the FileInfo class and its FullName property, highlighting the efficiency of the language in simplifying file system tasks. Programmers can acquire valuable knowledge on retrieving file paths effectively, enhancing their understanding of C#'s functionality in handling file-related operations.

Input Required

This code uses input(). Please provide values below: