[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

Windows Service - File Watcher -- read comma delimited values

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
  • 2
2 Solutions
Bob LearnedCommented:
What have you tried?  What is your question?  How may I help you get your problem solved?
gautam_reddycAuthor Commented:
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
Bob LearnedCommented:
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now