C # 032 combat: file read and write files to read and display progress
Before the discovery of a problem when writing the console program transfer Fanuc, when I transfer files using Python call interface, you do not always know how many documents to read, how long before the completion of the reading, began to engage in a program always thought hung up, for easy viewing transfer progress, today we make a progress bar reading and writing files, here is a dynamic presentation chart:
Tools / materials
-
C#
Method / Step
-
1
First, we define the source file path and get to the file name, and then develop a storage path and get the file path name, which was comparing the extracted file name, if it already exists we will remove it out.
string filepath = @"E:\Adobe Photoshop CS6.zip";
string filename = Path.GetFileName(filepath);
string saveDir = @"E:\UG";
string savePath = saveDir +"\\"+ filename;
DirectoryInfo sp = new DirectoryInfo (This means);
FileSystemInfo[] fi = sp.GetFileSystemInfos();
foreach (FileSystemInfo i in fi)
{
if (i.Name == filename)
{
i.Delete();
}
}
-
2
Here we begin to read and write files define the flow, and acquires the file size, we define the following variables to a few read and record the position of each length, and the remaining reading size read.
FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate,FileAccess.Read);
FileStream wr = new FileStream(savePath,FileMode.OpenOrCreate,FileAccess.Write);
long count = fs.Length; // file length
int start = 0; // start reading
int num = 0; // length of each reading
long end = count; // read the remaining length
double prePercent = 0; // progress bar
-
3
Now, we'll draw a progress bar background, we set the length of the progress bar 50, the color is green, you can print directly fill the background space.
ConsoleColor colorBack = Console.BackgroundColor;
ConsoleColor colorFore = Console.ForegroundColor;
if (count > 0)
{
// Draw the background interface read the article
Console.WriteLine("********************* Loading *********************");
Console.BackgroundColor = ConsoleColor.DarkCyan; // set the background color
for (int i = 0; ++i <= 50; )
{
Console.Write ( ""); // print space
}
Console.WriteLine(" ");
Console.BackgroundColor = colorBack; // restore the background color
Console.WriteLine("0%");
Console.WriteLine("***************************************************");
}
-
4
Then we start to read and write functions, access beginning of the file, it is determined that the remaining file size, if a lack of bytes of space, the num equal to the actual size of the remaining space, otherwise the size of bytes per transfer until the end of the remaining size 0 Meanwhile, we write to the file segments written to the specified file directory.
while (end > 0)
{
fs.Position = start;
if (end < maxlength)
num = fs.Read (bytes, 0, Convert.ToInt32 (end)); // read the file
else
num = fs.Read (bytes, 0, maxlength); // read the file
wr.Write (bytes, 0, num); // write to file
if (num == 0)
break;
start += num;
end -= num;
}
fs.Close();
wr.Close();
fs.Dispose();
Console.WriteLine(" Finished Reading !");
Console.ReadLine();
}
-
5
This is what we do not see the progress of reading and writing, because we have not read each time the data were presented from the above code can be seen every time we record the size of the transfer, the number has been transmitted, how much we have left now know, then we use the file size and the total size of the transfer of value has been calculated to obtain the proportion of the file every time we transfer, and draw it into a progress bar is displayed.
-
6
We will calculate the value of the transmission, and then multiplied by a fractional obtained in terms of 100 percent and rounded Math.Ceiling (percent * 100), the next step is to update the progress bar, like the above, we define the background color, cursor movement as we move to the proportion of transmission.
-
7
Finally, do not forget to close the file stream, or an error occurs when a file is written (less bytes of the contents of a file leads to error, can not be used), remember.