7

您好我必须从目录中的指定路径获取文件。这是我写的方法,但我没有从子文件夹中获取文件。

Private void getfiles(){
Directoryinfo info = new Directoryinfo(configurationmanager.appsettings["Targetroot"].tostring ());
if (info.exists){
     Gvfiles.datasource = info.GetFiles();
     Gvfiles.databind();
   }
}
4

4 回答 4

15

您将需要包含 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

于 2013-04-24T21:13:47.383 回答
0
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();
}
于 2012-08-02T10:42:31.750 回答
0

这将返回(立即)子目录的数组:

System.IO.DirectoryInfo ParentDirectory = new System.IO.DirectoryInfo(ParentPath);
System.IO.DirectoryInfo[] DirectoryArr = ParentDirectory.GetDirectories();

我相信你可以找到一种方法来适应你的喜好。

于 2012-08-02T10:56:26.977 回答
0
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;
}
于 2019-05-06T07:02:31.777 回答