应CNET要求。把目录浏览类转贴过来。这个是几个月以前写的。(来自ASP.NET版)
2024-07-10 13:04:04
供稿:网友
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: 在此处添加构造函数逻辑
//
}
}
}