質問

これを使用:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

私はこの出力を取得します:

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

すべてのユーザーのルートディレクトリを取得するにはどうすればよいですか?つまり:

"C:\\Documents and Settings\\[USER]\\"
役に立ちましたか?

解決

これが良い解決策になるかもしれません:これがVista/Win7またはXPであるかどうかを考慮に入れて、環境変数を使用せずに:

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

ただし、環境変数を使用することははるかに明確です。

他のヒント

試す:

System.Environment.GetEnvironmentVariable("USERPROFILE");

編集:

使用している.NETのバージョンが4以上の場合、 Environment.SpecialFolder 列挙:

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

これだけでuserprofileパスを取得できます。

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

また、調査中に非常に役立ちます Environment.SpecialFolder 列挙。 LINQPADを使用するか、ソリューションを作成してこのコードを実行します。

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

Folder Paths

これは私のマシンの結果です:

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 

ところで。 「フィッシュ」は私の姓の最初の5文字です (そして「魚」のドイツ語です). 。これは、Microsoftアカウントにサインインするときに割り当てられたユーザー名です。

Environment.GetEnvironmentVariable("userprofile")

名前付きのSpecialFolderからナビゲートしようとすることは、問題が発生しやすいです。フォルダーが期待する場所にない理由はたくさんあります - ユーザーは自分で移動でき、GPOはそれらを移動でき、フォルダーのリダイレクトはUNCパスなどに移動できます。

を使用して 環境変数 ユーザープロファイルは、これらの可能な問題のいずれかを反映する必要があります。

試す:

System.IO.Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName/

環境変数やハードコーディングされた親フォルダーオフセットをいじくり回すことは、必要な情報を取得するためのAPIがある場合、決して良い考えではありません。電話してください。 SHGetSpecialFolderPath(...,CSIDL_PROFILE,...)

$env:USERPROFILE = "C:\\Documents and Settings\\[USER]\\"

次のコードを使用できます。

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

詳細については、以下を参照してください。 C#でWinxpとWinceの両方で現在のディレクトリパスを取得します

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top