在 C# 中创建 CSV 文件
我正在尝试创建一个 csv 文件,其中每个条目均由用户输入。输入一组值后,应继续输入下一组值。然而我似乎无法做到这一点。每次我运行该程序时,它都会替换以前的值。
using System;
using System.IO;
using System.Collections.Generic;
namespace FileAccess
{
class MainClass
{
public static void Main(string[] args)
{
string FName;
string LName;
string Dpt;
string Grade;
string NumberOfHours;
string file_name = "EmployeeDetails.txt";
System.IO.StreamWriter objWriter;
objWriter = new System.IO.StreamWriter(file_name);
String[] ArrayDetails = new string[5];
Console.Write("Enter Emp First Name: ");
FName=Console.ReadLine();
Console.Write("Enter Emp Last Name: ");
LName = Console.ReadLine();
Console.Write("Enter Emp Department: ");
Dpt = Console.ReadLine();
Console.Write("Enter Employee Grade: ");
Grade = Console.ReadLine();
Console.Write("Enter Number Of Hours Worked: ");
NumberOfHours = Console.ReadLine();
ArrayDetails[0] = FName;
ArrayDetails[1] = LName;
ArrayDetails[2] = Dpt;
ArrayDetails[3] = Grade;
ArrayDetails[4] = NumberOfHours;
// Create new list of strings
List<string> LDetails = new List<string>(); // Create new list of strings
LDetails.Add(FName); // Add string 1
LDetails.Add(LName); // 2
LDetails.Add(Dpt); // 3
LDetails.Add(Grade); // 4
LDetails.Add(NumberOfHours); // 5
//OutPut the data into the file
string LDetailsCSV = string.Join(",", LDetails.ToArray());
Console.WriteLine(LDetailsCSV);
objWriter.Write(LDetailsCSV);
objWriter.Close();
}
}
}
请一些建议。
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 to do the bit. Each time i run the program, it replaces the previous values.
using System;
using System.IO;
using System.Collections.Generic;
namespace FileAccess
{
class MainClass
{
public static void Main(string[] args)
{
string FName;
string LName;
string Dpt;
string Grade;
string NumberOfHours;
string file_name = "EmployeeDetails.txt";
System.IO.StreamWriter objWriter;
objWriter = new System.IO.StreamWriter(file_name);
String[] ArrayDetails = new string[5];
Console.Write("Enter Emp First Name: ");
FName=Console.ReadLine();
Console.Write("Enter Emp Last Name: ");
LName = Console.ReadLine();
Console.Write("Enter Emp Department: ");
Dpt = Console.ReadLine();
Console.Write("Enter Employee Grade: ");
Grade = Console.ReadLine();
Console.Write("Enter Number Of Hours Worked: ");
NumberOfHours = Console.ReadLine();
ArrayDetails[0] = FName;
ArrayDetails[1] = LName;
ArrayDetails[2] = Dpt;
ArrayDetails[3] = Grade;
ArrayDetails[4] = NumberOfHours;
// Create new list of strings
List<string> LDetails = new List<string>(); // Create new list of strings
LDetails.Add(FName); // Add string 1
LDetails.Add(LName); // 2
LDetails.Add(Dpt); // 3
LDetails.Add(Grade); // 4
LDetails.Add(NumberOfHours); // 5
//OutPut the data into the file
string LDetailsCSV = string.Join(",", LDetails.ToArray());
Console.WriteLine(LDetailsCSV);
objWriter.Write(LDetailsCSV);
objWriter.Close();
}
}
}
Some advise please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
StreamWriter 构造函数 将覆盖您正在使用的文件(如果存在)。有一个重载构造函数,您可以将布尔值作为第二个参数传递给它来告诉它附加:
此外,您使用的 Write 方法不会附加换行符。如果您使用 WriteLine 方法,它会。
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:
In addition, the Write method you are using will not append a line break. If you use the WriteLine method, it will.
您必须以附加模式打开文件,如下所示:
You have to open the file in append mode, like this:
就你而言,我认为你最好这样做:
In your case I think you are better off doing something like: