我正在尝试编写一个循环读取列表,并根据数据的小时将该列表的一部分保存到不同的文本文件中。我想根据数据的小时来命名文本文件。
以下是相关代码:
private StreamWriter filename;
string[] hour = {"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23}
private void btn_hourfiles_click(object sender, EventArgs e)
{
int sizeoflist = Data_List.Count(); //This is the list containing the data
for (int i = 0; i<sizeoflist,i++)
{
string[] listsplit = Data_List[i].Split(":");
for (int j = 0; j<24; j++)
{
if (listsplit[2] == hour[j])
{
string[] datesplit = txt_DATE.Text.Split('-');//splits input date
filename = datesplit[0] + '_' + dateplit[1] + '_' + datesplit[2] + '_' + hour[j] + '.dat'
filename = new StreamWriter(new FileStream(Data_List[i], FileMode.Append, FileAccess.Write));
}
}
}
}
问题:我无法更改StreamWriter filename
。我收到一条错误消息:Cannot implicitly convert type string to System.IO.StreamWriter
问题:如何更改变量文件名?
答案 0 :(得分:0)
答案 1 :(得分:0)
问题在于StreamWriter
如此声明:
private StreamWriter filename;
但随后分配了一个字符串:
filename = datesplit[0] + '_' + dateplit[1] + '_' + datesplit[2] + '_' + hour[j] + '.dat'
建议:对文件名和StreamWriter
使用单独的变量。