首页 > 其他分享 >FileSystemWatcher 局域网中大文件的内部传输共享和处理方案

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

时间:2023-04-23 20:01:13浏览次数:44  
标签:FileSystemWatcher string System 局域网 th new using 共享 NotifyFilters

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

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小时的文件

        }
    }
}

 

作者:兮去┓( ´∀` )┏博客
出处:https://www.cnblogs.com/bklsj/p/17347520.html
版权:本文版权归作者和博客园共有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任

标签:FileSystemWatcher,string,System,局域网,th,new,using,共享,NotifyFilters
From: https://www.cnblogs.com/bklsj/p/17347520.html

相关文章

  • 共治、共建、共享!龙蜥社区第 16 次运营委员会会议顺利召开!
    4月14日,龙蜥社区在凝思软件召开了第16次运营委员会会议,本次会议由统信软件运营委员会委员崔开主持。来自阿里云、红旗软件、海光、Intel、联通软研院、浪潮信息、凝思软件、普华基础软件、统信软件、万里红、新华三、中科方德等理事单位的23位委员及委员代表出席,请假7人。......
  • win10在访问共享的时候出现问题,异常代码为0x80004005的解决方案
    在使用win10访问网上邻居的共享时候,意外跳出异常:0x80004005,同时无法访问网上邻居的电脑。百度网上的解决方案,大都是搞什么注册dll的操作,如regsvr32softpub.dll,regsvr32wintrust.dll之类后来又找到网上的这个修改注册表的操作,说是应对访问共享时候遇到该代码的方案,尝试后问题......
  • windows连接ubuntu共享文件夹
    安装sambasudoaptinstallsamba编辑配置文件sudovim/etc/samba/smb.conf在末尾加入:[echohye]#smb用户path=/home/echohye/共享文件夹#共享路径available=yeswriteable=yessecurity=sharebrowseable=yesguestok=yes......
  • ubuntu共享电脑文件夹
    有多种方法可以将Windows文件传输到Ubuntu,以下是两种常见的方法:1.通过共享文件夹在你的Windows操作系统中,可以设置一个共享文件夹,然后在Ubuntu中通过网络访问和复制这个文件夹中的文件。要设置一个共享文件夹,在Windows中右击要共享的文件夹,选择"属性",然后选择"共享"......
  • Ubuntu 搭建一个局域网文件存储服务器
    在Ubuntu上搭建一个局域网文件存储服务器可以使用Samba服务,Samba是一个能够与Windows兼容的文件和打印机共享服务。下面是搭建过程:安装Samba服务打开终端,运行以下命令安装Samba:sqlCopycodesudoapt-getupdatesudoapt-getinstallsamba创建共享文件夹......
  • Ubuntu 解决mnt中有hgfs文件夹,但是无共享文件问题
    文章目录一、遇到问题二、尝试方法三、解决方法一、遇到问题最开始设置共享文件夹的时候是可以用的,但是隔天再开Ubuntu虚拟机的时候,就出现问题了。mnt中有hgfs文件夹,但是没有共享文件问题。二、尝试方法也去谷歌百度了解决方案,试了很多方法。但是都不奏效。weijian@ubuntu:/mnt/h......
  • AdGuard Home 排除对某个局域网 IP 的拦截
    最近装上了AdGuardHome来解决一下移X宽带惨不忍睹的DNS问题,但是发现网X云一直都没速度,手机APP也显示设备离线,登录后台一看,发现是AdGuard把迅X的API拦截了于是,在"过滤器-自定义过滤规则"里关掉对客户端192.168.10.4,也就是网X云的局域网IP的拦截@@||*.*^$clien......
  • Windows 与 虚拟机VirtualBox smb共享
    一、安装Samba更新系统:sudoaptupdatesudoaptupgrade安装Samba:sudoaptinstallsamba二、配置共享创建共享目录sudomkdir /samba_share允许用户访问,修改权限:sudochmod777 /samba_share三、配置Samba打开samba配置文件,sudovim /etc/samba/smb.conf按i进......
  • 共享库的问题
    1.新安装的程序运行时找不到动态库,若共享库安装到了/lib或/usr/lib那么需要执行以下ldconfig,ldconfig的作用是在默认搜索路径/lib和/usr/lib和动态库配置文件/etc/ld.so.conf所列的目录下搜索出动态链接库。2.若共享库安装到了/usr/local/lib下,那么执行ldconfig之前需要把......
  • 共享栈
    #include<iostream>#defineMAXSIZE10//定义共享栈的结构体typedefstruct{ intdata[MAXSIZE]; inttop,rear;}SqStack;//初始化共享栈,令首位索引分别指向栈头和栈尾boolinitStack(SqStack&S){ S.top=-1; S.rear=MAXSIZE;}//判断栈是否为空,若为空返回true......