簡體   English   中英

迭代文件結構時更新進度條

[英]Update progress bar while iterating file structure

我使用這個函數來搜索所選目錄中的所有exe文件:

public static IEnumerable<string> GetFiles(string root, string searchPattern)
{
    Stack<string> pending = new Stack<string>();
    pending.Push(root);
    while (pending.Count != 0)
    {
        var path = pending.Pop();
        string[] next = null;
        try
        {
            next = Directory.GetFiles(path, searchPattern);
        }
        catch { }
        if (next != null && next.Length != 0)
            foreach (var file in next) yield return file;
        try
        {
            next = Directory.GetDirectories(path);
            foreach (var subdir in next) pending.Push(subdir);
        }
        catch { }
    }
}

如何根據找到的文件數更新進度條狀態?

關鍵是你不知道你會發現的exe文件總數(也就是100%)所以基本上你不能渲染進度條! 對於這種任務,它更適合沙漏或大帳篷吧......

您可能希望搜索並最后將進度條設置為找到的文件數。

您可以指定一個計數器,將值a =分配給找到的文件#然后設置

progressBar.Maximum = a;

也許我在這里遺漏了一些東西,但為什么不將進度條的最大值分配給pending.Count並在每次處理文件時將進度條的值加1?

暫無
暫無

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

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