在 Windows 窗体 C# 中,我必须在多个线程中写入多个文件,如果文件已经存在,则覆盖它。全部完成后重新安排时间。需要帮忙..
public Boolean CreateWriteCSV(string filename,BindingList<Data> data,ItemList us)
{
bool result = false;
try
{
if data!= null)
{
// lock (data)
{
if (data.Count > 0)
{
String contents = string.Empty;
StreamWriter writer;
using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write))
{
// lock (stream)
{
writer = new StreamWriter(stream);
writer.WriteLine(contents.TrimEnd(new char[] { '\r', '\n' }));
contents = "X,Y,Z"+Environment.NewLine;
foreach (Data item in data)
{
contents += data.x+","+data.y+","+data.z+ Environment.NewLine;
}
writer.WriteLine(contents.TrimEnd(new char[] { '\r', '\n' }));
writer.Flush();
stream.Close();
///---check to see file created or not
///
us.IsExported = true;
lock (parallelThreadList)
{
parallelThreadList.Remove(filename);
}
}
}
}
return true;
}
}
else
{
return false;
}
}
catch
{
return false;
}
}