hi itsme
hi itsme

Reputation: 445

StreamReader path using * - Illegal Characters in Path

I need to use StreamReader like this

StreamReader fichRecheio = new StreamReader(@"G:\C#Projects\folder1\*.tl2",System.Text.Encoding.Default);  

But it gives me an error : Unhandled Exception - Illegal Characters in Path
If i put the name of the file instead of the * works ok.
There is only one file in that directory.I just don´t know the name, just extension.
Thanks

Upvotes: 2

Views: 4320

Answers (4)

BLoB
BLoB

Reputation: 9725

If I've understood your question correctly why not do it like this:

Use Directory.EnumerateFiles() method instead.

var sourceDirectory = @"G:\C#Projects\folder1\"
var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.tl2");

foreach (string currentFile in txtFiles)
{
   StreamReader leFiles = new StreamReader(currentFile, System.Text.Encoding.Default);  
}

Upvotes: 4

Jason Loki Smith
Jason Loki Smith

Reputation: 438

if you are trying use a wildcard to search for files with a specific pattern in the name, use System.IO.Directory.GetFiles("*.tl2") which returns an array of paths that you can enumerate through.

string[] FilePaths = System.IO.Directory.GetFiles("*.tl2");

foreach (file in FilePaths)
{
//process the file
}

Upvotes: 0

M.S.
M.S.

Reputation: 4423

Because you are trying to open multiple files with a single stream, that's not possible.

You have to specify the exact file name with which you are trying to connect reader as:

 StreamReader fichRecheio = new StreamReader(@"G:\C#Projects\folder1\test.tl2",System.Text.Encoding.Default);

Upvotes: 0

Lloyd
Lloyd

Reputation: 29668

Of course, * is used for wildcard matching of multiple files. Specify a single filename:

StreamReader fichRecheio = new StreamReader(@"G:\C#Projects\folder1\example.tl2",System.Text.Encoding.Default);

Upvotes: 2

Related Questions