请问如何获得文件夹的路径(而不是文件),如c:\WINNT?用FileDialog类? 在System.IO里面有Directory ,DirectoryInfo,File ,FileInfo等类 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这些Directory ,DirectoryInfo,File ,FileInfo当然不行了,如果要到网络路径怎么办? 试试System.Windows.Forms.FolderBrowserDialog .net2003 里面有目录选择框。或者在.net7里面引用vb.net的commondialog. private Microsoft.VisualBasic.Compatibility.VB6.DriveListBox drlb; private Microsoft.VisualBasic.Compatibility.VB6.DirListBox dlb; private Microsoft.VisualBasic.Compatibility.VB6.FileListBox flb; 下面的代码忘了是从哪里找到的了using System;using System.Windows.Forms;using System.Diagnostics;using System.Runtime.InteropServices;using System.Collections; /// <summary> /// Summary description for FolderBrowser. /// </summary> /// [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)][ComVisible(true)]public class BROWSEINFO { public IntPtr hwndOwner; public IntPtr pidlRoot; public IntPtr pszDisplayName; public string lpszTitle; public int ulFlags; public IntPtr lpfn; public IntPtr lParam; public int iImage;} [Flags, Serializable]public enum BrowseFlags { BIF_DEFAULT = 0x0000, BIF_BROWSEFORCOMPUTER = 0x1000, BIF_BROWSEFORPRINTER = 0x2000, BIF_BROWSEINCLUDEFILES = 0x4000, BIF_BROWSEINCLUDEURLS = 0x0080, BIF_DONTGOBELOWDOMAIN = 0x0002, BIF_EDITBOX = 0x0010, BIF_NEWDIALOGSTYLE = 0x0040, BIF_NONEWFOLDERBUTTON = 0x0200, /// </summary> BIF_RETURNFSANCESTORS = 0x0008, BIF_RETURNONLYFSDIRS = 0x0001, BIF_SHAREABLE = 0x8000, BIF_STATUSTEXT = 0x0004, BIF_UAHINT = 0x0100, BIF_VALIDATE = 0x0020, BIF_NOTRANSLATETARGETS = 0x0400,} public class API { [DllImport("shell32.dll", PreserveSig=true, CharSet=CharSet.Auto)] public static extern IntPtr SHBrowseForFolder(BROWSEINFO bi); [DllImport("shell32.dll", PreserveSig=true, CharSet=CharSet.Auto)] public static extern bool SHGetPathFromIDList(IntPtr pidl, IntPtr pszPath); [DllImport("shell32.dll", PreserveSig=true, CharSet=CharSet.Auto)] public static extern int SHGetSpecialFolderLocation(IntPtr hwnd, int csidl, ref IntPtr ppidl);}public class FolderBrowser { private string m_strDirectoryPath; private string m_strTitle; private string m_strDisplayName; private BrowseFlags m_Flags; public FolderBrowser() { // // TODO: Add constructor logic here //可以定制对话框外观 m_Flags = BrowseFlags.BIF_DEFAULT; m_strTitle = ""; } public string DirectoryPath { get{return this.m_strDirectoryPath;} } public string DisplayName { get{return this.m_strDisplayName;} } public string Title { set{this.m_strTitle = value;} } public BrowseFlags Flags { set{this.m_Flags = value;} } public DialogResult ShowFolderBrowser() { BROWSEINFO bi = new BROWSEINFO(); bi.pszDisplayName = IntPtr.Zero; bi.lpfn = IntPtr.Zero; bi.lParam = IntPtr.Zero; bi.lpszTitle = "Select Folder"; IntPtr idListPtr = IntPtr.Zero; IntPtr pszPath = IntPtr.Zero; try { if (this.m_strTitle.Length != 0) { bi.lpszTitle = this.m_strTitle; } bi.ulFlags = (int)this.m_Flags; bi.pszDisplayName = Marshal.AllocHGlobal(256); idListPtr = API.SHBrowseForFolder(bi); if (idListPtr == IntPtr.Zero) { return DialogResult.Cancel; } pszPath = Marshal.AllocHGlobal(256); bool bRet = API.SHGetPathFromIDList(idListPtr, pszPath); m_strDirectoryPath = Marshal.PtrToStringAuto(pszPath); this.m_strDisplayName = Marshal.PtrToStringAuto(bi.pszDisplayName); } catch (Exception ex) { Trace.WriteLine(ex.Message); return DialogResult.Abort; } finally { if (idListPtr != IntPtr.Zero) { Marshal.FreeHGlobal(idListPtr); } if (pszPath != IntPtr.Zero) { Marshal.FreeHGlobal(pszPath); } if (bi != null) { Marshal.FreeHGlobal(bi.pszDisplayName); } } return DialogResult.OK; }} 1.1的话,就有专门的FolderBrowserDialog 类一下就变简单了。 目录选择http://support.microsoft.com/default.aspx?scid=kb;en-us;Q306285 关于entity framework中的事物处理 怎样设置Panel的滚动条的位置? 关于C#中的字符串提取函数,如何提取c:\\data\name.dat中的name.dat文件名? C#.net中如何用定时器实现控件中的文字闪烁??? 关于一个算法(高手请给予解答) 请教一个关于C#操作EXCEL画曲线图的问题! 有关工具栏,进来坐坐吧!! c#如何操作NTFS权限? ORACLE DB開發 关于对象实例保存的问题 如何读写forpro表? 我能不能用手机发一条短信,用我的电脑受到呢?
或者在.net7里面引用vb.net的commondialog.
private Microsoft.VisualBasic.Compatibility.VB6.DriveListBox drlb;
private Microsoft.VisualBasic.Compatibility.VB6.DirListBox dlb;
private Microsoft.VisualBasic.Compatibility.VB6.FileListBox flb;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections;
/// <summary>
/// Summary description for FolderBrowser.
/// </summary>
/// [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
[ComVisible(true)]
public class BROWSEINFO
{
public IntPtr hwndOwner;
public IntPtr pidlRoot;
public IntPtr pszDisplayName;
public string lpszTitle;
public int ulFlags;
public IntPtr lpfn;
public IntPtr lParam;
public int iImage;
} [Flags, Serializable]
public enum BrowseFlags
{
BIF_DEFAULT = 0x0000, BIF_BROWSEFORCOMPUTER = 0x1000, BIF_BROWSEFORPRINTER = 0x2000, BIF_BROWSEINCLUDEFILES = 0x4000, BIF_BROWSEINCLUDEURLS = 0x0080, BIF_DONTGOBELOWDOMAIN = 0x0002, BIF_EDITBOX = 0x0010, BIF_NEWDIALOGSTYLE = 0x0040, BIF_NONEWFOLDERBUTTON = 0x0200, /// </summary>
BIF_RETURNFSANCESTORS = 0x0008, BIF_RETURNONLYFSDIRS = 0x0001, BIF_SHAREABLE = 0x8000, BIF_STATUSTEXT = 0x0004, BIF_UAHINT = 0x0100, BIF_VALIDATE = 0x0020, BIF_NOTRANSLATETARGETS = 0x0400,
}
public class API
{
[DllImport("shell32.dll", PreserveSig=true, CharSet=CharSet.Auto)]
public static extern IntPtr SHBrowseForFolder(BROWSEINFO bi); [DllImport("shell32.dll", PreserveSig=true, CharSet=CharSet.Auto)]
public static extern bool SHGetPathFromIDList(IntPtr pidl, IntPtr pszPath); [DllImport("shell32.dll", PreserveSig=true, CharSet=CharSet.Auto)]
public static extern int SHGetSpecialFolderLocation(IntPtr hwnd, int csidl, ref IntPtr ppidl);
}
public class FolderBrowser
{
private string m_strDirectoryPath;
private string m_strTitle;
private string m_strDisplayName;
private BrowseFlags m_Flags;
public FolderBrowser()
{
//
// TODO: Add constructor logic here
//可以定制对话框外观
m_Flags = BrowseFlags.BIF_DEFAULT;
m_strTitle = ""; } public string DirectoryPath
{
get{return this.m_strDirectoryPath;}
}
public string DisplayName
{
get{return this.m_strDisplayName;}
}
public string Title
{
set{this.m_strTitle = value;}
}
public BrowseFlags Flags
{
set{this.m_Flags = value;}
}
public DialogResult ShowFolderBrowser()
{
BROWSEINFO bi = new BROWSEINFO();
bi.pszDisplayName = IntPtr.Zero;
bi.lpfn = IntPtr.Zero;
bi.lParam = IntPtr.Zero;
bi.lpszTitle = "Select Folder";
IntPtr idListPtr = IntPtr.Zero;
IntPtr pszPath = IntPtr.Zero;
try
{
if (this.m_strTitle.Length != 0)
{
bi.lpszTitle = this.m_strTitle;
}
bi.ulFlags = (int)this.m_Flags;
bi.pszDisplayName = Marshal.AllocHGlobal(256);
idListPtr = API.SHBrowseForFolder(bi);
if (idListPtr == IntPtr.Zero)
{
return DialogResult.Cancel;
}
pszPath = Marshal.AllocHGlobal(256);
bool bRet = API.SHGetPathFromIDList(idListPtr, pszPath);
m_strDirectoryPath = Marshal.PtrToStringAuto(pszPath);
this.m_strDisplayName = Marshal.PtrToStringAuto(bi.pszDisplayName);
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
return DialogResult.Abort;
}
finally
{
if (idListPtr != IntPtr.Zero)
{
Marshal.FreeHGlobal(idListPtr);
}
if (pszPath != IntPtr.Zero)
{
Marshal.FreeHGlobal(pszPath);
}
if (bi != null)
{
Marshal.FreeHGlobal(bi.pszDisplayName);
}
}
return DialogResult.OK;
}
}
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q306285