用C#怎样实现 文件夹浏览对话框 啊

sandy2001 2002-06-21 08:57:13
同上
...全文
118 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
visualbibi 2002-06-22
  • 打赏
  • 举报
回复
这个类可以。

/*
目录选择框类
*/

using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
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;
}

/// <summary>
/// Implements the SHBrowseForFolder function to show a common folder dialog.
/// </summary>
public class DirectoryDialog
{
private const int MAX_PATH = 260;
/// <summary>Specifies the type of items to browse for.</summary>
public enum BrowseForTypes
{
/// <summary>Browse for computers..</summary>
Computers = 0x1000,
/// <summary>Browse for directories.</summary>
Directories = 0x1,
/// <summary>Browse for files and directories.</summary>
/// <remarks>Only works on shell version 4.71 and higher!<remarks>
FilesAndDirectories = 0x4000, // Version 4.71
/// <summary>Browse for file system ancestors (an ancestor is a subfolder that is beneath the root folder in the namespace hierarchy.).</summary>
FileSystemAncestors = 0x8
}
[ DllImport( "ole32.dll", CharSet=CharSet.Ansi )]
private static extern int CoTaskMemFree(IntPtr hMem);
[ DllImport( "kernel32.dll", CharSet=CharSet.Ansi )]
private static extern IntPtr lstrcat(string lpString1, string lpString2);
[ DllImport( "shell32.dll", CharSet=CharSet.Ansi )]
private static extern IntPtr SHBrowseForFolder(ref BROWSEINFO lpbi);
[ DllImport( "shell32.dll", CharSet=CharSet.Ansi )]
private static extern int SHGetPathFromIDList(IntPtr pidList, StringBuilder lpBuffer);
/// <summary>Shows the common folder dialog.</summary>
/// <param name="hWndOwner">The owner of the folder dialog.</param>
protected bool RunDialog(IntPtr hWndOwner)
{
BROWSEINFO udtBI = new BROWSEINFO();
IntPtr lpIDList;
GCHandle hTitle = GCHandle.Alloc(Title, GCHandleType.Pinned);
// set the owner of the window
udtBI.hWndOwner = hWndOwner;
// set the owner of the window
udtBI.lpszTitle = Title;
// set the owner of the window
udtBI.ulFlags = (int)BrowseFor;
// create string buffer for display name
StringBuilder buffer = new StringBuilder(MAX_PATH);
buffer.Length = MAX_PATH;
udtBI.pszDisplayName = buffer.ToString();
// show the 'Browse for folder' dialog
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);
// get the path from the IDList
SHGetPathFromIDList(lpIDList, path);
m_Selected = path.ToString();
}
// free the block of memory
CoTaskMemFree(lpIDList);
}
else
{
return false;
}
return true;
}
/// <summary>Shows the common folder dialog.</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>Specifies the title of the dialog.</summary>
/// <value>The title of the dialog.</value>
/// <exceptions cref="ArgumentNullException">Thrown when the specified value is null (VB.NET: Nothing)</exceptions>
public string Title
{
get
{
return m_Title;
}
set
{
if (value == null)
throw new ArgumentNullException();
m_Title = value;
}
}
/// <summary>Returns the selected item.</summary>
/// <value>The selected item.</value>
public string Selected
{
get
{
return m_Selected;
}
}
/// <summary>Specifies what to browse for.</summary>
/// <value>What to browse for.</value>
public BrowseForTypes BrowseFor
{
get
{
return m_BrowseFor;
}
set
{
m_BrowseFor = value;
}
}
// private declares
private BrowseForTypes m_BrowseFor = BrowseForTypes.Directories;
private string m_Title = "";
private string m_Selected = "";
}
晒屁屁 2002-06-22
  • 打赏
  • 举报
回复
是不是那个公用对话框,用API,呵呵我早成功了
snewxf 2002-06-21
  • 打赏
  • 举报
回复
我以前发的帖子。
http://www.csdn.net/expert/topic/728/728549.xml?temp=.1470301
就这个了。感觉还可以。
还有一个。
你可以自已写一个目录对话框。DLL。
然后引用它。
好运。
juqiang 2002-06-21
  • 打赏
  • 举报
回复
shit!!!msdn怎么这么慢?!!!???等会给你paste code

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧