應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: 在此處添加構造函數邏輯
//
}
}
}