簡體   English   中英

在讀取多個文本文件時如何顯示進度欄

[英]How to show progress bar while reading multiple text files

我正在逐個子文件夾中逐行讀取文本文件。 這意味着一旦完成讀取包含一個子文件夾的所有文本文件,然后開始從下一個子文件夾讀取文件。 您可以從我的代碼中了解。 一切工作正常,我要顯示的是從第一個文件開始讀取時的進度條,然后在執行完成后隱藏進度條。 任何幫助,我們將不勝感激。 下面是我的代碼:

private void browse_Click(object sender, EventArgs e)
    {
        try
        {
            string newFileName1 = "";
            string newFileName2 = "";
            week = textBox2.Text;
            if (week == null || week == "")
            {
                MessageBox.Show("Week cannot be null.");
                return;
            }


            DialogResult result = folderBrowserDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {
                DateTime starttime = DateTime.Now;

                string folderPath = Path.GetDirectoryName(folderBrowserDialog1.SelectedPath);
                string folderName = Path.GetFileName(folderPath);
                DirectoryInfo dInfo = new DirectoryInfo(folderPath);

                foreach (DirectoryInfo folder in dInfo.GetDirectories())
                {
                    newFileName1 = "Files_with_dates_mismatching_the_respective_week_" + folder.Name + ".txt";
                    newFileName2 = "Files_with_wrong_date_format_" + folder.Name + ".txt";

                    if (File.Exists(folderPath + "/" + newFileName1))
                    {
                        File.Delete(folderPath + "/" + newFileName1);
                    }

                    if (File.Exists(folderPath + "/" + newFileName2))
                    {
                        File.Delete(folderPath + "/" + newFileName2);
                    }

                    FileInfo[] folderFiles = folder.GetFiles();

                    if (folderFiles.Length != 0)
                    {
                        List<Task> tasks = new List<Task>();
                        foreach (var file in folderFiles)
                        {
                            var task = ReadFile(file.FullName, folderPath, folder.Name, week);
                            tasks.Add(task);
                        }

                        Task.WhenAll(tasks.ToArray());
                        DateTime stoptime = DateTime.Now;
                        TimeSpan totaltime = stoptime.Subtract(starttime);
                        label6.Text = Convert.ToString(totaltime);
                        textBox1.Text = folderPath;

                    }
                }
                DialogResult result2 = MessageBox.Show("Read the files successfully.", "Important message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        catch (Exception)
        {

            throw;
        }
    }

    public async Task ReadFile(string file, string folderPath, string folderName, string week)
    {
        int LineCount = 0;
        string fileName = Path.GetFileNameWithoutExtension(file);

        using (FileStream fs = File.Open(file, FileMode.Open))
        using (BufferedStream bs = new BufferedStream(fs))
        using (StreamReader sr = new StreamReader(bs))
        {
            for (int i = 0; i < 2; i++)
            {
                sr.ReadLine();
            }

            string oline;
            while ((oline = sr.ReadLine()) != null)
            {
                LineCount = ++LineCount;
                string[] eachLine = oline.Split(';');

                string date = eachLine[30].Substring(1).Substring(0, 10);

                DateTime dt;

                bool valid = DateTime.TryParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);

                if (!valid)
                {
                    StreamWriter sw = new StreamWriter(folderPath + "/" + "Files_with_wrong_date_format_" + folderName + ".txt", true);
                    await sw.WriteLineAsync(fileName + "  " + "--" + "  " + "Line number :" + " " + LineCount);
                    sw.Close();
                }
                else
                {
                    DateTime Date = DateTime.ParseExact(date, "d/M/yyyy", CultureInfo.InvariantCulture);

                    int calculatedWeek = new GregorianCalendar(GregorianCalendarTypes.Localized).GetWeekOfYear(Date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Saturday);

                    if (calculatedWeek == Convert.ToInt32(week))
                    {

                    }
                    else
                    {
                        StreamWriter sw = new StreamWriter(folderPath + "/" + "Files_with_dates_mismatching_the_respective_week_" + folderName + ".txt", true);
                        await sw.WriteLineAsync(fileName + "  " + "--" + "  " + "Line number :" + " " + LineCount);
                        sw.Close();
                    }
                }
            }
        }
    }

您要顯示每個文件本身還是所有文件的進度?

您可以通過以下方式獲取文件大小:

FileInfo fileInfo = new FileInfo(file);
long fileLength = fileInfo.Length;

將進度條的最小值設置為0,最大值設置為100。創建一個包含當前流位置的變量,然后使用以下命令更新進度條:

(int)(((decimal)currentStreamPosition / (decimal)fileLength)*(decimal)100);

您可以添加所有文件大小並顯示百分比,或者在完成讀取一個文件后將currentStreamPosition設置為零。

您必須遍歷所有需要讀取的文件,然后才能獲得確切的總文件大小。

通常,在類似情況下,最好盡可能顯示2個進度條-一個用於“總體進度”,另一個用於“當前”。 因此,您可以先計算所有子文件夾中的所有文件,以估計總體進度,然后在“當前進度”欄中顯示每個文件的讀數。

如果進度條應該只有一個-則只能顯示總體進度。 您可以使用Directory.GetFiles方法計算文件數。 請參閱以下鏈接https://msdn.microsoft.com/en-us/library/3hwtke0f.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM