简体   繁体   English

datagridview中的C#文件信息

[英]C# file info in datagridview

I can't to count number of files within a directory using DirectoryInfo and FileInfo我无法使用DirectoryInfoFileInfo 计算目录中的文件数

I have tried this code without success, because I don't have error but can't show in gridview.我试过这段代码没有成功,因为我没有错误但无法在gridview中显示。

My code below.我的代码如下。

Can you help me?你能帮助我吗?

Thank you in advance for any help, really appreciated.预先感谢您的任何帮助,非常感谢。

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 #编辑 01

在此处输入图片说明

I need show the nr files on subdirectory on GridView Control , now is empty :我需要在GridView Control 的子目录中显示nr 文件,现在是空的:

在此处输入图片说明

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

About you want to show folder and all files of this folder in gridview, please check this article:关于你想在gridview中显示文件夹和该文件夹下的所有文件,请查看这篇文章:

Nested GridView Example in ASP.Net using C# and VB.Net 使用 C# 和 VB.Net 在 ASP.Net 中嵌套 GridView 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM