Mohanapriya R Mohanapriya R
Updated date Oct 31, 2024
In this article, we will learn how to convert directories to strings in C#.

Directory to Strings Conversion in C#

In C#, the System.IO namespace provides classes that enable us to work with files and directories. The Directory class, in particular, offers methods to manipulate directories. To convert a directory to a string, we will be using the ToString method, which is inherently available in C#.

using System;
using System.IO;

class Program
{
    static void Main()
    {
        // Specify the directory path
        string directoryPath = @"C:\TestDirectory";

        // Convert the directory to a string
        string directoryString = directoryPath.ToString();

        // Display the result
        Console.WriteLine("Directory converted to string: " + directoryString);
    }
}

In this program, we start by specifying a directory path (replace C:\TestDirectory with the actual path you want to use). The ToString method is then applied to convert the directory path to a string, and the result is displayed using Console.WriteLine.

Output:

Directory converted to string: C:\TestDirectory

Comments (0)

There are no comments. Be the first to comment!!!