您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 局域网通过共享文件的方式同步数据示例小程序
  所属分类: C#
  开发工具:
  文件大小: 33kb
  下载次数: 0
  上传时间: 2012-04-12
  提 供 者: dee****
 详细说明: 局域网通过共享文件的方式同步数据示例小程序 http://www.deepleo.com/archives/829 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Net.NetworkInformation; using System.Net; namespace SyncFilesRemoteShareFolder { class Program { static void Main(string[] args) { while (true) { Console.WriteLine(" ----------------------------------------"); Console.WriteLine(" Copyright(c) http:/ /www.deepleo.com"); Console.WriteLine("Please enter ip:"); string ip = Console.ReadLine(); Ping pingSender = new Ping(); IPAddress address = IPAddress.Parse(ip); PingReply reply = pingSender.Send(address, 2000);//ping ip, timeout=2s if (reply.Status != IPStatus.Success) { Console.WriteLine(string.Format("ping ip : {0} failed.", ip)); } else { Console.WriteLine(string.Format("ping ip : {0} success.", ip)); Console.WriteLine(@"please enter share folder, such as [Sample\deepleo.com]"); string remotepath = Console.ReadLine(); string sourceDir = string.Format(@"\\{0}\{1}", ip, remotepath); if (!Directory.Exists(sourceDir)) { Console.WriteLine(string.Format("The share folder is not existed.", remotepath)); } else { Console.WriteLine(string.Format("Get files form share folder: {0}, please wait....", sourceDir)); string[] sourceFiles = GetFiles(sourceDir); if (sourceFiles.Length == 0) { Console.WriteLine("None files."); } else { Console.WriteLine(string.Format("There have {0} files, do you want to see? Y/N", sourceFiles.Length)); if (Console.ReadLine().Trim().ToLower() == "y") { for (int i = 0; i < sourceFiles.Length; i++) { Console.WriteLine(sourceFiles[i]); } } Console.WriteLine("Do you want to copy? Y/N"); if (Console.ReadLine().Trim().ToLower() == "y") { string destinationFolder = @"C:\" + System.DateTime.Now.ToBinary().ToString(); if (!Directory.Exists(destinationFolder)) { Directory.CreateDirectory(destinationFolder); } int successCount = 0; int sumCount = sourceFiles.Length; System.Diagnostics.Stopwatch sp = new System.Diagnostics.Stopwatch(); sp.Start(); for (int i = 0; i < sumCount; i++) { string sourceFilePath = sourceFiles[i]; string sourceFileName = Path.GetFileName(sourceFilePath); string sourceFileDir = Path.GetDirectoryName(sourceFilePath); string desDir = sourceFileDir.Replace(sourceDir, destinationFolder); string desFile = Path.Combine(desDir, sourceFileName); bool isSuccess = RetryCopyFileOrDirectory(sourceFilePath, desFile); if (isSuccess) { Console.WriteLine(string.Format("{0}/{1} Success to copy file: {2}.", i + 1, sumCount, sourceFileName)); successCount++; } else { Console.WriteLine(string.Format("!{0}/{1} Failed to copy file: {2}", i + 1, sumCount, sourceFileName)); } } sp.Stop(); Console.WriteLine(string.Format("Success copy {0}/{1} files to {2}, spend {3} ms", successCount, sumCount, destinationFolder, sp.ElapsedMilliseconds)); } } } } } } /// ///之所以要单独写一个方法是因为 Directory.GetFiles经常会因为各种原因导长时间没有响应。 ///想做一个超时机制。便于以后扩展 /// /// /// public static string[] GetFiles(string sourceDir) { string[] sourceFiles = Directory.GetFiles(sourceDir, "*.*", SearchOption.AllDirectories); return sourceFiles; } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 输入关键字,在本站1000多万海量源码库中尽情搜索: