C # Get the path to "all user desktops"

Source: Internet
Author: User

Want to get the path to the all Users Desktop (public\desktop) in C #.

Originally thought very simple, however

Environment.getfolderpath (Environment.SpecialFolder.Desktop)

Only the current user's desktop path can be obtained. In the end, there is no way to call C + + functions only:

[DllImport ("Shfolder.dll", CharSet =CharSet.Auto)]Private Static extern intSHGetFolderPath (IntPtr hWndOwner,intNfolder, IntPtr Htoken,intdwFlags, StringBuilder lpszpath);Private Const intMAX_PATH =260;Private Const intCsidl_common_desktopdirectory =0x0019; Public Static stringGetallusersdesktopfolderpath () {StringBuilder Sbpath=NewStringBuilder (MAX_PATH); SHGetFolderPath (IntPtr.Zero, Csidl_common_desktopdirectory, IntPtr.Zero,0, Sbpath); returnsbpath.tostring ();}

Gets the directory of the DLL

Assembly myassembly = assembly.getentryassembly (); string Path =new  DirectoryInfo (path);p ath=dr. Parent;  // Top level directory of current directory

C # How to get the current path of the program

System.Environment.CurrentDirectory; // Example: C:\Test Application.executablepath; (including name) // Example: C:\test\myapp.exe Application.startuppath; (not including name) // Example: C:\Test

Gets the full path of the new process component and its main module associated with the currently active process, including the file name (process name).

string str = System.Diagnostics.Process.GetCurrentProcess (). Mainmodule.filename;result:x:\xxx\xxx\xxx.exe (. exe file in the same directory as +.exe filename)

Gets and sets the fully qualified path to the current directory (that is, the directory from which the process starts).

string str = system.environment.currentdirectory;result:x:\xxx\xxx (the directory where the. exe file resides)

Gets the base directory of the current application domain for Thread, which is used by the Assembly resolver to probe assemblies.

string str = system.appdomain.currentdomain.basedirectory;result:x:\xxx\xxx\ (the directory where the. exe file is located +" \")

Gets and sets the name of the directory that contains the application.

string str = system.appdomain.currentdomain.setupinformation.applicationbase;result:x:\xxx\xxx\ (. EXE file in the same directory +"\")

Gets the path to the executable file that launched the application, not including the name of the executable file.

string str = system.windows.forms.application.startuppath;result:x:\xxx\xxx (the directory where the. exe file resides)

Gets the path to the executable file that launched the application, including the name of the executable file.

string str = System.windows.forms.application.executablepath;result:x:\xxx\xxx\xxx.exe (the directory where the. exe file resides +.exe file name)

Gets the current working directory of the application (unreliable).

string str = System.IO.Directory.GetCurrentDirectory (); result:x:\xxx\xxx (the directory where the. exe file is located) gets the System special folder path (Favorites , desktop)

1. Favorite Folder path

System.Environment.GetFolderPath (System.Environment.SpecialFolder.Favorites)

2. Desktop Path

System.Environment.GetFolderPath (System.Environment.SpecialFolder.Desktop)

See Enum class System.Environment.SpecialFolder for more information

Static voidMain (string[] args) {  //Environment.getfolderpath (Environment.SpecialFolder); //This method retrieves the path to the System special folder (such as program Files, Programs, System, or Startup).//can be used to access public information. Special folders are set by default by the system, or are explicitly set by the user when installing a version of Windows. //The folder parameter specifies the special folders to retrieve, and the parameter must be a value in the Environment.SpecialFolder enumeration, and any other value will throw an exception. //Enumeration of Folder//The ApplicationData directory, which serves as the common repository for application-specific data for the current roaming user. //The Commonapplicationdata directory, which serves as a common repository for application-specific data that is used by all users. //The Localapplicationdata directory, which serves as a common repository for application-specific data that is currently used by non-roaming users. //cookies are used as directories for public repositories of Internet cookies. //Desktop logical desktops, not physical file system locations. //Favorites A directory that serves as a common repository for user favorite items. //History The directory that is used as the public repository for Internet Chronicle items. //Internetcache is used as a directory for public repositories of temporary Internet files. //Programs contains a directory of user program groups. //MyComputer the "My Computer" folder. //MyMusic the "My Music" folder. //mypictures the "My Pictures" folder. //recent The directory that contains the documents that the user has recently used. //SendTo The directory containing the Send menu item. //StartMenu The directory containing the Start menu item. //startup directory that corresponds to the user's startup program group. //System "System" directory. //Templates A directory that is used as a common repository for document templates. //The desktopdirectory is used to physically store the directory of File objects on the desktop. //Personal The directory that is used as a common repository for documents. //MyDocuments the "My Computer" folder. //ProgramFiles The "Program Files" directory. //commonprogramfiles The directory for components that are shared among applications. Console.WriteLine ("Public repository: {0}", Environment.getfolderpath (Environment.SpecialFolder.ApplicationData)); Console.WriteLine ("directory: {0}", Environment.getfolderpath (Environment.SpecialFolder.CommonApplicationData)); Console.WriteLine ("directory: {0}", Environment.getfolderpath (Environment.SpecialFolder.LocalApplicationData)); Console.WriteLine ("Cookie path: {0}", Environment.getfolderpath (Environment.SpecialFolder.Cookies)); Console.WriteLine ("Logical Desktop: {0}", Environment.getfolderpath (Environment.SpecialFolder.Desktop)); Console.WriteLine ("favorite path: {0}", Environment.getfolderpath (Environment.SpecialFolder.Favorites)); Console.WriteLine ("Web browsing History path: {0}", Environment.getfolderpath (Environment.SpecialFolder.History)); Console.WriteLine ("path to Temporary Internet files: {0}", Environment.getfolderpath (Environment.SpecialFolder.InternetCache)); Console.WriteLine ("application directory: {0}", Environment.getfolderpath (Environment.SpecialFolder.Programs)); Console.WriteLine ("My Computer directory: {0}", Environment.getfolderpath (Environment.SpecialFolder.MyComputer)); Console.WriteLine ("My Music directory: {0}", Environment.getfolderpath (Environment.SpecialFolder.MyMusic)); Console.WriteLine ("Picture catalog: {0}", Environment.getfolderpath (Environment.SpecialFolder.MyPictures)); Console.WriteLine ("recently used document directory: {0}", Environment.getfolderpath (Environment.SpecialFolder.Recent)); Console.WriteLine ("recently sent files directory: {0}", Environment.getfolderpath (Environment.SpecialFolder.SendTo)); Console.WriteLine ("Directory Starting in Start menu: {0}", Environment.getfolderpath (Environment.SpecialFolder.StartMenu)); Console.WriteLine ("System directory: {0}", Environment.getfolderpath (Environment.SpecialFolder.System)); Console.WriteLine ("directory used as a common repository for document templates: {0}", Environment.getfolderpath (Environment.SpecialFolder.Templates)); Console.WriteLine ("directory used to physically store file objects on the desktop: {0}", Environment.getfolderpath (Environment.SpecialFolder.DesktopDirectory)); Console.WriteLine ("directory used as a common repository for documents: {0}", Environment.getfolderpath (Environment.SpecialFolder.Personal)); Console.WriteLine ("My Computer folder: {0}", Environment.getfolderpath (Environment.SpecialFolder.MyDocuments)); Console.WriteLine ("program Files directory. : {0}", Environment.getfolderpath (Environment.SpecialFolder.ProgramFiles)); Console.WriteLine ("directory for components that are shared among applications: {0}", Environment.getfolderpath (Environment.SpecialFolder.CommonApplicationData));        Console.ReadLine (); }

C # Get the path to "all user desktops"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.