Directory.GetFiles throw an exception

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi,

The following statement throws an exception when the
search pattern returns no files:

string[] files = Directory.GetFiles(directory,"*pl*.txt");

Is there any way to find out whether there isn't a match
so that I don't have to wrap the above code up in a try-
catch block?

John
 
John said:
The following statement throws an exception when the
search pattern returns no files:

string[] files = Directory.GetFiles(directory,"*pl*.txt");

Is there any way to find out whether there isn't a match
so that I don't have to wrap the above code up in a try-
catch block?

What's the exception?
 
IOException ("Files not found")
-----Original Message-----
John said:
The following statement throws an exception when the
search pattern returns no files:

string[] files = Directory.GetFiles (directory,"*pl*.txt");

Is there any way to find out whether there isn't a match
so that I don't have to wrap the above code up in a try-
catch block?

What's the exception?

--
Jon Skeet - <[email protected]>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.
 
John said:
IOException ("Files not found")

Hmm... I can't reproduce that at all. Using the following program:

using System;
using System.IO;

public class Test
{
static void Main(string[] args)
{
try
{
string[] files = Directory.GetFiles (args[0]);
Console.WriteLine (files.Length);
}
catch (Exception e)
{
Console.WriteLine (e.Message);
}
}
}

I get "0" printed if I give a valid directory with no matching files
in, or error messages such as:

Could not find a part of the path "c:\asd".
or
The directory name is invalid.

if I give the name of a non-existent directory or a file instead. Could
you give a bit more help to reproduce them problem? What happens with
the above program on your box?
 
Hi John,

That seemed to work fine with a simple test. Now I need
to try it with a directory and file search we originally
used, but this won't be until tomorrow, watch this space!

John
-----Original Message-----
John said:
IOException ("Files not found")

Hmm... I can't reproduce that at all. Using the following program:

using System;
using System.IO;

public class Test
{
static void Main(string[] args)
{
try
{
string[] files = Directory.GetFiles (args[0]);
Console.WriteLine (files.Length);
}
catch (Exception e)
{
Console.WriteLine (e.Message);
}
}
}

I get "0" printed if I give a valid directory with no matching files
in, or error messages such as:

Could not find a part of the path "c:\asd".
or
The directory name is invalid.

if I give the name of a non-existent directory or a file instead. Could
you give a bit more help to reproduce them problem? What happens with
the above program on your box?

--
Jon Skeet - <[email protected]>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.
 
Back
Top