Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

Directory.GetFiles()

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sameer Gupta - 06 Oct 2006 13:22 GMT
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
Carl Daniel [VC++ MVP] - 06 Oct 2006 15:31 GMT
> Hi
>
[quoted text clipped - 6 lines]
>
> Please suggest a way to get *.tif files only.

You'll have to filter them yourself - the behavior you describe is exactly
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
Gabriele G. Ponti - 06 Oct 2006 15:54 GMT
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();
       }

   }
}

> Hi
>
[quoted text clipped - 12 lines]
> Siemens
> UK

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.