执行代码string csvFileName = Path.Combine(@"E:\report", fileName + ".csv"); if (File.Exists(csvFileName)) { File.Delete(csvFileName); } using (StreamWriter sw = new StreamWriter(csvFileName, true, Encoding.UTF8)) 表头{ sw.WriteLine("Test Model: " + exParams.GetParameter("Model") + "--特斯拉烧录系统"); sw.WriteLine("Software Version: " ); sw.WriteLine("Serial Number: " + barcode); sw.WriteLine("Start Time: " ); sw.WriteLine("Test Time: " + testTime); sw.WriteLine("Test Result: " + result); sw.WriteLine("Test Operator: " ); sw.WriteLine("Test Equipment Number: " + exParams.GetParameter("E_NO")); sw.WriteLine("Burnprocess: " + exParams.GetParameter("Process")); sw.WriteLine("************************************************************************************************************************"); } }在转换后,之前的txt文件内容不见了,只剩添加的表头了。在此基础上改,先转换,再将表头添加到csv文件中,txt文件内容紧随其后
时间: 2023-06-18 13:04:20 浏览: 134
可以尝试以下代码:
```
string txtFileName = Path.Combine(@"E:\report", fileName + ".txt");
string csvFileName = Path.Combine(@"E:\report", fileName + ".csv");
if (File.Exists(csvFileName)) {
File.Delete(csvFileName);
}
// 读取txt文件内容
string txtContent = File.ReadAllText(txtFileName, Encoding.UTF8);
using (StreamWriter sw = new StreamWriter(csvFileName, true, Encoding.UTF8)) {
// 先写入表头
sw.WriteLine("Test Model: " + exParams.GetParameter("Model") + "--特斯拉烧录系统");
sw.WriteLine("Software Version: ");
sw.WriteLine("Serial Number: " + barcode);
sw.WriteLine("Start Time: ");
sw.WriteLine("Test Time: " + testTime);
sw.WriteLine("Test Result: " + result);
sw.WriteLine("Test Operator: ");
sw.WriteLine("Test Equipment Number: " + exParams.GetParameter("E_NO"));
sw.WriteLine("Burnprocess: " + exParams.GetParameter("Process"));
sw.WriteLine("************************************************************************************************************************");
// 写入txt文件内容
sw.WriteLine(txtContent);
}
```
首先读取原来的txt文件内容,然后将表头和txt文件内容一起写入到csv文件中。注意添加了`true`参数,表示以追加方式写入文件。
阅读全文