Returns the "My Documents" path string
Environment. getfolderpath (environment. specialfolder. Personal)
This technique uses the getfolderpath method to obtain the path pointing to the system special folder identified by the specified enumeration. The syntax format is as follows:
Public static string getfolderpath (specialfolder folder)
The folder parameter identifies the enumerated constant of the system special folder.
If the special folder of the specified system exists on the user's computer, the path to the folder is returned; otherwise, it is a null string (""). If the system does not create a folder, the existing folder has been deleted, or the folder is a virtual directory (such as "My Computer") that does not correspond to a physical path, the folder does not actually exist.
MainCodeAs follows:
MessageBox. Show ( " My document system path: " + Environment. getfolderpath (environment. specialfolder. Personal ), " My Documents " , Messageboxbuttons. OK, messageboxicon. information );
Reference 1: C # How to obtain the "My Documents" Directory of a user
Console. writeline (system. environment. getfolderpath (environment. specialfolder. mydocuments ));
System. environment. getfolderpath Method
Gets the path to the system special folder identified by the specified enumeration.
Public static string getfolderpath (
Environment. specialfolder folder
)
Environment. specialfolder enumeration description:
Member Name Description
Applicationdata Directory, which is used as the application of the current roaming user Program Public repository for specific data.
The commonapplicationdata Directory, which is used as a public repository of application-specific data for all users.
Localapplicationdata Directory, which is used as a public repository of application-specific data for non-roaming users.
The directory where cookies are used as the public repository of Internet cookies.
Desktop logical desktop, rather than the location of the physical file system.
Favorites is used as the directory of the public repository for user favorites items.
History is used as the directory of the public repository for Internet history items.
The directory where internetcache is used as a public repository for Internet temporary files.
Programs contains the directory of the user program group.
Mycomputer "my computer" folder.
Mymusic "My music" folder.
Mypictures "my pictures" folder.
Recent contains the directory of the most recently used documents.
Sendto directory that contains the "send" menu item.
Startmenu contains the directory of the "Start" menu item.
Startup corresponds to the directory of the user's "start" program group.
System "system" directory.
Templates Composition Directory of the public repository of the file template.
Topdirectory is used to physically store the directory of file objects on the desktop.
The directory where personal is used as the public repository of the document.
Mydocuments "my computer" folder.
ProgramFiles "Program Files" directory.
Commonprogramfiles is the directory of components shared between applications.
Reference 2: C # open a desktop or other special system path
Different operating systems have different desktop paths and different installation locations.
C # You can read the location of your special folders (desktops, favorites, etc.) from the Windows registry.
The Code is as follows:
Using Microsoft. Win32;
Namespace jpgcompact
{
Public partial class mainform: Form
{
Private void test ()
{
Registrykey folders;
Folders = openregistrypath (registry. currentuser, @ "\ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ Shell Folders ");
// Windows user desktop path
String export toppath = folders. getvalue ("desktop"). tostring ();
// Windows user font directory path
String fontspath = folders. getvalue ("fonts"). tostring ();
// Windows user's network neighbor path
String nethoodpath = folders. getvalue ("nethood"). tostring ();
// Path of my documents for Windows users
String personalpath = folders. getvalue ("personal"). tostring ();
// Windows user Start Menu program path
String programspath = folders. getvalue ("programs"). tostring ();
// The directory path for Windows users to store the shortcuts for recently accessed documents
String recentpath = folders. getvalue ("Recent"). tostring ();
// Directory path for Windows users
String sendtopath = folders. getvalue ("sendto"). tostring ();
// Windows user Start Menu directory path
String startmenupath = folders. getvalue ("startmenu"). tostring ();
// Directory path of the Windows user Start Menu startup Item
String startuppath = folders. getvalue ("Startup"). tostring ();
// Windows user favorites directory path
String favoritespath = folders. getvalue ("favorites"). tostring ();
// Path of the Windows user's web page's historical directory
String historypath = folders. getvalue ("History"). tostring ();
// Windows user cookies directory path
String cookiespath = folders. getvalue ("cookies"). tostring ();
// Windows user cache directory path
String cachepath = folders. getvalue ("cache"). tostring ();
// Path to the data directory of the Windows user application
String appdatapath = folders. getvalue ("appdata"). tostring ();
// Windows user print directory path
String printhoodpath = folders. getvalue ("printhood"). tostring ();
}
Private registrykey openregistrypath (registrykey root, string S)
{
S = S. Remove (0, 1) + @"\";
While (S. indexof (@"\")! =-1)
{
Root = root. opensubkey (S. substring (0, S. indexof (@"\")));
S = S. Remove (0, S. indexof (@ "\") + 1 );
}
Return root;
}
}
C # reads system environment variables, my document paths, desktop paths, etc.
1
Directly system. environment. getenvironmentvariable ["variable name"];
For example, you can obtain the computer name and program folder.
[Sourcecode Language = 'C # ']
Textbox1.text = system. environment. getenvironmentvariable ("computername") + "rn ";
Textbox1.text = system. environment. getenvironmentvariable ("ProgramFiles") + "rn ";
[/Sourcecode]
As for "desktop" and "my documents ",
[Code language = 'C # ']
Textbox1.text + = environment. getfolderpath (environment. specialfolder. Personal) + "RN"; // my documents
Textbox1.text + = environment. getfolderpath (environment. specialfolder. desktop) + "RN"; // Desktop
[/Code]
Environment. getfolderpath (environment. specialfolder. Special folder)
Paths such as cookies, music, videos, and sending can be obtained in this way.
2
C # Read system environment variables
using system; using system. collections; class foreachapp { Public static void main () { // obtain all values in the environment variable, put it in the variable environment idictionary environment = environment. getenvironmentvariables (); // print the header console. writeline ("environment variable name \ t = \ t environment variable value"); // Traverse all key values in Environment Foreach (string environmentkey in environment. Keys) { // Print the names and values of all environment variables Console. writeline ("{0} \ t = \ t {1}", environmentkey, Environment [environmentkey]. tostring ()); } } } |
3
C # Read and set the PATH environment variable and restart the computer
using system;
using system. collections. generic;
using system. componentmodel;
using system. data;
using system. drawing;
using system. text;
using system. windows. forms;
using Microsoft. win32; // the space to be referenced by registry operations
using system. runtime. interopservices; // reference required to call an API function to load an unmanaged user32.dll
use a program to modify the environment variables in namespace
{
Public partial class form1: form
{
Public form1 ()
{
initializecomponent ();
}
Private void form1_load (Object sender, eventargs E)
{
}
/// <Summary>
/// Read the Registry
/// Path: [HKEY_LOCAL_MACHINE \ SYSTEM \ controlset001 \ Control \ Session Manager \ environment]
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void tbnread_click (Object sender, eventargs E)
{
Registrykey reglocalmachine = registry. localmachine;
Registrykey regsystem = reglocalmachine. opensubkey ("system", true); // open the system under HKEY_LOCAL_MACHINE
Registrykey regcontrolset001 = regsystem. opensubkey ("controlset001", true); // open controlset001
Registrykey regcontrol = regcontrolset001.opensubkey ("control", true); // Open Control
Registrykey regmanager = regcontrol. opensubkey ("Session Manager", true); // Open Control
Registrykey regenvironment = regmanager. opensubkey ("environment", true); // open MSSQLServer under MSSQLServer
This. richtextbox1.text = regenvironment. getvalue ("path"). tostring (); // read the value of Path
}
Private void btnclose_click (Object sender, eventargs E)
{
Close ();
}
/// <Summary>
///
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
[Dllimport ("user32.dll", setlasterror = true, charset = charset. Auto)] // sendmessagetimeout is defined in user32.dll
Public static extern intptr sendmessagetimeout (
Intptr when whandle,
Uint MSG,
Intptr wparam,
Intptr lparam,
Sendmessagetimeoutflags flags,
Uint timeout,
Out intptr result
);
[Flags]
Public Enum sendmessagetimeoutflags: uint
{
Smto_normal = 0x0000,
Smto_block = 0x0001,
Smto_abortifhung = 0x0002,
Smto_notimeoutifnothung = 0x0008
}
Private void btnwrite_click (Object sender, eventargs E)
{
Registrykey reglocalmachine = registry. localmachine;
Registrykey regsystem = reglocalmachine. opensubkey ("system", true); // open the system under HKEY_LOCAL_MACHINE
Registrykey regcontrolset001 = regsystem. opensubkey ("controlset001", true); // open controlset001
Registrykey regcontrol = regcontrolset001.opensubkey ("control", true); // Open Control
Registrykey regmanager = regcontrol. opensubkey ("Session Manager", true); // Open Control
Registrykey regenvironment = regmanager. opensubkey ("environment", true); // open MSSQLServer under MSSQLServer
Regenvironment. setvalue ("path", this. richtextbox1.text); // read the value of Path
MessageBox. Show ("modified successfully ");
// Do not restart the computer after sending system messages.
Const int hwnd_broadcast = 0 xFFFF;
// DWORD dwmsgresult = 0l;
Const uint wm_settingchange = 0;
Const long smto_abortifhung = 0x2;
System. uint32 dwmsgresult1 = 0;
Long S;
// Sendmessagetimeout (hwnd_broadcast, wm_settingchange, 0, (string) "environment", smto_abortifhung, 5000, (long) S );
}
/// <Summary>
/// Restart the computer
/// </Summary>
/// <Param name = "X"> </param>
/// <Param name = "Y"> </param>
/// <Returns> </returns>
[Dllimport ("user32.dll")]
// This is the main API. Note: It must be declared as static extern.
Private Static extern int exitwindowsex (int x, int y );
Private void button#click (Object sender, eventargs E)
{
Exitwindowsex (2, 0 );
}
}
}