Jak Mogę uzyskać bieżący katalog użytkownika?

Używając tego:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

I get this output:

"C:\\Documents and Settings\\[USER]\\Application Data"

Jak mogę uzyskać katalog główny wszystkich użytkowników? tj.:

"C:\\Documents and Settings\\[USER]\\"
Author: bytecode77, 2009-07-17

9 answers

Być może będzie to dobre rozwiązanie: biorąc pod uwagę czy jest to Vista / Win7 czy XP i bez użycia zmiennych środowiskowych:

string path = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName;
if ( Environment.OSVersion.Version.Major >= 6 ) {
    path = Directory.GetParent(path).ToString();
}

Chociaż użycie zmiennej środowiskowej jest o wiele bardziej zrozumiałe.

 41
Author: Anton Kolesov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-20 22:19:38

Try:

System.Environment.GetEnvironmentVariable("USERPROFILE");

Edit:

Jeśli używana wersja. Net jest 4 lub wyższa, możesz użyć wyliczenia Environment.SpecialFolder:

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
 132
Author: Thomas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-18 12:15:43

Możesz uzyskać ścieżkę UserProfile za pomocą tego:

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

 16
Author: zionyx,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-06 11:07:43

Również bardzo pomocne, podczas badania Environment.SpecialFolder enum. Użyj LINQPad lub utwórz rozwiązanie i wykonaj ten kod:

Enum.GetValues(typeof(Environment.SpecialFolder))
    .Cast<Environment.SpecialFolder>()
    .Select(specialFolder => new
    {
        Name = specialFolder.ToString(),
        Path = Environment.GetFolderPath(specialFolder)
    })
    .OrderBy(item => item.Path.ToLower())

Ścieżki Folderów

Oto wynik na mojej maszynie:

MyComputer
LocalizedResources
CommonOemLinks
ProgramFiles            C:\Program Files (x86) 
ProgramFilesX86         C:\Program Files (x86) 
CommonProgramFiles      C:\Program Files (x86)\Common Files 
CommonProgramFilesX86   C:\Program Files (x86)\Common Files 
CommonApplicationData   C:\ProgramData 
CommonStartMenu         C:\ProgramData\Microsoft\Windows\Start Menu 
CommonPrograms          C:\ProgramData\Microsoft\Windows\Start Menu\Programs 
CommonAdminTools        C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools 
CommonStartup           C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup 
CommonTemplates         C:\ProgramData\Microsoft\Windows\Templates 
UserProfile             C:\Users\fisch 
LocalApplicationData    C:\Users\fisch\AppData\Local 
CDBurning               C:\Users\fisch\AppData\Local\Microsoft\Windows\Burn\Burn 
History                 C:\Users\fisch\AppData\Local\Microsoft\Windows\History 
InternetCache           C:\Users\fisch\AppData\Local\Microsoft\Windows\INetCache 
Cookies                 C:\Users\fisch\AppData\Local\Microsoft\Windows\INetCookies 
ApplicationData         C:\Users\fisch\AppData\Roaming 
NetworkShortcuts        C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Network Shortcuts 
PrinterShortcuts        C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Printer Shortcuts 
Recent                  C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Recent 
SendTo                  C:\Users\fisch\AppData\Roaming\Microsoft\Windows\SendTo 
StartMenu               C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Start Menu 
Programs                C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Start Menu\Programs 
AdminTools              C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools 
Startup                 C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 
Templates               C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Templates 
Desktop                 C:\Users\fisch\Desktop 
DesktopDirectory        C:\Users\fisch\Desktop 
Favorites               C:\Users\fisch\Favorites 
MyMusic                 C:\Users\fisch\Music 
MyDocuments             C:\Users\fisch\OneDrive\Documents 
MyDocuments             C:\Users\fisch\OneDrive\Documents 
MyPictures              C:\Users\fisch\OneDrive\Pictures 
MyVideos                C:\Users\fisch\Videos 
CommonDesktopDirectory  C:\Users\Public\Desktop 
CommonDocuments         C:\Users\Public\Documents 
CommonMusic             C:\Users\Public\Music 
CommonPictures          C:\Users\Public\Pictures 
CommonVideos            C:\Users\Public\Videos 
Windows                 C:\Windows 
Fonts                   C:\Windows\Fonts 
Resources               C:\Windows\resources 
System                  C:\Windows\system32 
SystemX86               C:\Windows\SysWoW64 

Btw. "fisch "to pierwsze 5 liter mojego nazwiska (i to po niemiecku"ryba") . Jest to nazwa użytkownika przypisana podczas logowania się za pomocą konta Microsoft.

 5
Author: bytecode77,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-28 21:43:41
Environment.GetEnvironmentVariable("userprofile")

Próba poruszania się w górę z nazwanego folderu specjalnego jest podatna na problemy. Istnieje wiele powodów, dla których foldery nie będą tam, gdzie ich oczekujesz - użytkownicy mogą je przenieść na własną rękę, GPO może je przenieść, przekierowanie folderów do ścieżek UNC itp.

Użycie zmiennej środowiskowej dla userprofile powinno odzwierciedlać każdy z tych możliwych problemów.

 4
Author: Scott Ivey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-07-16 21:40:26

Try:

System.IO.Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName/
 3
Author: Jay Riggs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-07-16 21:26:51

Mieszanie się ze zmiennymi środowiskowymi lub twardo zakodowanymi przesunięciami folderów nadrzędnych nigdy nie jest dobrym pomysłem, gdy istnieje API, aby uzyskać pożądane informacje, zadzwoń SHGetSpecialFolderPath(...,CSIDL_PROFILE,...)

 1
Author: Anders,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-07-18 09:03:43
$env:USERPROFILE = "C:\\Documents and Settings\\[USER]\\"
 -3
Author: Jeff,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-12 09:20:48

Możesz użyć następującego kodu:

if(Platform.Equals("WinCE"))
{
    m_CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
}
else if(Platform.Equals("Win32NT"))
{
    m_CurrentPath = Directory.GetCurrentDirectory();
}

Więcej informacji patrz: Pobieranie bieżącej ścieżki katalogu zarówno w WinXP jak i WinCE z C #

 -3
Author: ksblog,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-11 14:25:14