c#中寻找文件夹根目录效果

希望能有详细的代码
能不能再详细点

详细代码

递归实现查找目录下的所有子目录和文件
public void FindFile(string dir) //参数为指定的目录
{
//在指定目录及子目录下查找文件,在listBox1中列出子目录及文件
DirectoryInfo Dir=new DirectoryInfo(dir);
try
{
foreach(DirectoryInfo d in Dir.GetDirectories()) //查找子目录
{
FindFile(Dir+d.ToString()+"\\");
listBox1.Items.Add(Dir+d.ToString()+"\\"); //listBox1中填加目录名
}
foreach(FileInfo f in Dir.GetFiles("*.*")) //查找文件
{
listBox1.Items.Add(Dir+f.ToString()); //listBox1中填加文件名
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}

调用
private void button1_Click(object sender, System.EventArgs e)
{
string currentdir="F:\\myprogram\\C#\\FileSearch"; //搜索的目录
if(currentdir[currentdir.Length-1]!='\\') //非根目录
currentdir+="\\";
FindFile(currentdir); //调用查找文件函数
}
加上 using System.IO;

//------------------------------------------------------------------------------------------------------
用asp.net(c#)编写程序得到本机指定目录下的所有文件
首先添加引用:
using System.IO;
然后在Page_Load中编写代码:
string FilePath = "c:\\test";
if(!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}
if(!Directory.Exists(FilePath + "\\Abnormal"))
{
Directory.CreateDirectory(FilePath + "\\Abnormal");
}
DirectoryInfo UnPostil = new DirectoryInfo(FilePath + "\\Abnormal");
FileInfo[] ArrUnPostil = UnPostil.GetFiles();
LB_Postil.Items.Clear();
foreach (FileInfo FileName in ArrUnPostil)
{
if(FileName.Length > 0)
{
LB_Postil.Items.Add(FileName.Name);
}
}
代码中的LB_Postil是一个LISTBOX服务器控件。
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-05-26
可以利用 System.IO.Path 类实现你所说的功能

using System.IO; // 先导入此命名空间

// 获取指定路径的根目录信息
string fullPath = System.Windows.Forms.Application.StartupPath;
string rootPath = Path.GetPathRoot(fullPath);

// rootPath 即为根目录
第2个回答  2009-05-26
application.startuppath就是获取应用程序的根目录!

比如

string mP=Application.StartUpath;
后你想找该文件夹下中其它文件或文件夹,采用递归方法就行了
第3个回答  2009-05-27
问题不明确,不过前面给的代码比较详细,很容易参照来写自己的代码
相似回答