Not Monitored
Tag not monitored by Microsoft.
42,024 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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});
}
}
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; }
}
}