-4

I need to make a simple windows form application in C#, that when I type something in a textbox, the program will search for the words in a path. For example, C:\Users\John\Desktop using the "text" in the textbox as the search key.

Example

text in textbox = "room"

The program will search for the "room" on the desktop.

Possible output : (room202.swf) any file extension as long as the search key is on the name.

I hope I made myself clear.. if you have questions regarding this feel free to ask.

Brian
  • 5,069
  • 7
  • 37
  • 47
Jurel Jacinto
  • 129
  • 1
  • 10
  • Just to be clear, by 'desktop' do you mean the actual desktop or the file system as well? – Brian Jan 10 '13 at 16:31
  • My question to you is: what have you tried? – Seany84 Jan 10 '13 at 16:31
  • And I need a Porsche, can you help me ? :) – xlecoustillier Jan 10 '13 at 16:31
  • [How do I search for a list of files using wildcard](http://stackoverflow.com/questions/1584711/how-do-i-search-for-a-list-of-files-using-wildcard) & [How to get a path to the desktop for current user in C#?](http://stackoverflow.com/questions/634142/how-to-get-a-path-to-the-desktop-for-current-user-in-c) – Alex K. Jan 10 '13 at 16:32

4 Answers4

1

This code contains some of the basic functionality that you can use to search your directory

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

for more information, read up on the System.IO Namespace

1
string[] files = Directory.GetFiles(@"C:\Users\John\Desktop", "*" + textBox1.Text + "*");
foreach(string file in files)
{
    // Output file to user somewhere
}
Bali C
  • 30,582
  • 35
  • 123
  • 152
0

Try the following

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
        }
    }
  1. Here Environment.GetFolderPath(Environment.SpecialFolder.Desktop); will give you the path for desktop.
  2. Use this to get all file paths in the desktop Directory.GetFiles(path ,"*.*", SearchOption.AllDirectories);
Wolf
  • 2,150
  • 1
  • 15
  • 11
0

Below should work. It enumerates that path. Be sure to have a multiline textbox called txtOutput, and a txtSearch named control. You can put this in a button click or where ever.

 txtOutput.Text = "";

 foreach(string file in Directory.GetFiles("c:\\path"))
    if(Path.GetFileName(file).Contains(txtSearch.Text))
        txtOutput.Text += txtOutput.Text + file + ", ";
apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
  • thanks a lot! this helps me.. but can i ask 1 more question ? how can i access the searched file .. if its a video, or a music .. can i play it on the form1 i make? thanks again! – Jurel Jacinto Jan 10 '13 at 17:04
  • You can indeed just use Process.Start(filename). This is specified in the docs for Process.Start: http://msdn.microsoft.com/en-us/library/53ezey2s.aspx – apollosoftware.org Jan 10 '13 at 21:31