How to get a path to the desktop for current user in C#?

CsharpServer Side ProgrammingProgramming

The desktop path of the current user can be fetched by using Environment.SpecialFolder. The Environment.SpecialFolder gets the path to the system special folder that is identified by the specified enumeration.

string desktopPath =

Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

The System.Environment Class provides information about the current environment and platform. The System.Environment Class uses to retrieve Environment variable settings, Version of the common language runtime, contents of the call stack etc. This class cannot be inherited.

Environment class is static class which Provides the system configuration,Current program execution Environment as wel some properties for string manipulation such as news line,System Namespace represents the Environment Class.

Environment class is a combination of functions and Properties which represent the Environment variable details using IDictionary in the form of key,value pairs.

We can also loop through Dictionary to get all Environment variable details, before introducing all Environment variable, let us go through some frequently used Environment variable details. As Environment class is the static class because of this we directly access the methods and properties with help of class name itself without creating object.

Example

 Live Demo

using System;
namespace DemoApplication{
   public class Program{
      public static void Main(){
         string desktopPath =
         Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
         Console.WriteLine($"Desktop Path: {desktopPath}");
         Console.ReadLine();
      }
   }
}

Output

The output of the above code is

Desktop Path: C:\Users\UserName\Desktop
raja
Published on 19-Aug-2020 11:25:35
Advertisements