从VB.NET搜索文件

时间:2021-12-03 13:24:51

Given a filename, how do I efficiently search for that file on disk?

给定文件名,如何在磁盘上有效搜索该文件?

(Visual Studio 2005, i.e. .NET 2.0)

(Visual Studio 2005,即.NET 2.0)

2 个解决方案

#1


Your question is vague, you do not specify any programming language. So, you can do this using the command prompt:

您的问题很模糊,您没有指定任何编程语言。因此,您可以使用命令提示符执行此操作:

dir /s /b d:\<filename>

or use the above in a system call from whatever language you're using.

或者在您使用的任何语言的系统调用中使用上述内容。

In C/C++ or any other language that uses the native Win32 APIs use:

在C / C ++或使用本机Win32 API的任何其他语言中使用:

and recurse through any directories you encounter. In C#/VB/other .Net language, it's:

并通过您遇到的任何目录进行递归。在C#/ VB /其他.Net语言中,它是:

#2


If you want to implement the search mechanism, I'd start with something like this (C#)

如果你想实现搜索机制,我会从这样的事情开始(C#)

using System;
using System.Collections.Generic;
using System.IO;

namespace Samples.FileSearcher
{
    public delegate void FileFoundHandler(string fileName);
    public delegate void SearchStatChangeHandler( bool newStat);
    public class FileSearch
    {
        private bool _isSearching;
        private FileFoundHandler _fileFound;
        private SearchStatChangeHandler _searchStatusChanged;
        public bool IsSearching { get { return _isSearching; } }
        public event FileFoundHandler FileFound{add { _fileFound += value; }remove { _fileFound -= value; }}
        public event SearchStatChangeHandler SearchingStatusChanged { add { _searchStatusChanged += value; } remove { _searchStatusChanged -= value; } }

        public void Search(string rootFolder, string filePattern)
        {
            ChangeStat(true);
            Queue<string> folderList = new Queue<string>();
            folderList.Enqueue(rootFolder);

            while (folderList.Count > 0)
            {
                string currentFolder = folderList.Dequeue();
                foreach (string folder in Directory.GetDirectories(currentFolder))
                    folderList.Enqueue(folder);
                foreach (string foundFile in Directory.GetFiles(currentFolder, filePattern))
                    if (_fileFound != null)
                        _fileFound(foundFile);
            }
            ChangeStat(false);
        }
        private void ChangeStat(bool newStat)
        {
            _isSearching = newStat;
            if (_searchStatusChanged != null) _searchStatusChanged(_isSearching);
        }

    }
}

That's just a quick-class for doing it. You should implement the form using it, some error handling on the Search Method and probably some canceling flags so you won't keep searching forever when you already found what you wanted.

这只是一个快速的做法。您应该使用它来实现表单,对搜索方法进行一些错误处理,可能还有一些取消标记,这样当您已经找到想要的内容时,您将无法继续搜索。

I implemented my Form with something like this:

我用这样的方式实现了我的表单:

private void button1_Click(object sender, EventArgs e)
    {
        listView1.Items.Clear();
        Samples.FileSearcher.FileSearch searcher = new Samples.FileSearcher.FileSearch();
        searcher.FileFound += new FileFoundHandler(searcher_FileFound);
        searcher.Search(textBox1.Text, textBox2.Text);
    }

    void searcher_FileFound(string fileName)
    {
        listView1.Items.Add(fileName);
    }

If you have more specific doubts, please post them up and we'll try to look into it and do our best to help you out.

如果您有更具体的疑问,请将它们发布,我们会尽力调查,并尽力帮助您。

#1


Your question is vague, you do not specify any programming language. So, you can do this using the command prompt:

您的问题很模糊,您没有指定任何编程语言。因此,您可以使用命令提示符执行此操作:

dir /s /b d:\<filename>

or use the above in a system call from whatever language you're using.

或者在您使用的任何语言的系统调用中使用上述内容。

In C/C++ or any other language that uses the native Win32 APIs use:

在C / C ++或使用本机Win32 API的任何其他语言中使用:

and recurse through any directories you encounter. In C#/VB/other .Net language, it's:

并通过您遇到的任何目录进行递归。在C#/ VB /其他.Net语言中,它是:

#2


If you want to implement the search mechanism, I'd start with something like this (C#)

如果你想实现搜索机制,我会从这样的事情开始(C#)

using System;
using System.Collections.Generic;
using System.IO;

namespace Samples.FileSearcher
{
    public delegate void FileFoundHandler(string fileName);
    public delegate void SearchStatChangeHandler( bool newStat);
    public class FileSearch
    {
        private bool _isSearching;
        private FileFoundHandler _fileFound;
        private SearchStatChangeHandler _searchStatusChanged;
        public bool IsSearching { get { return _isSearching; } }
        public event FileFoundHandler FileFound{add { _fileFound += value; }remove { _fileFound -= value; }}
        public event SearchStatChangeHandler SearchingStatusChanged { add { _searchStatusChanged += value; } remove { _searchStatusChanged -= value; } }

        public void Search(string rootFolder, string filePattern)
        {
            ChangeStat(true);
            Queue<string> folderList = new Queue<string>();
            folderList.Enqueue(rootFolder);

            while (folderList.Count > 0)
            {
                string currentFolder = folderList.Dequeue();
                foreach (string folder in Directory.GetDirectories(currentFolder))
                    folderList.Enqueue(folder);
                foreach (string foundFile in Directory.GetFiles(currentFolder, filePattern))
                    if (_fileFound != null)
                        _fileFound(foundFile);
            }
            ChangeStat(false);
        }
        private void ChangeStat(bool newStat)
        {
            _isSearching = newStat;
            if (_searchStatusChanged != null) _searchStatusChanged(_isSearching);
        }

    }
}

That's just a quick-class for doing it. You should implement the form using it, some error handling on the Search Method and probably some canceling flags so you won't keep searching forever when you already found what you wanted.

这只是一个快速的做法。您应该使用它来实现表单,对搜索方法进行一些错误处理,可能还有一些取消标记,这样当您已经找到想要的内容时,您将无法继续搜索。

I implemented my Form with something like this:

我用这样的方式实现了我的表单:

private void button1_Click(object sender, EventArgs e)
    {
        listView1.Items.Clear();
        Samples.FileSearcher.FileSearch searcher = new Samples.FileSearcher.FileSearch();
        searcher.FileFound += new FileFoundHandler(searcher_FileFound);
        searcher.Search(textBox1.Text, textBox2.Text);
    }

    void searcher_FileFound(string fileName)
    {
        listView1.Items.Add(fileName);
    }

If you have more specific doubts, please post them up and we'll try to look into it and do our best to help you out.

如果您有更具体的疑问,请将它们发布,我们会尽力调查,并尽力帮助您。