#c_sharp
Как можно получить имя папки "Мой компьютер" для windows 10. // тут должно быть имя This PC, а не MyComputer. string name=Environment.SpecialFolder.MyComputer.ToString();
Ответы
Ответ 1
Имя для виртуального каталога проводника можно получить с помощью интерфейса IShellItem: using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace WindowsFormsTest1 { class ShellFolder { //Получает локализованное имя для системной папки public static string GetKnownFolderName(ref Guid FolderID) { //https://stackoverflow.com/questions/41423491/how-to-get-localized-name-of-known-folder IntPtr p = IntPtr.Zero; IShellItem pItem = null; IntPtr pString = IntPtr.Zero; Guid guid = new Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE");//IShellItem int hr; hr = SHGetKnownFolderIDList(ref FolderID, 0, IntPtr.Zero, out p); if (hr != 0) throw Marshal.GetExceptionForHR(hr); try { hr = SHCreateItemFromIDList(p, ref guid, out pItem); if (hr != 0) throw Marshal.GetExceptionForHR(hr); hr = pItem.GetDisplayName(0, out pString); if (hr != 0) throw Marshal.GetExceptionForHR(hr); string s = Marshal.PtrToStringUni(pString); return s; } finally { if (p != IntPtr.Zero) IlFree(p); if (pString != IntPtr.Zero) CoTaskMemFree(pString); if (pItem != null) Marshal.ReleaseComObject(pItem); } } //Получает локализованное имя для системной папки (устаревший вариант) public static string GetKnownFolderName(uint csidl) { //https://stackoverflow.com/questions/41423491/how-to-get-localized-name-of-known-folder IntPtr p = IntPtr.Zero; IShellItem pItem = null; IntPtr pString = IntPtr.Zero; Guid guid = new Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE");//IShellItem int hr; hr = SHGetFolderLocation(IntPtr.Zero, (int)csidl, IntPtr.Zero, 0, out p); if (hr != 0) throw Marshal.GetExceptionForHR(hr); try { hr = SHCreateItemFromIDList(p, ref guid, out pItem); if (hr != 0) throw Marshal.GetExceptionForHR(hr); hr = pItem.GetDisplayName(0, out pString); if (hr != 0) throw Marshal.GetExceptionForHR(hr); string s = Marshal.PtrToStringUni(pString); return s; } finally { if (p != IntPtr.Zero) IlFree(p); if (pString != IntPtr.Zero) CoTaskMemFree(pString); if (pItem != null) Marshal.ReleaseComObject(pItem); } } public static Guid FOLDERID_ComputerFolder = new Guid("0AC0837C-BBF8-452A-850D-79D08E667CA7"); public static Guid FOLDERID_NetworkFolder = new Guid("D20BEEC4-5CA8-4905-AE3B-BF251EA09B53"); //https://www.pinvoke.net/default.aspx/shell32.shgetknownfolderpath //CSIDL Folder Constants public const uint CSIDL_DESKTOP = 0x00;//Рабочий стол public const uint CSIDL_NETWORK = 0x12;//Сетевое окружение public const uint CSIDL_DRIVES = 0x0011;//Компьютер //... //https://github.com/lxn/win/blob/master/shell32.go [DllImport("shell32.dll")] static extern int SHCreateItemFromIDList( /*PCIDLIST_ABSOLUTE*/ IntPtr pidl, ref Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out IShellItem ppv); [DllImport("shell32.dll")] public static extern int SHGetKnownFolderIDList(ref Guid rfid, int dwFlags, IntPtr hToken, out IntPtr ppidl); [DllImport("shell32.dll")] static extern int SHGetFolderLocation(IntPtr hwndOwner, int nFolder, IntPtr hToken, uint dwReserved, out IntPtr ppidl); [DllImport("shell32.dll", EntryPoint = "ILFree")] static extern void IlFree(IntPtr p); [DllImport("ole32.dll")] static extern void CoTaskMemFree(IntPtr pv); } [ComImport, Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IShellItem { int a(); int b(); [PreserveSig] int GetDisplayName([In] uint sigdnName, out IntPtr ppszName); //SIGDN_NORMALDISPLAY = 0x00000000 int c(); int d(); } } Использование: private void button1_Click(object sender, EventArgs e) { string name = ShellFolder.GetKnownFolderName(ref ShellFolder.FOLDERID_ComputerFolder); MessageBox.Show(name); }
Комментариев нет:
Отправить комментарий