微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用FileSystemWatcher创建文件后将文件复制到另一个文件夹-C#

如何解决使用FileSystemWatcher创建文件后将文件复制到另一个文件夹-C#

C#的新增功能-我需要构建一个控制台应用程序以监视Windows 2019 Server上的一组文件夹,并使用相同的文件名将所有新创建的.txt文件复制到另一个文件夹。我需要copy方法的帮助。这是我到目前为止所拥有的:

using System;
using System.IO;

namespace Folderwatch
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourcePath = @"C:\Users\me\Documents\SomeFolder";

            FileSystemWatcher watcher = new FileSystemWatcher(sourcePath);

            watcher.EnableRaisingEvents = true;
            watcher.IncludeSubdirectories = true;
            watcher.Filter = "*.log";


            // Add event handlers.
            watcher.Created += new FileSystemEventHandler(OnCreated);


            // Wait for the user to quit the program.
            Console.WriteLine("Monitoring File System Activity on {0}.",sourcePath);
            Console.WriteLine("Press \'q\' to quit.");
            while (Console.Read() != 'q') ;

        }

        // Define the event handlers. 

        private static void OnCreated(object source,FileSystemEventArgs e)
        {
            // Specify what is done when a file is created.
            string destPath = @"C:\Users\pdeeb\Desktop\Temp";
            string sourceFile = ??
            string destFile = Path.Combine(destPath,sourceFile);
            File.copy(sourceFile,destFile,true);
        }

    }
}

我觉得我可能使一个简单的任务复杂化了-我只想将创建的文件复制到具有相同文件名的destPath并允许覆盖。我应该如何定义sourceFile变量,或者使用这种方法我该走在正确的轨道上?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。