How to read text file from Desktop location in C#

Updated on: March 19, 2023

To provide Desktop location path in C#, we have 2 ways, either hard code entire path like "C:\Users\logged_in_username\Desktop" or by using Environment.SpecialFolder.Desktop as shown below:

We have created a file on Desktop called "test.txt", now in C# if we want to read text.txt from Desktop, we can read it simply using "Environment.SpecialFolder.Desktop"

Following is the C# console app which we have created and added following content in Program.cs to read Desktop location path and read file content and display it as output:

using System;
using System.IO;

namespace EnvSpecialFolderSolution
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string desktpPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string testFilePath = Path.Combine(desktpPath, "test.txt");
            string contents = File.ReadAllText(testFilePath);
            Console.WriteLine(contents);
            Console.ReadLine();
        }
    }

}

Output of above program is:

Here, in this example, we have written System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) which takes Desktop location path and we can use this path to read/write any file content.