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

A

ALI-R

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());
}
}
}
 
C

Chris R. Timmons

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());
}
}
}

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).
 
A

ALI-R

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 said:
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());
}
}
}

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top