当前位置:编程学习 > C#/ASP.NET >>

应CNET要求。把目录浏览类转贴过来。这个是几个月以前写的。(来自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);
      &n

上一个:聊天主题:如何用C#定制个性化的界面控件
下一个:用C#创建COM对象(转自计算机世界)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,