0

What i am trying to do is get the number of files in a specific directory and add that in .csv file with the computer name. (which is done). What i can't do is read the file first, if computer name already exists, it should change the corresponding values. There are five fields in .csv file. I want to find and replace the rest of the changed values on the basis of first field i.e. the name of computer.

Is there a way to probably make a search pattern to replace.

        if (File.ReadAllText(filepath).Contains(compName))
        {
            var s = File.ReadAllText(filepath);
            s = s.Replace(compName, fileText.ToString());
            File.WriteAllText(filepath, s);
        }
       else
       {
            File.AppendAllText(filepath, fileText);
       }
Taarush Jain
  • 5
  • 1
  • 4
  • 4
    It's a CSV... So use a [CSV parser](http://stackoverflow.com/questions/2081418/parsing-csv-files-in-c-with-header) to build an object model of the data, make your alterations and write it back out. Doing it any other way is akin to using [regular expressions to parse HTML](http://stackoverflow.com/questions/2081418/parsing-csv-files-in-c-with-header). – spender Apr 11 '16 at 16:10
  • Your question is pretty confusing. Please provide a sample csv file and explain referring to that sample, what you need to achieve – Vikhram Apr 11 '16 at 16:55

0 Answers0