Greetings everyone,
I have a CSV file and for some reason the first row is bunch of junk data. Is it there a way to delete the first row using C# ... my boss wants a win application and i am a DB developer... i know very little about C#. Thank you in advance. Waiting for your input.
Ion

delete first row in CSV file
Crasch
It works. I do not want to impose.. but can the output file be the same one as the input. Sorry... Thanks
dcab
There are 2 ways. Either 1, read the input file into memory and parse it line by line. Close the file, delete it, write the file from memory. Which I do not think would be a best practice because if the file is large you use up a lot of memory and if there is a failure before you complete the write operation you lose the file.
-or- I suppose a best practice would be to let the read/write process finish. Then delete the input file and rename the output file. Like this:
File.Delete(inputfile);
File.Move( outputfile, inputfile );
That code snippet would be inserted after the sr.close(); line.
- David Sandor
Fluffy Bunnyfeet
The easiest way to do this would be like this:
using
System;using System.IO;
namespace
ConsoleApplication2{ /// <summary>
/// Summary description for Class1.
/// </summary> class Class1
{ /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string inputfile = @"C:\inputfile.csv";
string outputfile = @"C:\outputfile.csv";
string buffer;
StreamReader sr = File.OpenText(inputfile);
buffer = sr.ReadLine();
// first line of the input file. if ( buffer != null ){ // Create an output stream.
StreamWriter sw = File.CreateText( outputfile ); //Read line 2
buffer = sr.ReadLine(); while(buffer != null) // keep looping as long as the last line read was not null
{ // write the last read line to the output file
sw.WriteLine(buffer); // read the next line and loop
buffer = sr.ReadLine();
}
sw.Close();
// Close the output file.}
sr.Close();
// Close the input file.}
}
}
- David Sandor