By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,451 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

how to select specific rows and columns from a text file

P: 1
I am a newbie to C#. I want to select three columns from my text file i.e. Empl No, Start Date and Created Date. After selecting this, I want to insert the data into a database.

I have attached a copy of the text file.

I have the following code so far:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  if (File.Exists(filename))
  3.                 {
  4.  
  5.                     string[] lines = File.ReadAllLines(filename);
  6.                     for (int y = 0; y < lines.Length; y++) {
  7.  
  8.  
  9.                         Console.WriteLine(lines[y].ToString());
  10.  
  11.                     }
  12.  
  13.  
How do i select specific details for each employee
Attached Files
File Type: pdf test.pdf (409 Bytes, 321 views)
May 29 '14 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,035
your 'test.pdf' is not a PDF file, it's a text file.....

This as a next step, might help:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.  
  14.              String filename = @"c:\temp\test.pdf";
  15.  
  16.              if (File.Exists(filename))
  17.              {
  18.                  string[] lines = File.ReadAllLines(@filename);
  19.                  for (int y = 0; y < lines.Length; y++)
  20.                  {
  21.  
  22.                      Console.WriteLine( lines[y]);
  23.                      string[] columns = lines[y].Split('\t');
  24.                      for (int z = 0; z < columns.Length; z++)
  25.                      {
  26.                          Console.WriteLine(z + ":" + columns[z]);
  27.                      }
  28.                  }
  29.              }
  30.  
  31.         }
  32.     }
  33. }
  34.  
P.S. I'm not experienced in C# coding too.....
May 30 '14 #2

P: 22
hello try this for read specific line from text file in c#

Expand|Select|Wrap|Line Numbers
  1. private void read_txtfile(object sender, EventArgs e)
  2. {
  3.     string assemblyName = Assembly.GetExecutingAssembly().Location;
  4.     string assemblyDirectory = Path.GetDirectoryName(assemblyName);
  5.     m_readFile = new StreamReader (assemblyDirectory + @"\" + 
  6.      "test.txt");
  7.  
  8.     int counter = 1;
  9.     string line;
  10.     while ((line = m_readFile.ReadLine()) != null)
  11.     {
  12.         string col = line.Split(' ')[2];
  13.         MessageBox.Show(line);            
  14.         counter++;
  15.     }
  16.  
  17.     m_readFile.Close();
  18. }
18 Hours Ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.