I’m developing a winform application that will search for a string in the filenames of a specified source directory.. The problem is I need to access the file ..
Example: The search result is a .flv or .swf — When the search is finished .. the result should be accessible.
This is what I have so far ..
private void button1_Click(object sender, EventArgs e)
{
txtOutput.Text = "";
foreach (string file in Directory.GetFiles("C:\\Users\\John\\Desktop\\Sample"))
if (Path.GetFileName(file).Contains(txtSearch.Text))
txtOutput.Text += txtOutput.Text + file + ", ";
}
With this code I was able to search the file but it is not accessible .. also the output of the search came with the path of the file .. ( something like this c:\users\John\desktop\sample\Filename.swf ) I need a file name only, not the whole path..
I’m using a Multiline Textbox for the output, should I use something else? .. If you have a better suggestion please help me.
If you are looking for files with certain extension, then use search pattern of
EnumerateFilesor Directory.GetFiles methods. Also use Path.GetFileName to get file name from file path:Your
txtSearch.Textsuppose to have extension of searched files (i.e..swfor.flv). Thus search pattern will be*.swfor*.flv.So, if your search textbox has text
.swfand there are two sfw files in your sample directory, then you will get output asfile1.swf, file2.swf.If you want to search any substring in file name:
And instead of multiline textbox, use listbox for displaying files:
UPDATE: opening files