Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What am I doing wrong?

What am I doing wrong?

Scheduled Pinned Locked Moved C#
csharplinqquestionlounge
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 8327598
    wrote on last edited by
    #1

    The first code works, however I need to be able to read contents of multiple files from a particular directory. Start simple and build from there I figure. Doing something wrong somewhere, as the 2nd code only brings up the dos window without any data.

    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 need to have working. I must be structuring something wrong. I've tried different ways, but no luck.

    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();
    
            }
        }
    }
    
    H R 2 Replies Last reply
    0
    • U User 8327598

      The first code works, however I need to be able to read contents of multiple files from a particular directory. Start simple and build from there I figure. Doing something wrong somewhere, as the 2nd code only brings up the dos window without any data.

      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 need to have working. I must be structuring something wrong. I've tried different ways, but no luck.

      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();
      
              }
          }
      }
      
      R Offline
      R Offline
      RobCroll
      wrote on last edited by
      #2

      try

      string[] files = Directory.GetFiles("C:\\Users\\", "*.txt");

      Although it may read a bit better with an @ sign

      string[] files = Directory.GetFiles(@"C:\Users\", "*.txt");

      "You get that on the big jobs."

      1 Reply Last reply
      0
      • U User 8327598

        The first code works, however I need to be able to read contents of multiple files from a particular directory. Start simple and build from there I figure. Doing something wrong somewhere, as the 2nd code only brings up the dos window without any data.

        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 need to have working. I must be structuring something wrong. I've tried different ways, but no luck.

        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();
        
                }
            }
        }
        
        H Offline
        H Offline
        Hum Dum
        wrote on last edited by
        #3

        Member 8363084 wrote:

        string[] files = Directory.GetFiles("C:\\Users", ".txt");

        you have to use * here as

        string[] files = Directory.GetFiles("C:\\Users\\", "*.txt");

        regards

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups