C#小白提问,自己修改了一些网上的代码,最后实用的时候出现了问题,不知道问题出在哪里,请大佬帮忙瞥一眼

建了几个方法,来监控一个文件夹内的变化,文件夹的内容由仪器传入,并将需要的内容提取出来,汇总到另一个文件。当数值达到要求的点后,启动Collect方法,但是现在出现的问题是,无法将内容提取并写入,达到了要求的点后,也无法调用collect方法。还会出现被另一个进程使用,无法访问的情况。
主要代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Timers;
using System.Collections;
using System.IO;

namespace 自动收集样品
{
    public partial class Form1 : Form
    {
                 public struct Point
        {
            public double X;
            public double Y;
        }
        static void ReadTxtAndSaveAsDir()
        {
            string path = "D:\\ECNU\\紫外测试";
            //判断文件夹是否存在
            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);
                            }
                            // 至此,所有的数据点都在字典中了……  
                            double x = myDictionary[274];
                            double y = myDictionary[269];
                            double div = x / y;
                            WriteIn(x, y, div);
                            if (div > 1.2 && x > 0.01)
                            {
                                Collect();
                            }
                        }
                        catch (FormatException)
                        { }
                    }
                }
            }
            else { Directory.CreateDirectory(path); }
        }
                static public void WriteIn(double X, double Y, double Z)
        {
            //传入写入的内容
            string Text = "274nm=" + X + "," + "269nm=" + Y+"," + "274nm/269nm=" + Z;
            //判断文件是否存在
            if (File.Exists(@"E:\\ak.txt"))
            {
                StreamWriter sw = File.AppendText(@"E:\\ak.txt");
                //开始写入
                sw.WriteLine(Text);
                sw.Flush();
                sw.Close();
            }
            else
            {
                //创建并写入
                File.WriteAllText(@"E:\\ak.txt", Text + "\n");
            }
        }
                static public void Collect()
        {
            //启动收集样品
            //调整速度
            form.textBox1.AppendText("66\r\n");
            form.Send();
            //改为顺时针
            form.textBox1.AppendText("97\r\n");
            form.Send();
            //启动
            form.textBox1.AppendText("9\r\n");
            form.Send();

            System.Timers.Timer aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new ElapsedEventHandler(form.stop);
            //到达时间的时候执行事件;    
            aTimer.Interval = 24000;
            // 设置引发时间的时间间隔 此处设置为24秒,注入体积为40毫升
            aTimer.AutoReset = false;
            //设置是执行一次(false)还是一直执行(true);
            aTimer.Enabled = true;
            //是否执行System.Timers.Timer.Elapsed事件
            //WatcherStart("D:\\ECNU\\紫外测试", "*.txt", false);//关闭监控
            form.button3.Enabled = true;//打开合成按钮
        }
                        public void stop(object source, ElapsedEventArgs e)
        {
            textBox1.AppendText("57\r\n");//停止指令
            Send();
        }

StreamWriter sw = File.AppendText(@"E:\\ak.txt");
//开始写入
sw.WriteLine(Text);
sw.Flush();
sw.Close();

改为

using (StreamWriter sw = File.AppendText(@"E:\\ak.txt"))
{
    sw.WriteLine(Text);
    sw.Flush();
}     

以确保流被关闭并释放,因为using最终会调用对象的Dispose方法

主要是这里
StreamWriter sw = File.AppendText(@"E:\ak.txt");
你定时器得到文件创建的时候,未必另一个程序写完了文件,或者另一个程序锁定了文件。此时无法读取。
如果另一个程序也是你写的,要及时关闭CloseHandle/Close/fclose等等。
然后你这里可以try...catch配合while循环不断重试