实时监控文件变化以及处理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 ...
随机推荐
- oracle ojdbc 版本须对应,否则日期字段查询结果与实际值可能不一致
1. 数据库版本:select * from v$version; 2. 版本对应:
- numpy中的快速的元素级数组函数
numpy中的快速的元素级数组函数 一元(unary)ufunc 对于数组中的每一个元素,都将元素代入函数,将得到的结果放回到原来的位置 >>> import numpy as np ...
- C语言--- 高级指针2(结构体指针,数组作为函数参数)
一.结构体指针 1. 什么是结构体指针?指向结构体变量的指针 结构体: typedef struct stu{ char name[ ...
- IDEA的快捷方式
一,IDEA的快捷方式1,F8单步执行 2,F9运行调试 3,CTRL +鼠标左键=进入查看定义 4,CTRL+alt +鼠标左键=查看实现 5,Shift+F6重命名 6,alt +intsert= ...
- log4net日志输出配置即输出到文件又输出到visual studio的output窗口
<configuration> <configSections> <section name="log4net" type="log4net ...
- python 绘制对象检测框及中文信息标注
# 坐标顺序: 上->左->下->右 def draw_bounding_box_on_image(image, ymin, xmin, ymax, xmax, color='red ...
- Win10+Linux(CentOS) 双系统安装教程--踩坑实录
最近心血来潮想给自己的笔记本装一套linux系统作为开发环境, 说干就干,首先先收集一下现在linux主流版本, 貌似现在市场上应用服务器比较多的是redhat相关产品,而ubuntu的优势在于它庞大 ...
- PAT Basic 1071 小赌怡情 (15 分)
常言道“小赌怡情”.这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注 t 个筹码后,计算机给出第二个数.若玩家猜对了,则系统奖励玩家 t 个 ...
- Jsoup抓取网页数据完成一个简易的Android新闻APP
前言:作为一个篮球迷,每天必刷NBA新闻.用了那么多新闻APP,就想自己能不能也做个简易的新闻APP.于是便使用Jsoup抓取了虎扑NBA新闻的数据,完成了一个简易的新闻APP.虽然没什么技术含量,但 ...
- 调试dcc 试图将u-boot放入ocm运行碰到的问题
1. 起因: gd->mon_len = (ulong)&__bss_end - (ulong)_start; 在u-boot.map中查找,发现__bss_end并不是u-boot.b ...