wizkid2332
asked on
reading through directories and files in .NET
I have a program that is searching through a specified directory and its subdirectories.
I want to list for this directory and each subdirectory the names of all files that
contain a specific word, lets say for example: "duck". I will then add that file to a textbox display.
The code snippet I have, I thought should work...but I am not reading in any files. Is it how I am cheching for 'duck"? if (file.Contains("duck") )
Thanks.
I want to list for this directory and each subdirectory the names of all files that
contain a specific word, lets say for example: "duck". I will then add that file to a textbox display.
The code snippet I have, I thought should work...but I am not reading in any files. Is it how I am cheching for 'duck"? if (file.Contains("duck") )
Thanks.
private void listFiles(string a_directory)
{
// get a list of all the files directories within the directory.
string[] files = Directory.GetFiles(a_directory);
string[] directories = Directory.GetDirectories(a_directory);
foreach (string file in files)
{
// display only files that have gteater assecced time
if (file.Contains("duck") )
{
txt_results.Text += file + "\r\n";
}
}
// go through all the directories and do the same thing.
foreach (string dir in directories)
{
if (dir.Contains("duck"))
{
txt_results.Text += dir + "\r\n";
}
// call recursivly to hit all directories
listFiles(dir);
}
}
I hope I'm not asking stupid or insulting questions, but I just want to be sure I understand what you are trying to accomplish.
- Is this being called from a WinForms application or a Web app? If it is being called from a web app, I'm just confirming that you know that the directory traversal will be done on the server, not on the client (i.e. browser). So, if the server has no files with that name, you will get an empty list.
I ran your function "as-is" within a WinForms app, and it worked fine...even with some mixed case file names.
Have you tried matching for a different pattern in the filename? Does this work?
- Is this being called from a WinForms application or a Web app? If it is being called from a web app, I'm just confirming that you know that the directory traversal will be done on the server, not on the client (i.e. browser). So, if the server has no files with that name, you will get an empty list.
I ran your function "as-is" within a WinForms app, and it worked fine...even with some mixed case file names.
Have you tried matching for a different pattern in the filename? Does this work?
Ooops...my other question was to confirm that you were looking to match the filename not the file contents. It looks like you are wanting to match the contents.
Are you searching text files, binary files or both?
Are you searching text files, binary files or both?
ASKER
I am searching text files. It is a WinForms application. thanks!
ASKER
but--on my second post, I am trying to search the contents, but I am not getting any results in my display...
Change the following
if (wrd == "duck")
To something like this:
if(wrd.Contains("duck"))
if (wrd == "duck")
To something like this:
if(wrd.Contains("duck"))
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Open in new window