user222427
user222427

Reputation:

C# opening each textfile and reading all text issue

{
        DirectoryInfo dinfo = new DirectoryInfo(@"C:\Documents and Settings\g\Desktop\123");
        FileInfo[] Files = dinfo.GetFiles("*.txt");
        foreach (FileInfo filex in Files)
        {
            string contents = File.ReadAllText(filex);
            string sharename = Regex.Match(contents, @"\Share+(\S*)(.)(.*)(.)").Groups[2].Value;
        }
    }

I just want to be able to open each textfile in this directory, read it all, and then pull the regex i have listed, With this code can someone point out where my brain fart is?

The best overloaded method match for 'System.IO.File.FileInfo' to 'string' has some invalid arguments Argument '1' cannot convert frm System.IO.FileInfo to string

Upvotes: 4

Views: 16805

Answers (3)

Joel Etherton
Joel Etherton

Reputation: 37533

File.ReadAllText does not accept FileInfo as a parameter. Use instead:

String contents = File.ReadAllText(filex.FullName);

Upvotes: 0

David Gladfelter
David Gladfelter

Reputation: 4213

Try:

string contents = File.ReadAllText(filex.FullName);

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

The ReadAllText method expects a filename argument of type string and not FileInfo:

string contents = File.ReadAllText(filex.FullName);

Upvotes: 28

Related Questions