You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#调用批处理后函数回跳致文件读取错误问题求助

问题描述

我正在编写C#函数,功能是结束当前运行进程后调用批处理文件。该批处理负责关闭BDE.exe并清空CheckUpdate.txt内容。此函数由txt文件变更事件触发,触发后读取文件内数字并执行对应逻辑。但调用批处理后程序未直接关闭,函数跳回读取txt文件的步骤,此时文件已被清空,引发错误。

相关代码

C# 文件变更事件处理函数

private void OnChangedUpdate(object sender, FileSystemEventArgs e)
{
    string[] lines = new string[1];            
    lines = System.IO.File.ReadAllLines(@"Dateien/DB/Status/CheckUpdate.txt");
    if (lines[0] == "1") // Update um 23:30 ausführen
    {
        TimerStartUpdate();
    }  
    else if (lines[0] == "2")   // Update direkt ausführen
    {
        OnTimedEventUpdateDirect();
        return;
    }
    else
    {                
        MessageBox.Show("Updated failed. Please contact supervisor");
    }
}

C# 立即更新处理函数

private void OnTimedEventUpdateDirect()
{
    // Prozess beenden
    Processfinish_Update();
    // Alles schreiben
    // Bat Datei öffnen
    Process P = new Process();
    P.StartInfo.FileName = "UpdateBDE.bat";
    P.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
    P.Start();

    // Theorie BDE wieder aufmachen in Batdatei
}

批处理文件代码

TASKKill /IM BDE.exe
echo.>Dateien/DB/Status/CheckUpdate.txt
问题根源与修复方案

问题根源

  1. 批处理清空CheckUpdate.txt时,会再次触发文件变更事件,此时文件内容为空,读取lines[0]会引发索引越界错误
  2. 当前进程未及时退出,导致事件处理函数继续执行,进而触发后续错误

修复步骤

1. 临时禁用文件监听

在处理文件变更事件时,先禁用文件监听,避免批处理修改文件时重复触发事件,处理完成后再恢复监听。

2. 强制当前进程退出

调用批处理后,立即强制当前进程退出,防止函数跳回读取文件的步骤。

3. 增加异常与空文件处理

读取文件时增加空文件判断和异常捕获,避免崩溃。

修改后的代码

更新后的文件变更事件处理函数

private void OnChangedUpdate(object sender, FileSystemEventArgs e)
{
    var watcher = sender as FileSystemWatcher;
    // 临时禁用事件触发
    watcher.EnableRaisingEvents = false;

    try
    {
        string[] lines = System.IO.File.ReadAllLines(@"Dateien/DB/Status/CheckUpdate.txt");
        // 空文件直接返回,避免索引越界
        if (lines.Length == 0) return;

        if (lines[0] == "1") // 23:30执行更新
        {
            TimerStartUpdate();
        }  
        else if (lines[0] == "2")   // 立即执行更新
        {
            OnTimedEventUpdateDirect();
        }
        else
        {                
            MessageBox.Show("更新失败,请联系管理员");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show($"处理更新时出错:{ex.Message}");
    }
    finally
    {
        // 恢复事件触发
        watcher.EnableRaisingEvents = true;
    }
}

更新后的立即更新处理函数

private void OnTimedEventUpdateDirect()
{
    // 结束当前进程
    Processfinish_Update();

    // 启动批处理
    Process P = new Process();
    P.StartInfo.FileName = "UpdateBDE.bat";
    P.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
    P.Start();

    // 强制当前进程退出,避免后续代码执行
    Environment.Exit(0);
}

优化后的批处理文件

增加强制关闭进程和等待逻辑,确保BDE.exe完全关闭后再清空文件:

REM 强制关闭BDE.exe进程
TASKKill /IM BDE.exe /F
REM 等待1秒确保进程彻底终止
timeout /t 1 /nobreak >nul
REM 清空CheckUpdate.txt内容
echo.>Dateien/DB/Status/CheckUpdate.txt

内容的提问来源于stack exchange,提问作者user20356767

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 22:05:29