实时监控文件变化以及处理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 ...
随机推荐
- Office批量授权(VL)版本和激活方法
Office 2010 Office 2010中文专业增强版 32位 文件名: SW_DVD5_Office_Professional_Plus_2010w_SP1_W32_ChnSimp_CORE_ ...
- python3抓取中国天气网不同城市7天、15天实时数据
思路:1.根据city.txt文档来获取不同城市code2.获取中国天气网7d和15d不同城市url3.利用requests库请求url获取html内容4.利用beautifulsoup获取7d和15 ...
- 初识MySQL <一>
创建一个 表 create table student( id int(10) not null unique auto_increment primary key, name varchar(30) ...
- java复习(3)继承
一.继承为题的提出 ---------------------------------------------------- 我们知道面向对象的三大特性是:封装.继承和多态,可以知道继承在java应用 ...
- String转int,int转String
String转int 1) int i = Integer.parseInt([String]); int i = Integer.parseInt([String],[int radix]); 2 ...
- jQuery中outerWidth()方法
截图自:菜鸟教程https://www.runoob.com/jquery/html-outerwidth.html
- 81. Search in Rotated Sorted Array II (JAVA)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- Java中的==和equals( )方法
在Java中,equals和==都是用于检测两个字符串是否相等,返回类型也都是boolean值,但是二者内部处理却不一样. ==与equals( ) ==在Java中是一个二元操作符,用于比较原生类型 ...
- 一文看懂HttpServletResponse
https://www.jianshu.com/p/8bc6b82403c5 Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的resp ...
- Linux--操作系统基础及基础命令--01
一.系统基础 1.三大部件: CPU:运算器.控制器.存储器 内存:CPU的数据只能从内存中读取,且内存数据是易失性的(页面) IO: 控制总线.数据总线 2.OS的管理 GUI:图形用户界面 GNO ...