问问题
29428 次
3 回答
15
Here is your solution using StreamReader
class:
String path = @"C:\CSV.txt";
List<String> lines = new List<String>();
if (File.Exists(path));
{
using (StreamReader reader = new StreamReader(path))
{
String line;
while ((line = reader.ReadLine()) != null)
{
if (line.Contains(","))
{
String[] split = line.Split(',');
if (split[1].Contains("34"))
{
split[1] = "100";
line = String.Join(",", split);
}
}
lines.Add(line);
}
}
using (StreamWriter writer = new StreamWriter(path, false))
{
foreach (String line in lines)
writer.WriteLine(line);
}
}
If you want to overwrite the file, use this StreamWriter constructor with append = false
.
于 2013-01-17T01:37:16.527 回答
2
也许像这样,你可能需要清理它并添加一些错误处理,但它可以完成工作
string path = @"C:\\CSV.txt";
string[] lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
if (line.Contains(","))
{
var split = line.Split(',');
if (split[1].Contains("34"))
{
split[1] = "100";
line = string.Join(",", split);
}
}
}
File.WriteAllLines(@"C:\\CSV.txt", lines);
于 2013-01-17T01:32:10.133 回答
0
string str;
string PID;
string PN;
string UP;
string DIS;
string STK;
string CSVFilePathName = @"C:\admin.csv";
string[] Lines = File.ReadAllLines(CSVFilePathName);
File.Delete(CSVFilePathName);
StreamWriter sw = new StreamWriter(CSVFilePathName", true);
PID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text;
PN = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
UP = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
DIS = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
STK = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text;
foreach (string li in Lines)
{
string[] Fields = li.Split(',');
if(PID==Fields[0])
{
str = PID + "," + PN + "," + UP + "," + DIS + "," + STK;
}
else
{
str = Fields[0] + "," + Fields[1] + "," + Fields[2] + "," + Fields[3] + "," + Fields[4];
}
sw.WriteLine(str);
}
sw.Flush();
sw.Close();
于 2017-03-24T15:15:37.333 回答