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

咨询:如何用C#实现文件自动检测、复制及移动功能

嘿,我来帮你完善这两个C#文件操作的需求!咱们分两部分来解决,先搞定你提到的自动复制*exp.zip的功能(包括实时监控文件夹),再处理zip文件的自动移动逻辑。

解决方案:C#实现文件移动与自动复制监控

一、自动复制*exp.zip到Folder2(含实时文件夹监控)

你已经写了目标文件的检测逻辑,接下来咱们把复制功能补全,再加上实时监控,这样文件一放进主文件夹就能自动触发复制。

1. 完善文件复制逻辑

首先处理复制部分,得考虑目标文件夹已有同名文件的情况,这里给你做了覆盖的示例,要是你想保留原文件或者重命名,改改逻辑就行:

string sourceFolder = "你的主文件夹路径"; // 替换成你的实际路径
string targetFolder = @"C:\YourPath\Folder2"; // 替换成Folder2的实际路径

// 查找主文件夹里的*exp.zip文件
string[] files = System.IO.Directory.GetFiles(sourceFolder, "*exp.zip", System.IO.SearchOption.TopDirectoryOnly);
if (files.Length < 1)
{
    MessageBox.Show("No File");
}
else
{
    foreach (string file in files)
    {
        string fileName = System.IO.Path.GetFileName(file);
        string targetPath = System.IO.Path.Combine(targetFolder, fileName);
        
        // 复制文件,目标存在就覆盖
        System.IO.File.Copy(file, targetPath, overwrite: true);
        MessageBox.Show($"已复制文件:{fileName}");
    }
}

2. 添加实时文件夹监控(FileSystemWatcher)

要实现“文件一放入就自动复制”,得用FileSystemWatcher来监听文件夹的文件创建事件,代码如下:

// 初始化监控器
FileSystemWatcher folderWatcher = new FileSystemWatcher();
folderWatcher.Path = sourceFolder;
// 只监听文件创建事件
folderWatcher.Created += FolderFileCreated;
// 开启监控
folderWatcher.EnableRaisingEvents = true;

// 文件创建事件的处理方法
private void FolderFileCreated(object sender, FileSystemEventArgs e)
{
    // 等1秒再操作,避免文件还在写入时被占用(也可以用更精准的文件占用检测)
    System.Threading.Thread.Sleep(1000);
    
    // 检查是不是咱们要的*exp.zip文件
    if (System.IO.Path.GetFileName(e.FullPath).EndsWith("exp.zip", StringComparison.OrdinalIgnoreCase))
    {
        string targetPath = System.IO.Path.Combine(targetFolder, e.Name);
        try
        {
            System.IO.File.Copy(e.FullPath, targetPath, overwrite: true);
            MessageBox.Show($"自动复制完成:{e.Name}");
        }
        catch (Exception ex)
        {
            MessageBox.Show($"复制失败:{ex.Message}");
        }
    }
}

二、检查文件夹中的zip文件并自动移动

这个需求的逻辑和复制类似,只是把Copy换成Move就行,同样处理重名情况:

string zipSourceFolder = "要检查的文件夹路径"; // 替换成实际路径
string zipTargetFolder = "移动的目标文件夹路径"; // 替换成实际路径

// 查找所有zip文件
string[] zipFiles = System.IO.Directory.GetFiles(zipSourceFolder, "*.zip", System.IO.SearchOption.TopDirectoryOnly);
if (zipFiles.Length == 0)
{
    MessageBox.Show("未找到任何zip文件");
}
else
{
    foreach (string zipFile in zipFiles)
    {
        string fileName = System.IO.Path.GetFileName(zipFile);
        string targetPath = System.IO.Path.Combine(zipTargetFolder, fileName);
        
        // 如果目标文件夹已有同名文件,先删除再移动(要是不想覆盖,这里可以加判断跳过)
        if (System.IO.File.Exists(targetPath))
        {
            System.IO.File.Delete(targetPath);
        }
        System.IO.File.Move(zipFile, targetPath);
        MessageBox.Show($"已移动文件:{fileName}");
    }
}

要是你想保留原文件夹的zip文件,把Move换成Copy,再加上File.Delete(zipFile)就行。

一些额外提醒

  • 一定要确保程序有目标文件夹的读写权限,不然会抛出权限错误。
  • 要是你的程序需要长时间运行监控,最好加上错误处理,防止监控意外停止:
folderWatcher.Error += WatcherErrorHandler;

private void WatcherErrorHandler(object sender, ErrorEventArgs e)
{
    MessageBox.Show($"监控出错了:{e.GetException().Message}");
    // 这里可以重新开启监控
    ((FileSystemWatcher)sender).EnableRaisingEvents = true;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:39:57