Click here to Skip to main content
15,887,321 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

here is my code for search criteria;

C#
string[] filepaths = Directory.GetFiles(@"c:\data\", textBox1.Text + ".jpg", SearchOption.AllDirectories);


there is a textbox on my form. All i need is when i write something in textbox(like "test"), when i click on search button, see all "test.jpg" files in c:\data and its subdirectories in a windows folder.

should i use openfiledialog? how should i use it?
Posted

"should i use openfiledialog? how should i use it?"

No - the array of strings filepaths will contain a collection of full paths to the files: OpenFileDialog allows the user to select which file(s) he specifically wants to open. You can't provide it with a list of files for him to pick from.

You can't display a list of files from different folders in any "standard" control, or in a Windows Explorer window - they work on a single folder.
Instead, look at the TreeView Control[^] perhaps - it is designed for showing hierarchical data such as folder / file structures, or perhaps a ListView with thumbnails of each image would be better?
 
Share this answer
 
Comments
Member 10587827 16-Dec-14 14:28pm    
listview might be a good idea. is there any way that when i double click on something on listbox,(for example c:\data\test.jpg) can i open this file?
OriginalGriff 16-Dec-14 14:41pm    
Yes, just handle the Double click event on the cell, (or picture box if you are going to use those) and pass the path to the file to Process.Start
Member 10587827 17-Dec-14 5:16am    
string[] files = Directory.GetFiles(@"c:\data", textBox1.Text + ".jpg", SearchOption.AllDirectories);
foreach (string dosya in files)
{
string filename = Path.GetFileName(dosya);
ListViewItem item = new ListViewItem(filename);

listView1.Items.Add(item);

}



this is my code for listing searched files. But when i run this code, nothing will appear on my listview window.What can be the reason for it?
OriginalGriff 17-Dec-14 5:48am    
Well, it probably does: it adds a bunch of strings to the listview, which it will display - as strings.

So if it doesn't add anything you need to use the debugger to see exactly what the array of paths contains once GetFiles returns, or change your code to display images...
It depends on many factors...

If you would like to search only C:\data\ folder, you don't, but if you would like to provide functionality to change searched drive/folder you need to use FolderBrowserDialog[^].
 
Share this answer
 
Comments
Member 10587827 16-Dec-14 14:18pm    
i just want to search c:\data and its subfolders by using textbox(for filename).
Maciej Los 16-Dec-14 14:20pm    
And...
Does my answer not satisfied you?
Yes, you can use the OpenFileDialog to achieve this, and set the InitialDirectory to the directory that you first want to search for, but there isn't a "Name" specific search, only extension specific search can be made, like searching for an Office file or a Web document etc.

However, if I had to do this project. I wouldn't use OpenFileDialog, instead I would have made a use of System.IO namespace and enumerated among the directories and look for my file. Because once inside these System.IO namespace's File or Directory class I can check for each file, by their types, create time or their names etc.

Once I would have caught all of the files inside the directory, a simple name check like this

C#
if(Path.GetFileName(file) == nameToSearch) {
   // match found!
}


File must come from the directory's file collection.

This second method would have given me much control, but the one thing "open the files in Window explorer" would now lag, since we can't filter the files by their names unless we use a Windows based search. But this method would let us, create a simple UI to show all of the possible files, and directories to create in the UI for the user to navigate.
 
Share this answer
 
Comments
Member 10587827 16-Dec-14 14:23pm    
when i get files with your method, how can see these searched files in folder?
Afzaal Ahmad Zeeshan 16-Dec-14 14:25pm    
You will create your own UI to display the files, because you cannot start the Windows explorer to merge all those files into one directory.

However, if you copy/paste those files to a virtual directory and so on... long process!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900