Answer the question
In order to leave comments, you need to log in
How to organize the process when copying?
Hello!
I copy an archive from a network resource. How to reflect the process?
First I used this method:
...
int i = 0;
FileStream fin = new FileStream(source, FileMode.Open);
FileStream fout = new FileStream(destatntion, FileMode.Create);
progressBarCopy1.Maximum = Convert.ToInt32(fin.Length);
do
{
i = fin.ReadByte();
if (i != -1)
fout.WriteByte((byte)i);
//progressBarCopy1.Value = progressBarCopy1.Maximum - 1;
progressBarCopy1.PerformStep();
} while (i != -1);
fin.Close();
fout.Close();
return i;
....
int array_length = (int)Math.Pow(2, 19);
byte[] dataArray = new byte[array_length];
using (FileStream fsread = new FileStream
(source, FileMode.Open, FileAccess.Read, FileShare.None, array_length))
{
progressBarCopy1.Maximum = Convert.ToInt32(fsread.Length);
using (BinaryReader bwread = new BinaryReader(fsread))
{
using (FileStream fswrite = new FileStream
(destination, FileMode.Create, FileAccess.Write, FileShare.None, array_length))
{
using (BinaryWriter bwwrite = new BinaryWriter(fswrite))
{
for (; ; )
{
progressBarCopy1.PerformStep();
int read = bwread.Read(dataArray, 0, array_length);
if (0 == read)
break;
bwwrite.Write(dataArray, 0, read);
}
}
}
}
}
Answer the question
In order to leave comments, you need to log in
First, it's bad practice to perform long operations (greater than 100 milliseconds) on the UI thread. The application will hang until the file is copied. To avoid this, all disk operations must be asynchronous (FileStream.BeginRead/EndRead can be used instead of async/await).
Secondly, your buffer is too big. 2^19 is 512 kilobytes. You may be copying smaller files, so the entire copy is done in a single read operation. Try a buffer, say 16kb.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question