「C:\Users\Default\Desktop」を出力するLINQPad (言語: C# プログラム) のスニペット:
void Main()
{
GetFolderPath(Environment.SpecialFolder.Desktop).Dump();
}
// Define other methods and classes here
[DllImport("shfolder.dll", CharSet=CharSet.Auto)]
internal static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, int hToken, int dwFlags, StringBuilder lpszPath);
public static string GetFolderPath(Environment.SpecialFolder folder)
{
if (!Enum.IsDefined(typeof(Environment.SpecialFolder), folder))
{
throw new Exception("Crap");
}
StringBuilder lpszPath = new StringBuilder(260);
SHGetFolderPath(IntPtr.Zero, (int) folder, -1, 0, lpszPath);
string path = lpszPath.ToString();
new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
return path;
}
編集:LINQPadに次のインポートがありました
System.Runtime.InteropServices
System.Globalization
System.Security.Permissions
リフレクターを使用して確認し、代わりにデフォルト ユーザーを取得する hToken として -1 を渡すことを指定Environment.GetFolderPath
することを確認しました。SHGetFolderPath