應CNET要求。把瀏覽目錄類轉貼過來。這個是幾個月以前寫的。(來自ASP.NET版)

來源:互聯網
上載者:User
asp.net using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Blood.Com.ClassLib
{
    /// <summary>
    /// 目錄對話方塊控制項
    /// </summary>
    public class DirectoryDialog
    {
        [ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
        ///<summary>
        ///瀏覽資訊
        ///</summary>
        public struct BROWSEINFO
        {
            public IntPtr hWndOwner;
            public int pIDLRoot;
            public string pszDisplayName;
            public string lpszTitle;
            public int ulFlags;
            public int lpfnCallback;
            public int lParam;
            public int iImage;
        }

        private const int MAX_PATH = 260;
        /// <summary>
        /// 指定瀏覽類型
        /// </summary>
        public enum BrowseForTypes
        {
            /// <summary>
            /// 瀏覽電腦
            /// </summary>
            Computers = 0x1000,
            /// <summary>
            /// 瀏覽目錄
            /// </summary>
            Directories = 0x1,
            /// <summary>
            /// 瀏覽目錄和檔案
            /// </summary>
            /// <remarks>只能工作於4.71或更高的版本<remarks>
            FilesAndDirectories = 0x4000, // 4.71版
            /// <summary>
            /// 瀏覽系統根目錄
            /// </summary>
            FileSystemAncestors = 0x8
        }

        [ DllImport( "ole32.dll")]
        private static extern int CoTaskMemFree(IntPtr hMem);
        [ DllImport( "kernel32.dll")]
        private static extern IntPtr lstrcat(string lpString1, string lpString2);
        [ DllImport( "shell32.dll")]
        private static extern IntPtr SHBrowseForFolder(ref BROWSEINFO lpbi);
        [ DllImport( "shell32.dll")]
        private static extern int SHGetPathFromIDList(IntPtr pidList, StringBuilder lpBuffer);
        /// <summary>
        /// 顯示公用檔案夾對話方塊
        /// </summary>
        /// <param name="hWndOwner">檔案夾對話方塊所有者</param>
        protected bool RunDialog(IntPtr hWndOwner)
        {
            BROWSEINFO udtBI = new BROWSEINFO();
            IntPtr lpIDList;
            GCHandle hTitle = GCHandle.Alloc(Title, GCHandleType.Pinned);
            // 設定WINDOWS的所有者
            udtBI.hWndOwner = hWndOwner;
            // 設定WINDOWS的所有者
            udtBI.lpszTitle =  Title;
            // 設定WINDOWS的所有者
            udtBI.ulFlags  = (int)BrowseFor;
            // 建立一個字串緩衝用來顯示名稱
            StringBuilder buffer = new StringBuilder(MAX_PATH);
            buffer.Length = MAX_PATH;
            udtBI.pszDisplayName = buffer.ToString();
            // 顯示瀏覽目錄對話方塊
            lpIDList = SHBrowseForFolder(ref udtBI);
            hTitle.Free();
            if (lpIDList.ToInt64() != 0)
            {
                if (BrowseFor == BrowseForTypes.Computers)
                {
                    m_Selected = udtBI.pszDisplayName.Trim();
                }
                else
                {
                    StringBuilder path = new StringBuilder(MAX_PATH);
                    //從lpIDList中取得路徑
                    SHGetPathFromIDList(lpIDList, path);
                    m_Selected = path.ToString();
                }
                //釋放記憶體
                CoTaskMemFree(lpIDList);
            }
            else
            {
                return false;
            }
            return true;
        }
        /// <summary>顯示公用檔案夾對話方塊</summary>
        public DialogResult ShowDialog()
        {
            return ShowDialog(null);
        }
        /// <summary>Shows the common folder dialog.</summary>
        /// <param name="owner">The owner of the folder dialog.</param>
        public DialogResult ShowDialog(IWin32Window owner)
        {
            IntPtr handle;
            if (owner != null)
                handle = owner.Handle;
            else
                handle = IntPtr.Zero;
            if (RunDialog(handle))
            {
                return DialogResult.OK;
            }
            else
            {
                return DialogResult.Cancel;
            }
        }

        /// <summary>
        /// 指定對話方塊的標題
        /// </summary>
        /// <value>對話方塊標題</value>
        /// <exceptions cref="ArgumentNullException">當值為null(VB.NET為Nothing)時拋出錯誤</exceptions>
        public string Title
        {
            get
            {
                return m_Title;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException();
                m_Title = value;
            }
        }
        /// <summary>返回選擇的項目</summary>
        /// <value>選擇的項目</value>
        public string Selected
        {
            get
            {
                return m_Selected;
            }
        }
        /// <summary>指定瀏覽類型</summary>
        /// <value>瀏覽類型</value>
        public BrowseForTypes BrowseFor
        {
            get
            {
                return m_BrowseFor;
            }
            set
            {
                m_BrowseFor = value;
            }
        }
        //申明私人變數
        private BrowseForTypes m_BrowseFor = BrowseForTypes.Directories;
        private string m_Title = "";
        private string m_Selected = "";

        /// <summary>
        /// 建構函式
        /// </summary>
        public DirectoryDialog()
        {
            //
            // TODO: 在此處添加建構函式邏輯
            //
        }
    }
}


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。