Windows Service - File Watcher -- read comma delimited values

Posted on 2009-12-18
Last Modified: 2012-05-08
Hi.. i have to read the comma delimited values from the text file.. and put each value in a string variable..
For example:


I have to read those values and display in string
str id = 01
str firstname = gautam
str lastname = reddy
address = hyderabad

BTW, I am writing this in the Windows Service (C#). So i need to use the file watcher to check when the txt files is deployed in the folder..
Please help
Question by:gautam_reddyc
    LVL 96

    Accepted Solution

    What have you tried?  What is your question?  How may I help you get your problem solved?

    Author Comment

    protected override void OnStart(string[] args)
                string pathToDirectory = "";//path to directory to watch goes here
                //the second argument below assumes that the files with the student info have the txt extension.
                //if it is a different extension adjust the parameter.
                studentFSW = new FileSystemWatcher(pathToDirectory, "*.txt");
                studentFSW.Created += new FileSystemEventHandler(studentFSW_Created);
                studentFSW.EnableRaisingEvents = true;
                WebServiceProxyClass proxy = new WebServiceProxyClass();
            void studentFSW_Created(object sender, FileSystemEventArgs e)
                //Here we handle each of the student files as they are created.  If the process that is filling the
                //files with data takes some time you might want to put a very short pause in here.
                Student stu = new Student();
                StreamReader sr = File.OpenText(e.FullPath);
                while (!sr.EndOfStream)
                    string nextLine = sr.ReadLine();
               // what to code here... i need to take values seperated by comma and put them in a string variables.. as explained in my question
    LVL 96

    Assisted Solution

    by:Bob Learned
    First of all, it sounded like you wanted someone to write code, and that is not something that I try to do out of the starting gate.

    Second, choosing the right method for CSV parsing depends on a few factors, like whether your CSV files have quotes, or not, how big the files are, how optimized you need the process, ...  

    You can use a simplified approach if you don't expect quotes, and the file are not very large (System.IO.File.ReadAllLines(file)), and string.Split(',') to break apart the lines.

    There are plenty of examples to be found with a Google search for more complicated CSV file readers.


    Parsing Data Files

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video discusses moving either the default database or any database to a new volume.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now