question

NiteshRathi-9709 avatar image
0 Votes"
NiteshRathi-9709 asked ·

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

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-supported
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Wpf doesn't have DataGridView control. If your project is more related about winform than wpf ,you can ask in this msdn forum WinFrom Forum


0 Votes 0 ·

1 Answer

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered ·

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; }
   }
 }
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.