Creating a CSV File in C#

后端 未结 3 784
温柔的废话
温柔的废话 2021-01-13 18:25

I am trying to created a csv file where each entry is inputed by the user. Once one set of of values have been entered it should move on to the next. However I cannot seem t

相关标签:
3条回答
  • 2021-01-13 19:07

    You have to open the file in append mode, like this:

    System.IO.StreamWriter objWriter;
    objWriter = new File.AppendText(file_name);
    
    0 讨论(0)
  • 2021-01-13 19:10

    In your case I think you are better off doing something like:

    File.AppendAllText("EmployeeDetails.txt", string.Join(",",new [] {FName,LName,Dpt,Grade,NumberOfHours}));
    
    0 讨论(0)
  • 2021-01-13 19:27

    The StreamWriter constructor will overwrite the file you are using if it exists. There is an overloaded constructor to which you can pass a boolean as a second argument to tell it to append:

    System.IO.StreamWriter objWriter;
    objWriter = new System.IO.StreamWriter(file_name, true);
    

    In addition, the Write method you are using will not append a line break. If you use the WriteLine method, it will.

     Console.WriteLine(LDetailsCSV);
     objWriter.WriteLine(LDetailsCSV);
     objWriter.Close();
    
    0 讨论(0)
提交回复
热议问题