How can i read multiple files and show reading progress of every files one by one into dataGridView?

Nitesh Rathi 1 Reputation point
2020-04-05T12:18:52.137+00:00

I have created two columns. first column is filename and second column is progress. I want to show reading progress of every files into second column (progress) one by one.

                    string path = "C:/Users/Nitesh/Documents/New folder (2)";

        DirectoryInfo dir = new DirectoryInfo(path);
        foreach (var file in dir.GetFiles())
        {
            FileStream fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite);
            long filesize = fs.Length;
            byte[] data = new byte[fs.Length];
            int offset = 0;

            while (filesize > 0)
            {
                int read = fs.Read(data, offset, (int)filesize);
                filesize -= read;
                offset += read;
                int pro = read * 100 / (int)file.Length;

                //dataGridView1.Rows.Add(new object[] { file.Name,  file.Length, pro});
            }
        }
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,515 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,056 Reputation points
    2020-04-05T13:08:10.537+00:00

    Hi, try following demo:

    XAML:

    <Window x:Class="WpfApp1.Window20"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp20"
            mc:Ignorable="d"
            Title="Window20" Height="450" Width="800">
      <Window.DataContext>
        <local:ViewModel/>
      </Window.DataContext>
        <Grid>
        <DataGrid ItemsSource="{Binding View}"/>
      </Grid>
    </Window>
    

    And ViewModel:

    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.IO;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Data;
    
    namespace WpfApp20
    {
      public class ViewModel
      {
        public ViewModel()
        {
          cvs.Source = col;
          Task.Factory.StartNew(new Action(loadInfo));
        }
        public ICollectionView View { get => cvs.View; }
    
        CollectionViewSource cvs = new CollectionViewSource();
        ObservableCollection<Data> col = new ObservableCollection<Data>();
    
        void loadInfo()
        {
          string dirPath = @"C:/temp";
          DirectoryInfo dir = new DirectoryInfo(dirPath);
          foreach (var file in dir.GetFiles())
          {
            try
            {
              FileStream fs = File.Open(file.FullName, FileMode.Open, FileAccess.ReadWrite);
              long filesize = fs.Length;
              byte[] data = new byte[fs.Length];
              int offset = 0;
              while (filesize > 0)
              {
                int read = fs.Read(data, offset, (int)filesize);
                filesize -= read;
                offset += read;
                int pro = read * 100 / (int)file.Length;
                Application.Current.Dispatcher.Invoke(() =>
                  col.Add(new Data() { Name = file.Name, Length = file.Length, Pro = pro }));
              }
            }
            catch { }
          }
        }
      }
    
      public class Data
      {
        public string Name { get; set; }
        public long Length { get; set; }
        public int Pro { get; set; }
      }
    }
    
    0 comments No comments