C#使用FileStream和StreamWriter写入文件时出现文件被其他程序占用的异常,求大神指点。
出现异常的代码:程序代码:
if ((ProjectInPath + "/Project/ProjectName/ProjectNameRead.LMPN")) { String ProjectNameWrite = (ProjectInPath + "/Project/ProjectName/ProjectNameRead.LMPN"); FileStream fs_sw = new FileStream(ProjectInPath + "/Project/ProjectName/ProjectNameRead.LMPN", FileMode.Create ); StreamWriter sw_fs = new StreamWriter(fs_sw); if(ProjectNameWrite == "") { sw_fs.Write(ProjectName); } else { sw_fs.Write(ProjectNameWrite + Environment.NewLine + ProjectName); } sw_fs.Flush(); sw_fs.Close(); fs_sw.Close(); FileStream FS = new FileStream(ProjectInPath + "/Project/ProjectPath/" + ProjectName + ".LMPP",FileMode.Create); StreamWriter SW = new StreamWriter(FS); SW.Write("DefaultProjectPath"); SW.Flush(); SW.Close(); FS.Close(); FileStream FSSW = new FileStream(ProjectInPath + "/Project/ProjectDate/" + ProjectName + ".LMPD",FileMode.Create); StreamWriter SWFS = new StreamWriter(FSSW); SWFS.Write(CreateDate); SWFS.Flush(); SWFS.Close(); FSSW.Close(); FileStream fs_LastOpenTime = new FileStream(ProjectInPath + "/Project/LastOpenTime/" + ProjectName + ".LMLOT", FileMode.Create); StreamWriter sw_LastOpenTime = new StreamWriter(fs_LastOpenTime); sw_LastOpenTime.Write(System.DateTime.Now.ToString()); sw_LastOpenTime.Flush(); sw_LastOpenTime.Close(); fs_LastOpenTime.Close(); FileStream fs_FinallyOpenProject = new FileStream(ProjectInPath + "/Project/FinallyOpenProject/FinallyOpenProject.LMFOP", FileMode.Create); StreamWriter sw_FinallyOpenProject = new StreamWriter(fs_FinallyOpenProject); sw_FinallyOpenProject.Write(ProjectName); sw_FinallyOpenProject.Flush(); sw_FinallyOpenProject.Close(); fs_FinallyOpenProject.Close(); return true; } else { return false; }
还使用了一段代码,调试了数次之后,发现似乎与这段代码有些关系:
程序代码:
String line = String.Empty; int IfProjectNameSave = 0; using (StreamReader reader = new StreamReader(SC.GetInPath() + "/Project/ProjectName/ProjectNameRead.LMPN")) { line = reader.ReadLine(); while(line != null && line != "") { if(TextBox_项目名称.Text == line) { MessageBox.Show("已存在相同项目名的项目!", "提示"); IfProjectNameSave = 1; break; }else { IfProjectNameSave = 2; } line = reader.ReadLine(); } if((SC.GetInPath() + "/Project/ProjectName/ProjectNameRead.LMPN") == "") { IfProjectNameSave = 2; } if(IfProjectNameSave == 2) { String ProjectType = String.Empty; String CompileType = String.Empty; Boolean IfCreateProject; if(RadioButton_Windos.Checked) { ProjectType = "Windows"; } if(RadioButton_解释编写.Checked) { CompileType = "脚本编译"; } if((SC.GetInPath() + "/Project/DefaultProjectPath/" + TextBox_项目名称.Text + ".LMP")) { MessageBox.Show("存储路径下有着相同的项目文件,程序将默认为覆盖此项目文件!", "警告"); File.Delete(SC.GetInPath() + "/Project/DefaultProjectPath/" + TextBox_项目名称.Text + ".LMP"); IfCreateProject = SC.CreateProject(TextBox_项目名称.Text, ProjectType, System.DateTime.Now.ToString(), CompileType, SC.GetInPath()); if (IfCreateProject) { SC.InEdit(); } else { MessageBox.Show("项目创建失败,请到官网查看解决方法!", "提示"); } } else { IfCreateProject = SC.CreateProject(TextBox_项目名称.Text, ProjectType, System.DateTime.Now.ToString(), CompileType, SC.GetInPath()); if (IfCreateProject) { SC.InEdit(); } else { MessageBox.Show("项目创建失败,请到官网查看解决方法!", "提示"); } } } reader.Close(); }