CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2011
    Posts
    2

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

  2. #2
    Join Date
    Nov 2011
    Posts
    2

    Re: Need streamreader to read content of multiple files

    I meant so say no "data" (not "date")

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Need streamreader to read content of multiple files

    What happens when you put a breakpoint in the code and step through it?

  4. #4
    Join Date
    Feb 2008
    Posts
    108

    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.
    Developing using:
    .NET3.5 / VS 2010

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured