How can I clear a file's content?
-
if (File.Exists(paraFile)) File.Delete(paraFile); using (FileStream fs = new FileStream(paraFile, FileMode.Create)) { //fs.Seek(0, SeekOrigin.Begin); //fs.SetLength(0); using (StreamWriter sw = new StreamWriter(fs)) { //声明数据流文件写入方法 sw.WriteLine(markLineOfRobotReason); sw.WriteLine(""); foreach (string reason in robotRegisterReasons) sw.WriteLine(reason); sw.WriteLine(""); sw.WriteLine(markLineOfHumanReason); sw.WriteLine(""); foreach (string reason in humanRegisterReasons) sw.WriteLine(reason); test++; sw.WriteLine(""); sw.Write("第" + test + "次写末尾参数"); sw.WriteLine(markLineOfExecRate); sw.WriteLine(""); sw.WriteLine(comboBox1.SelectedItem.ToString()); sw.WriteLine(""); sw.WriteLine(markLineOfIfShown); sw.WriteLine(""); sw.WriteLine(checkBox1.Checked); } }
result is like this:
Quote:
第3次写末尾参数------------运行频率(小时):------------ 1 ------------是否显示作业窗口:------------ True 第6次写末尾参数------------运行频率(小时):------------ 1 ------------是否显示作业窗口:------------ True 第3次写末尾参数------------运行频率(小时):------------ 1 ------------是否显示作业窗口:------------ True
why?I think it should only 1 times...
-
if (File.Exists(paraFile)) File.Delete(paraFile); using (FileStream fs = new FileStream(paraFile, FileMode.Create)) { //fs.Seek(0, SeekOrigin.Begin); //fs.SetLength(0); using (StreamWriter sw = new StreamWriter(fs)) { //声明数据流文件写入方法 sw.WriteLine(markLineOfRobotReason); sw.WriteLine(""); foreach (string reason in robotRegisterReasons) sw.WriteLine(reason); sw.WriteLine(""); sw.WriteLine(markLineOfHumanReason); sw.WriteLine(""); foreach (string reason in humanRegisterReasons) sw.WriteLine(reason); test++; sw.WriteLine(""); sw.Write("第" + test + "次写末尾参数"); sw.WriteLine(markLineOfExecRate); sw.WriteLine(""); sw.WriteLine(comboBox1.SelectedItem.ToString()); sw.WriteLine(""); sw.WriteLine(markLineOfIfShown); sw.WriteLine(""); sw.WriteLine(checkBox1.Checked); } }
result is like this:
Quote:
第3次写末尾参数------------运行频率(小时):------------ 1 ------------是否显示作业窗口:------------ True 第6次写末尾参数------------运行频率(小时):------------ 1 ------------是否显示作业窗口:------------ True 第3次写末尾参数------------运行频率(小时):------------ 1 ------------是否显示作业窗口:------------ True
why?I think it should only 1 times...
The question is unclear. Try to rephrase it and give all the necessary information to investigate the problem. Based on current info, perahps you're wondering why
foreach (string reason in robotRegisterReasons)
sw.WriteLine(reason);prints many times but that would happen robotRegisterReasons array (or list or what ever it is) contains multiple items.
-
The question is unclear. Try to rephrase it and give all the necessary information to investigate the problem. Based on current info, perahps you're wondering why
foreach (string reason in robotRegisterReasons)
sw.WriteLine(reason);prints many times but that would happen robotRegisterReasons array (or list or what ever it is) contains multiple items.
-
The question is unclear. Try to rephrase it and give all the necessary information to investigate the problem. Based on current info, perahps you're wondering why
foreach (string reason in robotRegisterReasons)
sw.WriteLine(reason);prints many times but that would happen robotRegisterReasons array (or list or what ever it is) contains multiple items.