private void btnBrowseMachine_Click(object sender, EventArgs e) { IntPtr pidlRet = IntPtr.Zero; IntPtr pidlRoot = IntPtr.Zero; Shell32.SHGetSpecialFolderLocation(this.Handle, (int)Shell32.FolderID.NetworkNeighborhood, out pidlRoot); try { Shell32.BROWSEINFO bi = new Shell32.BROWSEINFO(); IntPtr buffer = Marshal.AllocHGlobal(Win32Utils.MAX_PATH); bi.hwndOwner = this.Handle; bi.pidlRoot = pidlRoot; bi.lpszTitle = "Browse for Machine"; bi.pszDisplayName = buffer; bi.ulFlags = (int)(Shell32.BrowseInfoFlags.BIF_BROWSEFORCOMPUTER | Shell32.BrowseInfoFlags.BIF_NONEWFOLDERBUTTON); pidlRet = Shell32.SHBrowseForFolder(ref bi); if (pidlRet != IntPtr.Zero) { string machineName = Marshal.PtrToStringAuto(bi.pszDisplayName).ToUpper(); cboMachine.Text = machineName; InsertRecentMachine(machineName); } // Free the buffer we've allocated on the global heap Marshal.FreeHGlobal(buffer); } finally { Shell32.IMalloc malloc; Shell32.SHGetMalloc(out malloc); malloc.Free(pidlRoot); if (pidlRet != IntPtr.Zero) { malloc.Free(pidlRet); } } }
public static extern IntPtr SHBrowseForFolder(ref Shell32.BROWSEINFO bi);