トップ回答者
特殊フォルダの名称取得方法について

質問
-
Windows10 でC#を用いて、特殊フォルダの名称を取得したいのですが、方法がわからず困っております。
たとえば、ドキュメントフォルダであれば、Environment.GetFolderPathメソッドでパスを取得することは、可能ですが、
結果は、C:\Users\UserName\Documents といった物理パスが取得されます。
英語表記のフォルダ名称でなく、Windowsエクスプローラー上で表現されている名称を取得する
方法はないでしょうか?
(例) 下記のようにエクスプローラ上で表現されている名称を取得したいです。
- ドキュメント
- ピクチャ
- ビデオ
- ミュージック
回答
-
もちろんWindowsエクスプローラーに問い合わせる必要があります。
取得方法はいろいろありますが、簡単なのはSHGetNameFromIDList()を使うことでしょうか。ただしこのAPIで取得するためにはILDistが必要になります。パス文字列からILDistを構築するにはILCreateFromPath()があり使用後はILFree()します。
[DllImport("Shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)] static extern void SHGetNameFromIDList(IntPtr pidl, int sigdnName, out string ppszName); [DllImport("Shell32.dll", CharSet = CharSet.Unicode)] static extern IntPtr ILCreateFromPath(string pszPath); [DllImport("Shell32.dll")] static extern void ILFree(IntPtr pidl); static string GetFolderDisplayName(string fullpath) { var pidl = ILCreateFromPath(fullpath); if (pidl == null) throw new ArgumentException(); try { string pszName; SHGetNameFromIDList(pidl, 0, out pszName); return pszName; } finally { ILFree(pidl); } }
- 編集済み 佐祐理 2016年10月11日 4:18
- 回答としてマーク 特殊フォルダの名称を取得する方法について 2016年10月11日 6:26
すべての返信
-
もちろんWindowsエクスプローラーに問い合わせる必要があります。
取得方法はいろいろありますが、簡単なのはSHGetNameFromIDList()を使うことでしょうか。ただしこのAPIで取得するためにはILDistが必要になります。パス文字列からILDistを構築するにはILCreateFromPath()があり使用後はILFree()します。
[DllImport("Shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)] static extern void SHGetNameFromIDList(IntPtr pidl, int sigdnName, out string ppszName); [DllImport("Shell32.dll", CharSet = CharSet.Unicode)] static extern IntPtr ILCreateFromPath(string pszPath); [DllImport("Shell32.dll")] static extern void ILFree(IntPtr pidl); static string GetFolderDisplayName(string fullpath) { var pidl = ILCreateFromPath(fullpath); if (pidl == null) throw new ArgumentException(); try { string pszName; SHGetNameFromIDList(pidl, 0, out pszName); return pszName; } finally { ILFree(pidl); } }
- 編集済み 佐祐理 2016年10月11日 4:18
- 回答としてマーク 特殊フォルダの名称を取得する方法について 2016年10月11日 6:26