How to use folder path to find pathnames of certain files  
Author Message
Tareq Ismail





PostPosted: .NET Base Class Library, How to use folder path to find pathnames of certain files Top

Hello,

For a windows application I have created I have used the FolderBrowserDialog to retreive the pathname of a desired folder. Now that I have that pathname I wish to look in this folder for all files with a certain extensions (.html and .htm to be specific), after I find each file I would like to store the file's path name in an array of strings. Also I wish not to look into subfolders, but just at the root of the folder. Is this possible

I have looked all over the web and just cannot find anything that will help. I have looked at filestreams and have not gotten anywhere either. Can someone please help me with this matter

Thanks,

Tareq Ismail



.NET Development27  
 
 
ahmedilyas





PostPosted: .NET Base Class Library, How to use folder path to find pathnames of certain files Top

sure its possible.

you need to use the System.IO.Directory.GetFiles method which gives you a list of files found with the matching extension in the folder specified, including to search into subfolders

http://msdn2.microsoft.com/en-US/library/4cyf24ss.aspx

does this help

so an example....

 

string[] files = System.IO.Directory.GetFiles("path", "*.htm", SearchOption.AllDirectories);

 

this will return you a list of files in a string[] array, including fill ful path of files in the specified path which contain .htm extensions in all directories from the path supplied including the subdirectories. you could also tell it to only search the root folder by changing "AllDirectories" to "TopDirectoryOnly"

is this what you are after



 
 
Tareq Ismail





PostPosted: .NET Base Class Library, How to use folder path to find pathnames of certain files Top

Hello,

Thanks ahmedilyas for such a quick reply, I apperciate it.
That is generally what I am looking for but to be more specific I want only the files at the root of the folder. I looked it up on msdn and found that an option called TopDirectoryOnly will allow me to do so. However, when compiling it showed up with an error. My line of code is the following and I have obvuisly included System.IO,

string[] files = System.IO.Directory.GetFiles(FolderPath, "*.htm", SearchOption.TopDirectoryOnly);

To describe my code, FolderPath is a string with my desired folder's path. The error itself reads as:

The type or namespace name 'SearchOption' could not be found (are you missing a using directive or an assembly reference )

Does anyone know what this means Or what I'm doing wrong
Oh ahmedilyas, I noticed in your code you wrote :"*.htm* , I assumed that you meant to write "*.htm" ( so a quote rather than an asterix at the end) . I tryed running my code with both and neither of them worked. Can anyone help

Thanks,

Tareq Ismail



 
 
ahmedilyas





PostPosted: .NET Base Class Library, How to use folder path to find pathnames of certain files Top

import the System.IO namespace - add:

using System.IO;

at the top of your class file where the using decleratives are. you should be ok :-)

and yes you are correct, it was a typo from myself which I corrected as soon as I spotted it - it should have been a closed quote mark rather than an extra astrix!



 
 
Tareq Ismail





PostPosted: .NET Base Class Library, How to use folder path to find pathnames of certain files Top

Hi Ahmedilyas,

Yes I have already added that at the top, I actually mentioned it on my previous post. It still does not work, any ideas
Thanks again for the help,

Tareq Ismail


 
 
ahmedilyas





PostPosted: .NET Base Class Library, How to use folder path to find pathnames of certain files Top

interesting, i cant understand why.

This works for me...

 

string[] files = System.IO.Directory.GetFiles("PathHere", "*.htm", System.IO.SearchOption.TopDirectoryOnly);

 

actually I just added the System.IO there for you to explicitly "map" to it for you. Does this not work



 
 
Tareq Ismail





PostPosted: .NET Base Class Library, How to use folder path to find pathnames of certain files Top

Nope, still same error message.

Hmm maybe it's my version I am using Visual Studio 7.1.3088, with .NET framework 1.1 , do you think that makes a difference


 
 
ahmedilyas





PostPosted: .NET Base Class Library, How to use folder path to find pathnames of certain files Top

yes! :-) would be nice if you stated that originally :-P

instead you can do this....

string[] files = System.IO.Directory.GetFiles("PathHere", "*.htm");

does this work for you



 
 
Tareq Ismail





PostPosted: .NET Base Class Library, How to use folder path to find pathnames of certain files Top

It worked!! Thanks a lot ahmedilyas! Really apperciate it!