Connecting Tech Pros Worldwide Forums | Help | Site Map

Directory.GetFiles() dosn't accept *.txt as search pattern

ALI-R
Guest
 
Posts: n/a
#1: Nov 16 '05
I am using the follwoing code to get all files which have txt as an
extension but I get an error that your search pattern is not correct.it
seems this fuction dosn't accept "*.txt" as search pattern.

Is there somebody can help me?

using System;
using System.IO;

class Test
{
public static void Main()
{
try
{

string[] dirs = Directory.GetFiles(@"c:\", "*.txt");
Console.WriteLine("The number of files starting with c is {0}.",
dirs.Length);
foreach (string dir in dirs)
{
Console.WriteLine(dir);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}



Chris R. Timmons
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Directory.GetFiles() dosn't accept *.txt as search pattern


"ALI-R" <ali@microsoft.com> wrote in
news:uUhAELhwEHA.1404@TK2MSFTNGP11.phx.gbl:
[color=blue]
> I am using the follwoing code to get all files which have txt as
> an extension but I get an error that your search pattern is not
> correct.it seems this fuction dosn't accept "*.txt" as search
> pattern.
>
> Is there somebody can help me?
>
> using System;
> using System.IO;
>
> class Test
> {
> public static void Main()
> {
> try
> {
>
> string[] dirs = Directory.GetFiles(@"c:\", "*.txt");
> Console.WriteLine("The number of files starting with
> c is {0}.",
> dirs.Length);
> foreach (string dir in dirs)
> {
> Console.WriteLine(dir);
> }
> }
> catch (Exception e)
> {
> Console.WriteLine("The process failed: {0}",
> e.ToString());
> }
> }
> }[/color]

Your code works on my machine.

What error message are you getting? Or is Directory.GetFiles simply
not returning any files?

(Realize that Directory.GetFiles is not recursive. It only looks for
files in the specified folder, and will not search any child
folders).

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
ALI-R
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Directory.GetFiles() dosn't accept *.txt as search pattern


thank you very much .I made a mistake passing the right path to it.it is
working fine now

I appreciate your help
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xns9596DB379C4A9crtimmonscrtimmonsin@207.46.2 48.16...[color=blue]
> "ALI-R" <ali@microsoft.com> wrote in
> news:uUhAELhwEHA.1404@TK2MSFTNGP11.phx.gbl:
>[color=green]
> > I am using the follwoing code to get all files which have txt as
> > an extension but I get an error that your search pattern is not
> > correct.it seems this fuction dosn't accept "*.txt" as search
> > pattern.
> >
> > Is there somebody can help me?
> >
> > using System;
> > using System.IO;
> >
> > class Test
> > {
> > public static void Main()
> > {
> > try
> > {
> >
> > string[] dirs = Directory.GetFiles(@"c:\", "*.txt");
> > Console.WriteLine("The number of files starting with
> > c is {0}.",
> > dirs.Length);
> > foreach (string dir in dirs)
> > {
> > Console.WriteLine(dir);
> > }
> > }
> > catch (Exception e)
> > {
> > Console.WriteLine("The process failed: {0}",
> > e.ToString());
> > }
> > }
> > }[/color]
>
> Your code works on my machine.
>
> What error message are you getting? Or is Directory.GetFiles simply
> not returning any files?
>
> (Realize that Directory.GetFiles is not recursive. It only looks for
> files in the specified folder, and will not search any child
> folders).
>
> --
> Hope this helps.
>
> Chris.
> -------------
> C.R. Timmons Consulting, Inc.
> http://www.crtimmonsinc.com/[/color]


Closed Thread