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

文件夹监控:进程完成后删除对应.stl与.txt文件

解决方案

问题根源

  1. FileWatcher规则失效:你连续给watcher.Filter赋值,最后只有*.txt会生效,.stl文件的创建事件不会被捕获。
  2. 单文件路径记录限制:fullPath仅保存单个文件路径,进程退出后自然只能删除该文件。

1. 修复多扩展名监控逻辑

FileWatcher的Filter一次只能设置一个值,要同时监控.stl和.txt,有两种可行方式:

方式一:全局过滤+事件内判断扩展名

// 监听所有文件,在事件中筛选目标扩展名
watcher.Filter = "*.*";
watcher.NotifyFilter = NotifyFilters.FileName;
watcher.Created += OnFileCreated;
watcher.EnableRaisingEvents = true;

// 事件处理方法
private void OnFileCreated(object sender, FileSystemEventArgs e)
{
    string ext = Path.GetExtension(e.FullPath).ToLower();
    if (ext == ".stl" || ext == ".txt")
    {
        // 后续文件配对、启动进程逻辑
    }
}

方式二:创建两个独立的FileWatcher实例

// 监控.stl文件
var stlWatcher = new FileSystemWatcher
{
    Path = "你的监控文件夹路径",
    Filter = "*.stl",
    EnableRaisingEvents = true
};
stlWatcher.Created += OnFileCreated;

// 监控.txt文件
var txtWatcher = new FileSystemWatcher
{
    Path = "你的监控文件夹路径",
    Filter = "*.txt",
    EnableRaisingEvents = true
};
txtWatcher.Created += OnFileCreated;

2. 配对文件并批量删除

因为两个文件是同时添加的,它们应该拥有相同的文件名前缀(如part.stl和part.txt),可以通过前缀关联两个文件,待配对完成后再启动进程,进程退出时批量删除:

// 用字典保存文件名前缀对应的待处理文件集合(线程安全需加锁)
private static readonly Dictionary<string, List<string>> _pendingFiles = new Dictionary<string, List<string>>();
private static readonly object _lockObj = new object();

private void OnFileCreated(object sender, FileSystemEventArgs e)
{
    string fileNamePrefix = Path.GetFileNameWithoutExtension(e.FullPath);
    string filePath = e.FullPath;

    lock (_lockObj)
    {
        if (!_pendingFiles.ContainsKey(fileNamePrefix))
        {
            _pendingFiles[fileNamePrefix] = new List<string>();
        }
        _pendingFiles[fileNamePrefix].Add(filePath);

        // 检查是否两个文件都已到齐
        if (_pendingFiles[fileNamePrefix].Count == 2)
        {
            var targetFiles = _pendingFiles[fileNamePrefix];
            _pendingFiles.Remove(fileNamePrefix);

            // 启动进程
            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "你的进程执行路径",
                    Arguments = string.Join(" ", targetFiles) // 可将文件路径传给进程
                },
                EnableRaisingEvents = true
            };

            // 绑定退出事件,批量删除文件
            process.Exited += (s, args) =>
            {
                foreach (var file in targetFiles)
                {
                    if (File.Exists(file))
                    {
                        try
                        {
                            File.Delete(file);
                        }
                        catch (IOException)
                        {
                            // 处理文件被占用的情况,可添加重试逻辑
                        }
                    }
                }
                process.Dispose();
            };

            process.Start();
        }
    }
}

3. 额外注意事项

  • 若存在同名前缀的多组文件,字典的键值对逻辑可以保证每组文件独立处理。
  • 删除文件时建议捕获IOException,避免因文件被其他进程占用导致删除失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 19:35:15