重新连接后网络服务不起作用

时间:2019-12-10 00:21:09

标签: c# .net windows-services sharpdevelop

有一个用于监视网络文件夹的服务,它可以正确执行其功能,但有一个细微差别:如果网络断开连接,则在恢复连接后,该服务将停止执行其功能,或者该服务处于关闭状态。关闭。它仅解决重启问题。有人可以告诉我如何监控连接吗? (通过ping尝试过,但结果相同)。

代码

namespace CopyCema
{
    public class CopyCema : ServiceBase
    {

        public const string MyServiceName = "CopyCema";

        private void InitializeComponent()
        {
            this.ServiceName = MyServiceName;
        }

        Logger logger;

        public CopyCema()
        {
            InitializeComponent();
            this.CanStop = true;
            this.CanPauseAndContinue = true;
            this.AutoLog = true;
            this.CanPauseAndContinue = true;
        }

        protected override void OnStart(string[] args)
        {   
            logger = new Logger();
            Thread loggerThread = new Thread(new ThreadStart(logger.Start));
            loggerThread.Start();

        }

        protected override void OnStop()
        {
            logger.Stop();
            Thread.Sleep(1000);
        }
    }

    class Logger
    {
        FileSystemWatcher watcher;
        object obj = new object();
        bool enabled = true;

        public string _Day()
        {
            string DTime = "";
            var date = DateTime.Now;
            int dayOfWeek = (int)date.DayOfWeek;
            if (dayOfWeek == 1)
            {
                DTime = DateTime.Now.AddDays(-3).ToShortDateString().Replace("2019", "19");
            }
            else
            {
                DTime = DateTime.Now.AddDays(-1).ToShortDateString().Replace("2019", "19");
            }
            return DTime;
        }

        public Logger()
        {

                string NetDir = @"\\<PC in LAN>\backup\"+_Day();
                string DestDir = @"D:\backupchik\"+_Day();

                DirectoryInfo NetDirInfo = new DirectoryInfo(NetDir);

                if (!NetDirInfo.Exists)
                {
                    NetDirInfo.Create();
                }

                string[] Files = Directory.GetFiles(NetDir);

                if (Files.Length!=0)
                {
                    DirectoryInfo DDirInfo = new DirectoryInfo(DestDir);

                    if (!DDirInfo.Exists)
                    {
                        DDirInfo.Create();
                    }

                    foreach(string f in Files)
                    {
                        FileInfo FInfo = new FileInfo(DestDir + @"\" + Path.GetFileName(f));
                        if (!FInfo.Exists)
                        {
                            File.Copy(f, DestDir + @"\" + Path.GetFileName(f));
                        }

                    }

                }

                watcher = new FileSystemWatcher(@"\\<PC in LAN>\backup\"+_Day());
                watcher.Deleted += Watcher_Deleted;
                watcher.Created += Watcher_Created;
                watcher.Changed += Watcher_Changed;
                watcher.Renamed += Watcher_Renamed;


        }

        public void Start()
        {
            watcher.EnableRaisingEvents = true;
            while(enabled)
            {
                Thread.Sleep(1000);
            }
        }
        public void Stop()
        {
            watcher.EnableRaisingEvents = false;
            enabled = false;
        }
        // переименование файлов
        private void Watcher_Renamed(object sender, RenamedEventArgs e)
        {
            string fileEvent = "переименован в " + e.FullPath;
            string filePath = e.OldFullPath;
            RecordEntry(fileEvent, filePath);
        }
        // изменение файлов
        private void Watcher_Changed(object sender, FileSystemEventArgs e)
        {
            string fileEvent = "изменен";
            string filePath = e.FullPath;
            RecordEntry(fileEvent, filePath);
        }
        // создание файлов
        private void Watcher_Created(object sender, FileSystemEventArgs e)
        {

            string fileEvent = "создан";
            string filePath = e.FullPath;
            RecordEntry(fileEvent, filePath);

            string sourceDir = @"\\<PC in LAN>\backup\"+_Day();
            string DDir = @"D:\backupchik\"+_Day();

            Thread.Sleep(2000);
            Task Tsk1 = new Task( () => copyD(sourceDir, DDir));
            Tsk1.Start();
        }

        public void copyD(string source0, string source2)
        {
            string[] files = Directory.GetFiles(source0);
            DirectoryInfo DDirInfo = new DirectoryInfo(source2);
            string destDir = "";

            if (!DDirInfo.Exists)
            {
                DDirInfo.Create();
            }

            destDir = @"D:\backupchik\"+_Day();

            foreach(string str in files)
            {   
                FileInfo Finfo = new FileInfo(destDir + @"\" + Path.GetFileName(str));
                FileInfo FinfoS = new FileInfo(str);
                if (!Finfo.Exists)
                {
                    if (FinfoS.Exists)
                    {
                        File.Copy(str, destDir + @"\" + Path.GetFileName(str), true);
                    }
                }
            }


        }

        // удаление файлов
        private void Watcher_Deleted(object sender, FileSystemEventArgs e)
        {
            string fileEvent = "удален";
            string filePath = e.FullPath;
            RecordEntry(fileEvent, filePath);
        }

        private void RecordEntry(string fileEvent, string filePath)
        {
            lock (obj)
            {

                    using (StreamWriter writer = new StreamWriter(@"C:\Users\111\Desktop\templog.txt", true))   
                    {
                        writer.WriteLine(String.Format("{0} файл(папка) {1} был(а) {2}", 
                            DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"), filePath, fileEvent));
                        writer.Flush();
                    }


            }

        }

    }

}

0 个答案:

没有答案