locked
Reading lines from a .txt file or such? RRS feed

  • Question

  • I want to read from a txt file or another type of document. And read specific lines, so that  I can generate names from two txtfiles. Anyone know how?
    - Stig
    Sunday, March 7, 2010 6:54 AM

Answers

  • I recommend you to use IO.File.ReadAllLines(), it returns an array then you could acces to  the specific lines you need....

    Hope this helps.

    Para el correcto funcionamiento, y que otros usuarios se puedan beneficiar de la solucion de esta pregunta por favor marca las respuestas que te hayan ayudado como "Respuesta".
    Si la respuesta te ha sido util Votala.
    Mi Blog: Jtorrecilla
    Enlace a Faq de Winforms en Ingles Muy bueno
    • Marked as answer by Harry Zhu Monday, March 15, 2010 5:12 AM
    Sunday, March 7, 2010 7:17 AM
  • http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx

    using System;
    using System.IO;
    public class TextFromFile 
    {
        private const string FILE_NAME = "MyFile.txt";
        public static void Main(String[] args) 
        {
            if (!File.Exists(FILE_NAME)) 
            {
                Console.WriteLine("{0} does not exist.", FILE_NAME);
                return;
            }
            using (StreamReader sr = File.OpenText(FILE_NAME))
            {
                String input; int lineCount = 0;
                while ((input=sr.ReadLine())!=null) 
                {
                    if (lineCount == desired) use(input); lineCount++;
                }
                Console.WriteLine ("The end of the stream has been reached.");
            }
        }
    The code is from msdn (the link above), modified a little.

    andrei

    • Marked as answer by Harry Zhu Monday, March 15, 2010 5:11 AM
    Sunday, March 7, 2010 12:37 PM

All replies

  • I recommend you to use IO.File.ReadAllLines(), it returns an array then you could acces to  the specific lines you need....

    Hope this helps.

    Para el correcto funcionamiento, y que otros usuarios se puedan beneficiar de la solucion de esta pregunta por favor marca las respuestas que te hayan ayudado como "Respuesta".
    Si la respuesta te ha sido util Votala.
    Mi Blog: Jtorrecilla
    Enlace a Faq de Winforms en Ingles Muy bueno
    • Marked as answer by Harry Zhu Monday, March 15, 2010 5:12 AM
    Sunday, March 7, 2010 7:17 AM
  • http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx

    using System;
    using System.IO;
    public class TextFromFile 
    {
        private const string FILE_NAME = "MyFile.txt";
        public static void Main(String[] args) 
        {
            if (!File.Exists(FILE_NAME)) 
            {
                Console.WriteLine("{0} does not exist.", FILE_NAME);
                return;
            }
            using (StreamReader sr = File.OpenText(FILE_NAME))
            {
                String input; int lineCount = 0;
                while ((input=sr.ReadLine())!=null) 
                {
                    if (lineCount == desired) use(input); lineCount++;
                }
                Console.WriteLine ("The end of the stream has been reached.");
            }
        }
    The code is from msdn (the link above), modified a little.

    andrei

    • Marked as answer by Harry Zhu Monday, March 15, 2010 5:11 AM
    Sunday, March 7, 2010 12:37 PM