在c# 中如何实现读取制定目录下的文件名?急用,50分。请高手指点!
//是要遍历该文件夹下的所有文件吗?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 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.IO;//要增加System.IO名称空间
...
string[] dirs = Directory.GetFiles(@"c:\", "c*");
MessageBox.Show(dirs.Length.ToString());//文件个数
foreach (string dir in dirs) //循环读取文件名称
{
MessageBox.Show(dir);
}
if(!this.IsPostBack)
{
string fpath=Path.Combine(Server.MapPath("."),"UpLoad");
string[] f=Directory.GetFiles(fpath);
int length=fpath.Length;
StringBuilder sb = new StringBuilder();
StringBuilder FPathStr=new StringBuilder();
sb.Append("可下载文件:<br>");
string fileName,filePath;
foreach(string fi in f)
{
FPathStr.Append(fi);
FPathStr.Remove(0,length+1);
fileName=FPathStr.ToString();
filePath=Path.Combine("UpLoad",fileName);
sb.Append("<li><a href="+filePath+">"+fileName+"  </a>"+"</li><br>");
FPathStr.Remove(0,FPathStr.Length);
}
Label1.Text=sb.ToString();
}
StringBuilder是不是需要添加引用啊!