实时监控文件变化以及处理xml(仅用作笔记用,防止以后要用)
private static void WatcherStrat(string path, string filter)
{
try
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
watcher.Filter = filter;
watcher.Changed += new FileSystemEventHandler(OnProcess);
watcher.Created += new FileSystemEventHandler(OnProcess);
watcher.Deleted += new FileSystemEventHandler(OnProcess);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.EnableRaisingEvents = true;
watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess
| NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
watcher.IncludeSubdirectories = true;
}
catch (Exception ex)
{
throw;
}
}
private static void OnProcess(object source, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Created)
{
OnCreated(source, e);
}
else if (e.ChangeType == WatcherChangeTypes.Changed)
{
OnChanged(source, e);
}
else if (e.ChangeType == WatcherChangeTypes.Deleted)
{
OnDeleted(source, e);
}
}
private static void OnCreated(object source, FileSystemEventArgs e)
{
try
{
if (!Directory.Exists(Directory.GetCurrentDirectory() + "\\Uploads\\Decompression\\" + e.Name.Replace(".zip", "")))
{
Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Uploads\\Decompression\\" + e.Name.Replace(".zip", ""));
}
System.IO.Compression.ZipFile.ExtractToDirectory(e.FullPath, Directory.GetCurrentDirectory() + "\\Uploads\\Decompression\\" + e.Name.Replace(".zip", ""), Encoding.UTF8, false); //解压
string path = Directory.GetCurrentDirectory() + "\\Uploads\\Decompression\\" + e.Name.Replace(".zip", "");
DirectoryInfo root = new DirectoryInfo(path);
FileInfo[] files = root.GetFiles();
string filepath = string.Empty;
foreach (var item in files)
{
if (item.Name.ToLower().Contains(".xml"))
{
filepath = files[0].FullName;
}
}
XmlDocument xmlDoc = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true; //忽略文档里面的注释D:\Xcangdan\YsSoa.Api\Uploads\Receipt\HUIZHI\FFִ.xml
XmlReader reader = XmlReader.Create(filepath, settings);
xmlDoc.Load(reader);
foreach (XmlElement book in xmlDoc.SelectNodes(@"Manifest/Response/BorderTransportMeans"))
{
Console.WriteLine("id value: {0}.", book.GetAttribute("id"));
FileStream fs = new FileStream(@"C:\Users\WangXinChun\Desktop\create.txt", FileMode.Create);
//获得字节数组
byte[] data = System.Text.Encoding.Default.GetBytes(book.SelectNodes("JourneyID")[0].InnerText + "\r\n" + book.SelectNodes("ID")[0].InnerText + "\r\n" + xmlDoc.SelectNodes(@"Manifest/Response/ResponseType")[0].SelectNodes("Code")[0].InnerText + "\r\n" + xmlDoc.SelectNodes(@"Manifest/Response/ResponseType")[0].SelectNodes("Text")[0].InnerText);
//开始写入
fs.Write(data, 0, data.Length);
//清空缓冲区、关闭流
fs.Flush();
fs.Close();
}
reader.Dispose();
reader.Close();
DeleteFile(path);
DeleteFile(e.FullPath);
Console.WriteLine("文件新建事件处理逻辑 {0} {1} {2}", e.ChangeType, e.FullPath, e.Name);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 删除文件信息
/// </summary>
/// <param name="path"></param>
public static void DeleteFile(string path)
{
FileAttributes attr = File.GetAttributes(path);
if (attr == FileAttributes.Directory)
{
Directory.Delete(path, true);
}
else
{
File.Delete(path);
}
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("文件改变事件处理逻辑{0} {1} {2}", e.ChangeType, e.FullPath, e.Name);
}
private static void OnDeleted(object source, FileSystemEventArgs e)
{
Console.WriteLine("文件删除事件处理逻辑{0} {1} {2}", e.ChangeType, e.FullPath, e.Name);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine("文件重命名事件处理逻辑{0} {1} {2}", e.ChangeType, e.FullPath, e.Name);
}
实时监控文件变化以及处理xml(仅用作笔记用,防止以后要用)的更多相关文章
- JDK 之 NIO 2 WatchService、WatchKey(监控文件变化)
JDK 之 NIO 2 WatchService.WatchKey(监控文件变化) JDK 规范目录(https://www.cnblogs.com/binarylei/p/10200503.html ...
- Python监控文件变化:watchdog
Python监控文件变化有两种库:pyinotify和watchdog.pyinotify依赖于Linux平台的inotify,后者则对不同平台的的事件都进行了封装.也就是说,watchdog跨平台. ...
- mac 监控文件变化并重启php
自己撸一个框架,需要监控代码变化 安装fswatch brew install fswatch shell重启PHP脚本reload.sh #!/bin/sh do ps -ef | grep php ...
- 使用gulp 合并压缩打包,实时监控文件,实现本地server
今天不讲webpack,就说说gulp是怎么进行压缩合并打包 首先你的安装gulp : npm install gulp -g --save-dev 然后最基本的你因该知道gulp 的四个方法, gu ...
- inotify监控文件变化
1.安装inotify-tools yum install make gcc gcc-c++ #安装编译工具 inotify-tools下载地址:http://github.com/downloa ...
- 分享Grunt.js配置: watch + liveReload 实时监测文件变化自动刷新浏览器
http://www.tuicool.com/articles/2eaQJn 用Grunt配置watch和liveReload组件,可以实时检测指定的文件变化并自动刷新浏览器.目前基本已经成为了我的必 ...
- linux 监控文件变化
介绍 有时候我们常需要当文件变化的时候便触发某些脚本操作,比如说有文件更新了就同步文件到远程机器.在实现这个操作上,主要用到两个工具,一个是rsync,一个是inotifywait .inotifyw ...
- inotifywait命令如何监控文件变化?
转载自:https://segmentfault.com/a/1190000038351925 文件监控可以配合rsync实现文件自动同步,例如监听某个目录,当文件变化时,使用rsync命令将变化的文 ...
- Gulp-前端进阶A-3---如何不刷新监控文件变化?
npm install --save-dev gulp-connect npm install --save-dev gulp-livereload npm其他,前面已有 var gulp = req ...
随机推荐
- Spring中pom文件所需节点
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...
- Python文件的几种读写方式
1). "w "写模式,它是不能读的,如果用w模式打开一个已经存在的文件,会清空以前的文件内容,重新写 "w+ "是读写内容,只要沾上w,肯定会清空原来的文件2 ...
- [JavaScript] es6规则总结
let 和 const let 是块级作用域 let 声明的变量只在其所在的作用域内生效 <script> { var today = "周3"; let yester ...
- PHP之面试题总结
总结一些面试题,有备无患,走起... 1.熟悉的 nosql 和 sql 有什么区别(优势,劣势) Memcache,Redis 都是内存数据库 redis是一个开源的支持多种数据类型的key=> ...
- 【应用容器引擎】Docker笔记
一.Docker是什么? Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的linux机器上,也可以实现虚拟化.它是一个轻量级容器技 ...
- SpringBoot02——A Simple SpringBoot Project&Hot Deployment
1.简单的Controller映射 1.新建一个controller包,包一定在启动器的下一层级 2.可以在application.properties中进行调整端口和context-path参数 s ...
- Linux 安装 nginx 安装PCRE库
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库.这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是 ...
- Linux(Centos7)下redis5缓存服务集群分布式搭建
注意:可以查看Redis官网查看集群搭建方式,连接如下 https://redis.io/topics/cluster-tutorial 集群中应该至少有三个节点,每个节点有一备份节点.需要6台服务器 ...
- Spring Boot JPA - Querydsl
https://lufficc.com/blog/spring-boot-jpa-querydsl
- 【SaltStack官方版】—— STORING JOB RESULTS IN AN EXTERNAL SYSTEM
STORING JOB RESULTS IN AN EXTERNAL SYSTEM After a job executes, job results are returned to the Salt ...