FileSystemWatcher 局域网中大文件的内部传输共享和处理方案
在不使用其他软件的情况下共享,且自动清理。
1、在服务器建了个临时文件夹共享,并且设置只可写入和读取,不可执行(删除)
2、写服务

源码附上
private void OnCreated(object sender, FileSystemEventArgs e)
{
if (AddNewFile.Exists(a => a == e.Name) == false)
{
AddNewFile.Add(e.Name);
}
//manual.Reset();
Waiting(e.FullPath);//等待文件创建完成
int execint = 0;
new Thread(() =>
{
do
{
try
{
DateTime dtNow = DateTime.Now;
File.SetLastWriteTime(e.FullPath, dtNow);
var fileinfo = new FileInfo(e.FullPath);
if (fileinfo.LastWriteTime == dtNow)
{
AddNewFile.Remove(e.Name);
break;
}
Thread.Sleep(1000);
execint = 10;
}
catch
{
}
} while (execint < 3);
}).Start();
}
private static List<string> AddNewFile = new List<string>();
private void Waiting(string path)
{
try
{
FileInfo fi;
fi = new FileInfo(path);
long len1, len2;
len2 = fi.Length;
do
{
len1 = len2;
Thread.Sleep(100);//等待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 (AddNewFile.Exists(a => a == fileinfo.Name))//不删除新增的这个文件,因为要改时间为最新
{
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(10000);
}
});
th.IsBackground = true;
th.Start();
new Thread(() =>
{
//定时清理AddNewFile
while (true)
{
string[] files = Directory.GetFiles(DirPath);
foreach (string filefull in files)
{
var fileinfo = new FileInfo(filefull);
if (AddNewFile.Exists(a => a == fileinfo.Name) == false)//不删除新增的这个文件,因为要改时间为最新
{
AddNewFile.Remove(fileinfo.Name);
}
}
Thread.Sleep(1800000);//30分钟清理
}
}).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
版权:本文版权归作者和博客园共有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任

浙公网安备 33010602011771号