| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3825 人关注过本帖
标题:C#使用FileStream和StreamWriter写入文件时出现文件被其他程序占用的异常, ...
只看楼主 加入收藏
小刀神
Rank: 1
等 级:新手上路
帖 子:69
专家分:5
注 册:2018-5-27
结帖率:91.67%
收藏
 问题点数:0 回复次数:2 
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();
                        }


图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: 文件 Project new Close 项目 
2019-03-09 17:18
md00000000
Rank: 4
等 级:业余侠客
威 望:7
帖 子:105
专家分:289
注 册:2017-6-22
收藏
得分:0 

我也是懵逼的
我先逼逼一下:
        确认字符串空值的最好方法:
                string pipixia = "wow";
                if (pipixia.Length == 0) {
                        Console.WriteLine("哇,是空值");
                }
                else {
                        Console.WriteLine("哇,不是空值");
                }
2019-03-22 22:13
md00000000
Rank: 4
等 级:业余侠客
威 望:7
帖 子:105
专家分:289
注 册:2017-6-22
收藏
得分:0 
继续逼逼:
        System.String
        System.Boolean
因为
        using System;
所以直接
        string
        bool
就好啦
2019-03-22 22:14
快速回复:C#使用FileStream和StreamWriter写入文件时出现文件被其他程序占用的异 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021711 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved