实时监控文件变化以及处理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);
}

posted @ 2019-03-01 15:46  Zero77  阅读(348)  评论(0编辑  收藏  举报