簡體   English   中英

ZipArchive搜索到列表視圖C#

[英]ZipArchive search to listview C#

我是C#的新手,正在嘗試將搜索結果從文本框顯示到button_click上的列表視圖。

這是我的代碼:

private void button1_Click(object sender, EventArgs e)
{
    listView1.Items.Clear();
    listView1.Refresh();
    string[] filePaths = Directory.GetFiles(@textBox1.Text, "*.zip");
    string txtSearch = textBox2.Text;

    foreach (var filePath in filePaths)
    {
        using (ZipArchive archive = ZipFile.OpenRead(filePath))
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                var position = entry.Name.IndexOf(
                    txtSearch,                                             
                    StringComparison.InvariantCultureIgnoreCase);

                if (position > -1)
                {
                    listView1.Items.Add(entry.Name);
                }
                else
                {
                    MessageBox.Show(
                        "FILE NOT FOUND", 
                        "ERROR", 
                        MessageBoxButtons.OK, 
                        MessageBoxIcon.Error);
                }
            }
        }
    }
}'

如果其單個文件夾包含其他檔案,但具有以下文件夾結構,則此代碼有效:folderA(包含):FolderA1,FolderA2。 FloderA1(包含):、 FolderA1.1,FolderA1.2,FolderA1.3和FolderA1.4 FloderA1.1至1.4(每個包含):、文件夾和大量歸檔文件(.zip和.rar)FloderA2(包含):FolderA2 .1,FolderA2.2和FolderA2.3。 FloderA2.1至2.3(每個包含):大量存檔(.zip和.rar)

我如何使用此代碼搜索具有特定擴展名的文件以列出,即使其中任何folder = filepath。

提前致謝。

看,我點擊了4次,因為發生了4次“找不到文件”,並且每次都顯示一個消息框。 我認為您應該在代碼中進行一些更改:

foreach (var filePath in filePaths)
    {
        int count=0;
        using (ZipArchive archive = ZipFile.OpenRead(filePath))
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                var position = entry.Name.IndexOf(txtSearch, StringComparison.InvariantCultureIgnoreCase);
                if (position > -1)
                {
                    listView1.Items.Add(entry.Name);
                    count++;
                }

            }
            if(count==0)
            {
                 MessageBox.Show("FILE "+filepath+ "NOT FOUND", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

更新

要獲取文件夾及其子文件夾中的所有文件,請閱讀this和this以及thisthis

被告知4次的原因可能是因為您正在加載的Zip文件中有4個項目與txtSearch條件不匹配。 由於在內部foreach循環中具有消息框,因此每次在ZipArchiveEntry找不到文件時,都會顯示該消息框。

您可以將消息框移到外部循環,以便每個.zip文件僅顯示一次。 在使用它時,可以使用Lambda簡化代碼,而無需第二個foreach循環。 Where子句將過濾出ZipArchive.Entries集合,然后通過選擇.Select集合中每個項目的Name屬性,將結果集轉換為字符串集合。

private void button1_Click(object sender, EventArgs e)
{
    listView1.Items.Clear();
    listView1.Refresh();
    string[] filePaths = Directory.GetFiles(@textBox1.Text, "*.zip");
    string txtSearch = textBox2.Text;
    var foundEntries = new List<string>();

    foreach (var filePath in filePaths)
    {
        using (ZipArchive archive = ZipFile.OpenRead(filePath))
        {
            foundEntries = archive.Entries
                .Where(e => e.Name.IndexOf(txtSearch, StringComparison.InvariantCultureIgnoreCase) >= 0)
                .Select(e => e.Name);
            if (foundEntries.Any())
            {
                listView1.Items.AddRange(foundEntries);
            }
            else
            {
                MessageBox.Show("FILE NOT FOUND", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        foundEntries.Clear();
    }
}

如果名稱不包含指定的字符串,則將為每個zip文件中的每個存檔顯示該MessageBox。 我猜您希望只顯示一次該消息,以防所有zip文件都不包含任何名稱包含指定字符串的文件。

private void button1_Click(object sender, EventArgs e)
{
    listView1.Items.Clear();
    listView1.Refresh();
    string[] filePaths = Directory.GetFiles(@textBox1.Text, "*.zip");
    string txtSearch = textBox2.Text;

    bool found = false;
    foreach (var filePath in filePaths)
    {
        using (ZipArchive archive = ZipFile.OpenRead(filePath))
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                var position = entry.Name.IndexOf(txtSearch, StringComparison.InvariantCultureIgnoreCase);
                if (position > -1)
                {
                    found = true;
                    listView1.Items.Add(entry.Name);
                    break;      // Comment out this line if you want more results from a single zip file
                }
            }
        }
    }
    if (!found)
    {
        MessageBox.Show("File not found", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

暫無
暫無

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

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