Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#使用text.box上的文本作为搜索键在c:\Users\John\Desktop上搜索?_C#_Search_Textbox - Fatal编程技术网

c#使用text.box上的文本作为搜索键在c:\Users\John\Desktop上搜索?

c#使用text.box上的文本作为搜索键在c:\Users\John\Desktop上搜索?,c#,search,textbox,C#,Search,Textbox,我需要用C#制作一个简单的windows窗体应用程序,当我在文本框中键入内容时,程序将在路径中搜索单词。例如,C:\Users\John\Desktop使用文本框中的“text”作为搜索键 范例 text in textbox = "room" 程序将在桌面上搜索“房间” 可能的输出:(room202.swf)任何文件扩展名,只要 搜索键位于名称上 我希望我说清楚了。。如果您对此有任何疑问,请随时提问。此代码包含一些可用于搜索目录的基本功能 static void Main(string[]

我需要用C#制作一个简单的windows窗体应用程序,当我在文本框中键入内容时,程序将在路径中搜索单词。例如,
C:\Users\John\Desktop
使用文本框中的“text”作为搜索键

范例

text in textbox = "room"
程序将在桌面上搜索“房间”

可能的输出:(room202.swf)任何文件扩展名,只要 搜索键位于名称上


我希望我说清楚了。。如果您对此有任何疑问,请随时提问。

此代码包含一些可用于搜索目录的基本功能

static void Main(string[] args)
{
    string [] fileNames = Directory.GetFiles(@"c:\path");

    foreach(string fn in fileNames)
    {
        if(Path.GetFileName(fn).Contains(textBox1.Text))
        {
            //do something with fn
        }
    }
}
有关详细信息,请阅读并尝试以下操作

string strToSearch "room";
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string[] files = Directory.GetFiles(path , "*.*", SearchOption.AllDirectories);

 foreach(string fl in files)
    {
        if(Path.GetFileName(fl).Contains(strToSearch))
        {
            // Do the magic here
        }
    }
  • 这里
    Environment.GetFolderPath(Environment.SpecialFolder.Desktop)将为您提供桌面的路径
  • 使用此选项可获取桌面
    目录.GetFiles(路径“***”,SearchOption.AllDirectories)中的所有文件路径

  • 下面的方法应该有效。它列举了这条道路。确保有一个名为txtOutput的多行文本框和一个名为control的txtSearch。你可以点击一个按钮或者任何地方

     txtOutput.Text = "";
    
     foreach(string file in Directory.GetFiles("c:\\path"))
        if(Path.GetFileName(file).Contains(txtSearch.Text))
            txtOutput.Text += txtOutput.Text + file + ", ";
    

    我想明确一点,“桌面”是指真正的桌面还是文件系统?我的问题是:你试过什么?我需要一辆保时捷,你能帮我吗?:)&对不起,我可以问一些问题吗,我如何在textbox1上搜索文本?我很抱歉问你。。谢谢您的帮助。@JurelJacinto您可以从大约anywhere@JurelJacinto我已经将答案编辑为使用
    textBox1
    非常感谢!这对我有帮助。。但我能再问一个问题吗?如何访问搜索到的文件。。如果是视频或音乐。。我可以在我制作的表格1上玩吗?再次感谢!您确实可以只使用Process.Start(文件名)。这在流程的文档中指定。开始:
     txtOutput.Text = "";
    
     foreach(string file in Directory.GetFiles("c:\\path"))
        if(Path.GetFileName(file).Contains(txtSearch.Text))
            txtOutput.Text += txtOutput.Text + file + ", ";