|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Directory.GetFiles()Hi
I am using Directory.GetFiles(DirName, "*.tif"); to get all tif files in the directory. But it'll also give the other files i.e. *.tif1,*.tif_old,*.tif2. Please suggest a way to get *.tif files only. Regards Sameer Gupta C# Designer & Developer Siemens UK Sameer Gupta wrote:
> Hi You'll have to filter them yourself - the behavior you describe is exactly > > I am using > > Directory.GetFiles(DirName, "*.tif"); > > to get all tif files in the directory. But it'll also give the other > files i.e. *.tif1,*.tif_old,*.tif2. > > Please suggest a way to get *.tif files only. what you'd see if you did 'dir *.tif' at a command prompt. (It comes from the fact that the OS is matching the wildcard against both the long and the short filename, and the short filename for all of those files will end in ..tif). You could also use my FileSystemEnumerator class, which uses a regex to match wildcards instead of the OS pattern matching. See http://www.codeproject.com/cs/files/FileSystemEnumerator.asp -cd That's the intended behavior (see the Note in the Remarks section for the
method http://msdn2.microsoft.com/en-us/library/wz42302f.aspx), albeit questionable. Since you're probably going to process the list of files somehow anyway you can add some logic there, or write a helper method. using System; using System.Collections.Generic; using System.Text; using System.IO; namespace ConsoleApplication1 { class Program { static void Main( string[] args ) { string[] files = GetFilesWithExactExtension( ".", "*.tif" ); foreach( string file in files ) { Console.WriteLine( file ); } Console.Write( "Press any key to continue . . . " ); Console.ReadKey(); } static string[] GetFilesWithExactExtension( string path, string searchPattern ) { string[] candidates = Directory.GetFiles( path, searchPattern ); List<string> files = new List<string>(); foreach( string candidate in candidates ) { if( Path.GetExtension( candidate ) == searchPattern.Substring( searchPattern.IndexOf( "." ) ) ) { files.Add( candidate ); } } return files.ToArray(); } } } Show quote "Sameer Gupta" <SameerGu***@discussions.microsoft.com> wrote in message news:6F5AFAE6-CE31-489E-8B1B-100964CADD02@microsoft.com... > Hi > > I am using > > Directory.GetFiles(DirName, "*.tif"); > > to get all tif files in the directory. But it'll also give the other files > i.e. *.tif1,*.tif_old,*.tif2. > > Please suggest a way to get *.tif files only. > > Regards > Sameer Gupta > C# Designer & Developer > Siemens > UK > > |
|||||||||||||||||||||||