ZaneCen 2020-08-05 11:59 采纳率: 0%
浏览 747
已采纳

Sytem.IO.IOException:文件“XXX”正由另一进程使用,因此该进程无法访问该文件

程序是这样的

        public struct Point
        {
            public double X;
            public double Y;
        }
        public void ReadTxtAndSaveAsDir()
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            string path = "D:\\Zane自动合成";
            //判断文件夹是否存在
            if (Directory.Exists(path))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                FileInfo[] files = directoryInfo.GetFiles();
                for (int i = files.Length - 1; i < files.Length; i++)
                {
                    if (files[i].Extension.Equals(".txt"))   //判断是否为txt文件
                    {
                        //文本文件完整路径\并且读入所有行
                        string[] strs = File.ReadAllLines(path +"/"+ files[i].Name);
                        // 创建字典
                        Dictionary<double, double> myDictionary = new Dictionary<double, double>();
                        // 让过前两行,从第三行开始处理
                        try
                        {
                            for (int j = 2; j < strs.Length; j++)
                            {
                                string line = strs[j];
                                // 拆分行
                                string[] v = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                Point p;
                                // 获取Y(第一列)        
                                p.X = double.Parse(v[0]);
                                // 获取Y(第二列)        
                                p.Y = double.Parse(v[1]);
                                myDictionary.Add(p.X, p.Y);
                            }
                            strs = null;
                            // 至此,所有的数据点都在字典中了……  

                            //if (myDictionary.ContainsKey(274) || myDictionary.ContainsKey(269))
                            //{}
                            double x = myDictionary[274];
                            double y = myDictionary[269];
                            //double sub = x-y;
                            double div = x / y;
                        WriteIn(x, y, div);
                        textBox2.AppendText(
                                "274nm = " + x + ", " + "269nm = " + y + ", " + "274nm / 269nm = " + div+"\r\n");
                            if (div > 1.2 && x > 0.01)
                            {
                                Collect();
                            }
                    }
                    catch (FormatException)
                        { }
                    }
                }
            }
            else { Directory.CreateDirectory(path); }
        }

其他部分都能很好的运行,但是每次到
string[] strs = File.ReadAllLines(path +"/"+ files[i].Name);
这个部分就会报错
请大神指导

  • 写回答

2条回答 默认 最新

  • hxycsdn9159 2020-08-05 13:39
    关注

    1.for (int i = files.Length - 1; i < files.Length; i++) 确定不是 i = 0 开始?
    2.查看下你的任务管理器是不是已经有该进程在运行?也就是说你点了关闭按钮其实你的程序还有线程在运行导致你第再次打开的时候实际有两个或者多个进程在运行

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!