Need streamreader to read content of multiple files
I can get streamreader to work on one file, but can't code right to get it to read multiple.
The first code is where I have it working fine. The second one just produces a dos window with no date.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace readtest
{
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader("C:\\Users\Random.txt");
string str = sr.ReadLine();
string[] words = str.Split('|');
foreach (string word in words)
{
Console.WriteLine(word);
} Console.ReadKey();
}
}
}
This is the one I can't figure out what is wrong. No errors being produced, but not getting the result I want either.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string[] files = Directory.GetFiles("C:\\Users", ".txt");
foreach (string file in files)
{
StreamReader sr = new StreamReader(file);
string str = sr.ReadLine();
{
Console.WriteLine(file);
}
} Console.ReadKey();
}
}
}
Re: Need streamreader to read content of multiple files
I meant so say no "data" (not "date")
Re: Need streamreader to read content of multiple files
What happens when you put a breakpoint in the code and step through it?
Re: Need streamreader to read content of multiple files
I think you mean a wildcard "*.txt" rather than ".txt" in the Directory.GetFiles() function call.