46,300 questions
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; }
}
}