用asp.net读取当前文件夹所有文件名和名称,怎么做?

jenie 2005-07-06 09:31:50
用asp.net读取当前文件夹所有文件名和名称,怎么做?
...全文
2547 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
PerfectStar 2005-07-06
  • 打赏
  • 举报
回复
一个递归函数:

#region 树控件,遍历文件夹

public void GetSpecFF(string Folder,TreeNodeCollection who)
{
string[]dires=Directory.GetDirectories(Folder);
foreach(string dire in dires)
{
string[]filepart=dire.Split('\\');
string filename=filepart[filepart.Length-1].ToString();//析取文件名
TreeNode newNode = new TreeNode();
newNode.Text=filename;
newNode.ID=dire;
newNode.ImageUrl=@"/GreatOA/Images/Icon/005.gif";
who.Add(newNode);
GetSpecFF(dire,newNode.Nodes);
}
string[]files=Directory.GetFiles(Folder);
foreach(string file in files)
{
string[]filepart=file.Split('\\');
string filename=filepart[filepart.Length-1].ToString();
TreeNode newNode = new TreeNode();
newNode.Text=filename;
newNode.ID=file;//保存全路径
newNode.ImageUrl=@"/GreatOA/Images/Icon/046.gif";
who.Add(newNode);
}
}
#endregion
lovefootball 2005-07-06
  • 打赏
  • 举报
回复

//获取文件名,没有子目录
using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
// Only get files that begin with the letter "c."
string[] dirs = Directory.GetFiles(@"c:\", "c*");
Console.WriteLine("The number of files starting with c is {0}.", dirs.Length);
foreach (string dir in dirs)
{
Console.WriteLine(dir);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}

如果要获取子目录,就用
using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
// Only get subdirectories that begin with the letter "p."
string[] dirs = Directory.GetDirectories(@"c:\", "p*");
Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length);
foreach (string dir in dirs)
{
Console.WriteLine(dir);//这里递归
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
chinasqf 2005-07-06
  • 打赏
  • 举报
回复
好详细
wudixiaocaoren 2005-07-06
  • 打赏
  • 举报
回复
原理:http://www.microsoft.com/china/technet/community/scriptcenter/resources/hey1020.mspx

不取得子目录的话用这个:
using System.IO;
string[] dirs = Directory.GetDirectories(@"c:\");//路径
foreach (string dir in dirs)
{
Console.WriteLine(dir);
}
文件的话把GetDirectories改成GetFiles

如果要递归的话,就是想取得此目录下所有子目录和文件的办法用这个:
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;
阿牛在线 2005-07-06
  • 打赏
  • 举报
回复
System.IO
递归
renyu732 2005-07-06
  • 打赏
  • 举报
回复
System.IO命名空间下有.
建议楼主先看一下SDK.

然后再用一个循环,读取所有文件名...

62,051

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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