繁体   English   中英

如何使用Windows Service C#将文件从文件夹复制到另一个文件夹

[英]How to copy file from folder to another folder with Windows Service C#

如何使用Windows Service C#将文件从文件夹复制到另一个文件夹

我尝试使用此代码每30秒复制一次文件。 但是时间循环很好,因此无法完成复制。 因此,我使用Visual Studio2012。我将使用命令行安装Windows Service。 如何解决此代码。

using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Timers;
using System.IO;
using TestWindowService;

namespace TestCopy
{
    public partial class Copy : ServiceBase
    {
        private FileSystemWatcher watcher;
        private System.Timers.Timer timer1 = null;
        public Copy()
        {
           InitializeComponent(); 
        }

        protected override void OnStart(string[] args)
        {
            //File.Copy(@"C:\input\*.*", @"D:\output\*.*");
            Library.WriteErrorLog("Test window service started");
            timer1 = new System.Timers.Timer();
            this.timer1.Interval = 30000; //every 30 secs
            this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
            timer1.Enabled = true;
            watcher = new FileSystemWatcher();
            watcher.Path = "C:\\INPUT\\";
            watcher.Filter = "*.*";
            watcher.EnableRaisingEvents = true;
        }

        private void OnCreated(object sender, FileSystemEventArgs e)
        {
            String output_dir = "D:\\OUTPUT\\";
            String output_file = Path.Combine(output_dir, e.Name);
            File.Copy(e.FullPath, output_file);
            // File.Copy() works here
            Library.WriteErrorLog("File copy success");
        }

        private void timer1_Tick(object sender, ElapsedEventArgs e)
        {
            watcher.Created += new FileSystemEventHandler(OnCreated);                 
        }

        protected override void OnStop()
        {
            timer1.Enabled = false;
            Library.WriteErrorLog("Test window service stopped"); 
        }
    }
}

您要向FileSystemWatcher.Created添加处理程序。每30秒创建一次。

如果我理解正确,则希望每30秒执行一次复制。

using System;
using System.Collections.Concurrent;
using System.IO;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Timers;
using TestWindowService;

namespace TestCopy
{
    public partial class Copy : ServiceBase
    {
        private FileSystemWatcher watcher;
        private System.Timers.Timer timer;
        private ConcurrentQueue<FileInfo> lastCreated;

        public Copy()
        {
            InitializeComponent();

            timer = new System.Timers.Timer();
            timer.Interval = 30000; //every 30 secs
            timer.Elapsed += (s, e) => CopyFiles();

            watcher = new FileSystemWatcher();
            watcher.Path = "C:\\INPUT\\";
            watcher.Filter = "*.*";
            //schedule the file for copying on the next tick
            watcher.Created += (s, e) => lastCreated.Enqueue(new FileInfo(e.FullPath));
        }

        protected override void OnStart(string[] args)
        {
            timer.Enabled = true;
            watcher.EnableRaisingEvents = true;

            Library.WriteErrorLog("Test window service started");
        }

        private void CopyFiles()
        {
            String output_dir = "D:\\OUTPUT\\";

            FileInfo fi;
            while (lastCreated.TryDequeue(out fi))
                if (fi.Exists)
                    fi.CopyTo(Path.Combine(output_dir, fi.Name));

            Library.WriteErrorLog("File copy success");
        }

        protected override void OnStop()
        {
            watcher.EnableRaisingEvents = false;
            timer.Enabled = false;

            //empty the queue
            FileInfo fi;
            while (!lastCreated.IsEmpty)
                lastCreated.TryDequeue(out fi);

            Library.WriteErrorLog("Test window service stopped");
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM