2017-10-12 104 views
1

使用我不能算一个目录中的文件数量的DirectoryInfo的FileInfoC#文件信息

我曾尝试没有成功这个代码,因为我没有错误,但可以” t在gridview中显示。

我的代码如下。

你能帮我吗?

预先感谢您的任何帮助,真的很感激。

public DataTable getAllFiles() 
{ 
    DataTable dt = new DataTable(); 

    sql = @String.Format(" SELECT * FROM doTable ;"); 

    using (OdbcConnection cn = 
     new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString)) 
    { 
     using (OdbcCommand cmd = 
      new OdbcCommand(sql, cn)) 
     { 
      try 
      { 
       cmd.Connection.Open(); 

       using (OdbcDataReader reader = cmd.ExecuteReader()) 
       { 
        if (reader.HasRows) 
        { 
         dt.Columns.Add("Nr of Files in directory"); 

         while (reader.Read()) 
         { 
          FilePath = Server.MapPath("/aspnet/" + reader["sFolder"].ToString().Replace('/', '\\')); 

          Response.Write(FilePath); 

          DirectoryInfo directory = new DirectoryInfo(@FilePath); 
          DirectoryInfo[] subDirectories = directory.GetDirectories(); 
          FileInfo[] files = directory.GetFiles(); 

          foreach (DirectoryInfo dirInfo in subDirectories) 
          { 
           int nrFiles = Directory.EnumerateFiles(dirInfo.FullName, "*.*", SearchOption.AllDirectories).Count(); 
           Response.Write(nrFiles.ToString() + "<br />"); 
           //Here the output is correct// 

           foreach (FileInfo f in files) 
           { 
            DataRow dr = dt.NewRow(); 
            dr[0] = nrFiles.ToString(); 
           } 

           gvDownload.DataSource = subDirectories; 
           gvDownload.DataBind(); 
          } 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       throw new ApplicationException("operation failed!", ex); 
      } 
      finally 
      { 
       cmd.Connection.Close(); 
      } 
     } 
    } 
    return dt; 
} 

#Edit 01

enter image description here

我需要显示在GridView控件上子目录的NR文件,现在是空的:

enter image description here

+0

你能解释一下你正在尝试做什么? – alexay68

+0

@ alexay68谢谢你的回复。我尝试在我的服务器上的一个目录中的gridview文件夹,子文件夹和文件中显示。在DB中,我记住了列sFolder这个字符串控制/ Imp/foo –

回答

0

files包含directory文件 - nrFilesdirectory子目录的文件数量 - 你可能希望从你的子目录当前,而不是从it's父文件夹重复使用files重新查询文件

+0

感谢您的回复,请参阅**#编辑01 **在我的第一个问题中 –

+0

1.您是 - 在子目录的foreach中 - 重新绑定子目录一遍又一遍 - 将其移出循环。 2.您填写本地DataTable dt,它与您绑定的数据源没有连接。 3.您可能需要一些valueconverter将绑定的子目录转换为可显示的内容 - 没有足够的代码来准确知道需要什么 - 这是另一个问题和难题。 –

+0

谢谢,我也可以改变系统和代码来实现目标。你有什么建议吗? –