コード例 #1
0
 public static extern IntPtr SHBrowseForFolder(BROWSEINFO bi);
コード例 #2
0
        /// <summary>
        /// Call this method you have set the title, flags, etc. This will bring up
        /// the browser dialog box. Pick the folder you want to select and hit OK. If
        /// user clicks on Cancel button then no values for directory path, etc. are
        /// returned. If user hits OK and not errors occur, then the reurn value from 
        /// the method id DialogResult.OK otherwise it can be DialogResult.Cancel or
        /// DialogResult.Abort.
        /// </summary>
        /// <returns></returns>
        public DialogResult ShowDialog()
        {
            // Create
            BROWSEINFO bi = new BROWSEINFO();
            bi.pszDisplayName = IntPtr.Zero;
            bi.lpfn = IntPtr.Zero;
            bi.lParam = IntPtr.Zero;
            bi.lpszTitle = "Pfad für das lyra2 HTML-Verzeichnis...";
            IntPtr idListPtr = IntPtr.Zero;
            IntPtr pszPath = IntPtr.Zero;
            try
            {
                if (this.m_strTitle.Length != 0)
                {
                    bi.lpszTitle = this.m_strTitle;
                }
                bi.ulFlags = (int) this.m_Flags;
                bi.pszDisplayName = Marshal.AllocHGlobal(256);
                // Call SHBrowseForFolder
                idListPtr = Win32SDK.SHBrowseForFolder(bi);
                // Check if the user cancelled out of the dialog or not.
                if (idListPtr == IntPtr.Zero)
                {
                    return DialogResult.Cancel;
                }

                // Allocate ncessary memory buffer to receive the folder path.
                pszPath = Marshal.AllocHGlobal(256);
                // Call SHGetPathFromIDList to get folder path.
                // Convert the returned native poiner to string.
                m_strDirectoryPath = Marshal.PtrToStringAuto(pszPath);
                this.m_strDisplayName = Marshal.PtrToStringAuto(bi.pszDisplayName);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                return DialogResult.Abort;
            }
            finally
            {
                // Free the memory allocated by shell.
                if (idListPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(idListPtr);
                }
                if (pszPath != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pszPath);
                }
                if (bi != null)
                {
                    Marshal.FreeHGlobal(bi.pszDisplayName);
                }
            }
            return DialogResult.OK;
        }