Reading Text file in with c# code.

Reading Text file in with C# code
Hi all looking for some information on this.  I have a text file that has sections all the way through that look like this:

dn: uid=joeshmoe,
objectclass: myuser
uid: joeshmoe
creatorsname: cn=Directory Manager
createtimestamp: 20020827204754Z
usertype: testuser
admin: 0

I need to read through the file and when I hit the first line in this file then I need to look for things such as "uid" and store it to a variable.  Also on the first line I need to capture the ou=444444 into a variable named CustNO.    Then I want to store all these to variables.  Once I hit a new start line with "uid" in it i want to write to a SQL database the variables into a record I have all that figured out.  Then I want to clear the vars and then start over.  Like admin I will need to store to a variable but I dont need the creatorsname etc.  Thanks all for any help.  One last thing one of the lines is called "custid:" but then there is also colons in the value.  I need to read into a variable after the first colon so for example,
custid: 34:45:89.

I used a business object class to declare all these variables.
Who is Participating?
silemoneConnect With a Mentor Commented:
This code works for the template you provided...Stores each heading into a separate variable...i then send it to a label named label3 to show that it works...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace fileReader
     public partial class Form1 : Form
          public Form1()
               string strLine=String.Empty;
               string strLine2 = String.Empty;
               StreamReader objSR = null;
                     objSR = new StreamReader(new FileStream(@"c:\test.txt", FileMode.Open));
                     string[] separator = { "Date:", "address1:", "address2:", "state:", "zip:", "fax:", "body of the letter:", "specialist name:" };
                     strLine = objSR.ReadToEnd();
                     string[] strParts = strLine.Split(separator, StringSplitOptions.None);

                     int x = strParts.Length;

                     string date = strParts[0];
                     string address1 = strParts[1];
                     string address2 = strParts[2];
                     string state = strParts[3];
                     string zip = strParts[4];
                     string fax = strParts[5];
                     string body = strParts[6] + strParts[7];
                     string specialist = strParts[8];

                          label3.Text = date + Environment.NewLine + address1 + Environment.NewLine + address2 + Environment.NewLine + state + Environment.NewLine + zip + Environment.NewLine + fax + Environment.NewLine + body + Environment.NewLine + specialist;

                catch (Exception ex)
                     string error = ex.StackTrace.ToString();
                     if (objSR != null)

          private void Form1_Load(object sender, EventArgs e)

use scanner class....

if they all have carriage line feeds, then let the delimiter be the natural delimiter which should separate by \r\n

then split the text  at :  
oops..this is c#...i said scanner class which is java...
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

so use split twice...

split first at "\r\n" or Environment.Newline

then split text at : to get value...
here's an example of splitting to parse...
If you don't want to split then read line by line, find the first colon and use everything to the left of that as your key and to the right as your value.
onebite2Author Commented:
Thanks for ur suggestions....I have no clue how would I implement this clearly ....can any one help me write an example.
Attached is the textfile I have.which contains :

Date:May 13, 2008
address1:StONE RAYMOND
body of the letter,:
 The person listed above has filed an unemployment claim against your company. Based
  on the state's ruling, the  has filed an appeal. A hearing will be scheduled
  by the state within the next four to six weeks.
            * The claimant's file
            * Documentation (exhibits)
            * Hearing procedures (what to expect at the hearing)
  Please review the "Hearing Witness Guide" for helpful preparation information for
  the upcoming hearing latest ap headlines Longoria is AL Rookie of Year, Soto wins NL
  awardEU to resume frozen partnership talks with Russi Sex charge dropped against ex-Texas
  police chief Lottery results .DHL to cut 9,500 jobs and close US service centers
specialist name.:  Sam.

Now I want to read the text from this file ,just the variable values line by line and insert them into a table in database.
I am sure first I need to read the text and then load the values with adhoc SQL...

Can any one give me the sample code for this scenario....
Thanks!!!I appreciate ur help.
will start working on the way, this is c# yes?
onebite2Author Commented:
Thanks Silemone,

Yes it is c#.I appreciate your help.
wow...that middle section is really getting me...but i am working on this...
With the code, if you have only the heading that you say, then it searches by heading (headings are the delimiters)...With those headings, there will always be 9 fields (for some reason, the last paragraph of body is given a separate field...if you need to add heading, etc...then play with code and change it accordingly to accommodate template...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.