Antonio Mailtraq
Antonio Mailtraq

Reputation: 1407

C# file info in datagridview

I can't to count number of files within a directory using DirectoryInfo and FileInfo

I have tried this code without success, because I don't have error but can't show in 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

enter image description here

I need show the nr files on subdirectory on GridView Control, now is empty :

enter image description here

Upvotes: 0

Views: 532

Answers (2)

Hamamelis
Hamamelis

Reputation: 2105

About you want to show folder and all files of this folder in gridview, please check this article:

Nested GridView Example in ASP.Net using C# and VB.Net

Upvotes: 0

Patrick Artner
Patrick Artner

Reputation: 51683

filescontains the files of directory - nrFiles are the amount of files of the subdirectories of directory - you probably want the to requery the files from the subdirectory your currently in instead of reusing the files from it´s parent folder

Upvotes: 0

Related Questions