简体   繁体   English

ZipArchive搜索到列表视图C#

[英]ZipArchive search to listview C#

I'm new to C# I'm trying to display search result from textbox to a listview on button_click. 我是C#的新手,正在尝试将搜索结果从文本框显示到button_click上的列表视图。

Here is my code: 这是我的代码:

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);
                }
            }
        }
    }
}'

This code works if its a single folder contain other archives but with this folder structure: folderA(contains): FolderA1, FolderA2. 如果其单个文件夹包含其他档案,但具有以下文件夹结构,则此代码有效:folderA(包含):FolderA1,FolderA2。 FloderA1(contains): , FolderA1.1, FolderA1.2, FolderA1.3 and FolderA1.4 FloderA1.1 to 1.4(each contains): , Folder and lot of archives(.zip and .rar) FloderA2(contains): FolderA2.1, FolderA2.2 and FolderA2.3. FloderA1(包含):、 FolderA1.1,FolderA1.2,FolderA1.3和FolderA1.4 FloderA1.1至1.4(每个包含):、文件夹和大量归档文件(.zip和.rar)FloderA2(包含):FolderA2 .1,FolderA2.2和FolderA2.3。 FloderA2.1 to 2.3(each contains): lot of archives(.zip and .rar) FloderA2.1至2.3(每个包含):大量存档(.zip和.rar)

how do i use this code to search for files with particular extension to list even when any of the folders=filepath. 我如何使用此代码搜索具有特定扩展名的文件以列出,即使其中任何folder = filepath。

thanks in advance. 提前致谢。

Look, I took you 4 clicks because there're 4 times of "File not found" occurred, and it show a Messagebox for each time. 看,我点击了4次,因为发生了4次“找不到文件”,并且每次都显示一个消息框。 I think you should change a bit in your code: 我认为您应该在代码中进行一些更改:

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);
            }
        }
    }

Update 更新

To get all the files in a folder and its subfolders, read this and this and this and this . 要获取文件夹及其子文件夹中的所有文件,请阅读this和this以及thisthis

The reason you are getting told 4 times, is presumably because there are 4 items in the Zip file you are loading that do not match the txtSearch criteria. 被告知4次的原因可能是因为您正在加载的Zip文件中有4个项目与txtSearch条件不匹配。 Since you have the message box in the inner foreach loop, each time a file is not found within the ZipArchiveEntry , the message box will be displayed. 由于在内部foreach循环中具有消息框,因此每次在ZipArchiveEntry找不到文件时,都会显示该消息框。

You could move the message box to the outer loop, so that it is displayed only once per .zip file. 您可以将消息框移到外部循环,以便每个.zip文件仅显示一次。 While you're at it, you can use a Lambda to simplify the code, removing the need for a secondary foreach loop. 在使用它时,可以使用Lambda简化代码,而无需第二个foreach循环。 The Where clause will filter out the ZipArchive.Entries collection, and then transform the result set to a collection of strings by .Select 'ing the Name property of each item in the collection. 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();
    }
}

That MessageBox will be shown for each archive inside each zip file if the name does not contains the specified string. 如果名称不包含指定的字符串,则将为每个zip文件中的每个存档显示该MessageBox。 I guess you would prefer to show the message just one time, in case none of the zip files contains any file with a name containing the specified string. 我猜您希望只显示一次该消息,以防所有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