?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace 同步文件監視測試
{
??? class Program
??? {
???????
??????? static string a = @"C:\Users\dell\Desktop\123";
??????? static string d = @"C:\Users\dell\Desktop\備份";
??????? //static string d = @"D:\備份";
??????? static DirectoryInfo fbeifen;
??????? static DirectoryInfo f123 = new DirectoryInfo(a);
??????? static? StreamReader rain;//不能放于循環體多次創建,要記住啊!!!
??????? static? StreamWriter saout;
??????? static? StringBuilder fff = new StringBuilder("");
??????? static FileSystemWatcher w;
??????? static void Main(string[] args)
??????? {
???????????
??????????? w = new FileSystemWatcher(a);
??????????? w.Changed += new FileSystemEventHandler(w_Changed);
??????????? //w.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.FileName;
??????????? w.EnableRaisingEvents = true;
??????????? Console.Read();
??????? }
??????? static void w_Changed(object sender, FileSystemEventArgs e)
??????? {
??????????? //此處可避免同時執行兩次
??????????? w.EnableRaisingEvents = false;
??????????? System.Threading.Thread.Sleep(1000);
??????????? w.EnableRaisingEvents = true;
??????????? fbeifen = new DirectoryInfo(d);//注意這里,不能只用一個對象
??????????? // 每次改變都會執行兩次不知到為什么
??????????????? if (!fbeifen.Exists)
??????????????? {
??????????????????? try
??????????????????? {
??????????????????????? fbeifen.Create();
??????????????????????? Console.WriteLine("開始創建備份時間" + DateTime.Now.Ticks.ToString());
??????????????????????? foreach (var item in f123.GetFiles())
??????????????????????? {
??????????????????????????? rain = new StreamReader(item.DirectoryName + @"\" + item.Name,Encoding.GetEncoding("gb2312"));
??????????????????????????? File.Create(d + @"\" + item.Name).Close();
??????????????????????????? saout = File.AppendText(d + @"\" + item.Name);
??????????????????????????? saout.Write(rain.ReadToEnd());
??????????????????????????? rain.Close();
??????????????????????????? saout.Close();
??????????????????????????? //System.Threading.Thread.Sleep(100);
??????????????????????? }
??????????????????????? Console.WriteLine("創建備份成功!");
??????????????????? }
??????????????????? catch (Exception ex)
??????????????????? {
??????????????????????? Console.WriteLine("創建備份失敗!" + ex.Message);
??????????????????? }
??????????????? }
??????????????? else
??????????????? {
??????????????????? try
??????????????????? {
??????????????????????? rain = new StreamReader(a + @"\" + e.Name,Encoding.GetEncoding("gb2312"));
??????????????????????? File.Create(d + @"\" + e.Name).Close();
??????????????????????? saout = File.AppendText(d + @"\" + e.Name);
??????????????????????? saout.Write(rain.ReadToEnd());
??????????????????????? rain.Close();
??????????????????????? saout.Close();
??????????????????????? Console.WriteLine(e.Name + "---文件更新備份成功!");
??????????????????? }
??????????????????? catch (Exception ex)
??????????????????? { Console.WriteLine("文件更新備份失敗:" + ex.Message); }
??????????????? }
?
??????? }
??? }
}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
