作者: 小刀神
查看: 1309|回复: 0
打印 上一主题 下一主题

C#使用FileStream和StreamWriter写入文件时出现文件被其他程序...

[复制链接]
跳转到指定楼层
楼主
查看: 1309|回复: 0
出现异常的代码:
[C#] 纯文本查看 复制代码
if (System.IO.File.Exists(ProjectInPath + "/Project/ProjectName/ProjectNameRead.LMPN"))
 {
 String ProjectNameWrite = System.IO.File.ReadAllText (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;
 }


还使用了一段代码,调试了数次之后,发现似乎与这段代码有些关系:


[C#] 纯文本查看 复制代码
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(System.IO.File.ReadAllText(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(System.IO.File.Exists(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();
                        }



捕获.PNG (78.23 KB, 下载次数: 119)

C#使用FileStream和StreamWriter写入文件时出现文件被其他程序...

C#使用FileStream和StreamWriter写入文件时出现文件被其他程序...
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播
回复 论坛版权

使用道具 举报

您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-12-22 19:47, Processed in 0.546524 second(s), 33 queries , Gzip On.

快速回复 返回顶部 返回列表