您好我必须从目录中指定的路径获取文件。 这是我写的方法,但我没有从子文件夹中获取的文件。
Private void getfiles(){
Directoryinfo info = new Directoryinfo(configurationmanager.appsettings["Targetroot"].tostring ());
if (info.exists){
Gvfiles.datasource = info.GetFiles();
Gvfiles.databind();
}
}
您将要包括SearchOption.AllDirectories。
例:
info.GetFiles("*", SearchOption.AllDirectories);
参考: http://msdn.microsoft.com/en-us/library/ms143327(v=vs.80).aspx
和: http://msdn.microsoft.com/en-us/library/ms143448(v=vs.80).aspx
这返回(立即)子目录的数组:
System.IO.DirectoryInfo ParentDirectory = new System.IO.DirectoryInfo(ParentPath);
System.IO.DirectoryInfo[] DirectoryArr = ParentDirectory.GetDirectories();
我相信你能找到一种方法,以使其适应你的喜好。
DirectoryInfo info = new DrectoryInfo(configurationmanager.appsettings["Targetroot"].tostring ());
//FileInfo[] _files = info.GetFiles("You could set a search pattern");
//FileInfo[] _files = info.GetFiles("*.aspx");
FileInfo[] _files = info.GetFiles();
Gvfiles.datasource = _files;
Gvfiles.databind();
要么:
DirectoryInfo info = new DirectoryInfo(Server.MapPath("/"));
DirectoryInfo[] _info = info.GetDirectories();
for (int i = 0; i < _info.Length; i++)
{
FileInfo[] files = _info[i].GetFiles("search pattern");
Gvfiles.datasource = files;
Gvfiles.databind();
}
private List<String> DirSearch(string sDir)
{
List<String> files = new List<String>();
try
{
foreach (string f in Directory.GetFiles(sDir))
{
files.Add(f);
}
foreach (string d in Directory.GetDirectories(sDir))
{
files.AddRange(DirSearch(d));
}
}
catch (System.Exception excpt)
{
MessageBox.Show(excpt.Message);
}
return files;
}