FileSystemWatcher 局域网中大文件的内部传输共享和处理方案

发布时间 2023-04-23 19:56:29作者: 兮去

在不使用其他软件的情况下共享,且自动清理。

1、在服务器建了个临时文件夹共享,并且设置只可写入和读取,不可执行(删除)

2、写服务

源码附上

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;
using System.Threading;
using Microsoft.Win32.SafeHandles;

namespace ClearFile
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }
        //这个方法用来调试的
        internal void TestStartupAndStop(string[] args)
        {
            this.OnStart(args);
        }
        Thread th = null;
        private static string DirPath = "D:\\安装包\\临时";
        private void OnCreated(object sender, FileSystemEventArgs e)
        {
            AddNewFile = e.Name;
            Waiting(e.FullPath);//等待文件创建完成
            try
            {
                File.SetLastWriteTime(e.FullPath, DateTime.Now);
            }
            catch (Exception ex)
            {

            }
            finally {
                AddNewFile = "";
            }
            
        }
        private string AddNewFile = "";
        private void Waiting(string path)
        {
            try
            {
                FileInfo fi;
                fi = new FileInfo(path);
                long len1, len2;
                len2 = fi.Length;
                do
                {
                    len1 = len2;
                    Thread.Sleep(1000);//等待1秒钟
                    fi.Refresh();//这个语句不能漏了
                    len2 = fi.Length;
                } while (len1 < len2);
            }
            catch { }
        }
    
        protected override void OnStart(string[] args)
        {
            FileSystemWatcher watcher = new FileSystemWatcher(DirPath);
            watcher.NotifyFilter = NotifyFilters.Attributes
                                     | NotifyFilters.CreationTime
                                     | NotifyFilters.DirectoryName
                                     | NotifyFilters.FileName
                                     | NotifyFilters.LastAccess
                                     | NotifyFilters.LastWrite
                                     | NotifyFilters.Security
                                     | NotifyFilters.Size;
            watcher.Created += OnCreated;
            watcher.Filter = "*.*";
            watcher.IncludeSubdirectories = true;//监控子文件夹
            watcher.EnableRaisingEvents = true;
            th = new Thread(() =>
            {
                while (true)
                {
                    string[] files = Directory.GetFiles(DirPath);
                    foreach (string filefull in files)
                    {
                        var fileinfo = new FileInfo(filefull);
                        if (fileinfo.Name== "此临时文件夹超过8小时自动清理.txt")//给其他人提示的Readme不删
                        {
                            continue;
                        }
                        if (fileinfo.Name== AddNewFile)//不删除新增的这个文件,因为要改时间为最新
                        {
                            continue;
                        }
                        TimeSpan ts = DateTime.Now - fileinfo.LastWriteTime;
                        string time = ts.TotalHours.ToString();   //将时间差转换为秒
                        if (ts.TotalHours > 8)
                        {
                            try
                            {
                                if(File.Exists(filefull))
                                File.Delete(filefull);
                            }
                            catch (Exception)
                            {
                                throw;
                            }
                        }
                    }
                    Thread.Sleep(1000);
                }
            });
            th.IsBackground = true;
            th.Start();
#if DEBUG
      while (true) { }
#endif

        }

        protected override void OnStop()
        {
            if (th != null && th.ThreadState != System.Threading.ThreadState.Aborted) { th.Abort(); }
        }
    }
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace ClearFile
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main()
        {

#if DEBUG
            Service1 service1 = new Service1();
            service1.TestStartupAndStop(new string[0]);
            return;
#endif
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new Service1()
            };
            ServiceBase.Run(ServicesToRun);//用于自动清理超8小时的文件

        }
    }
}