登录 注册
当前位置:主页 > 资源下载 > 10 > 示例小程序通过局域网共享文件的方式实现数据同步

示例小程序通过局域网共享文件的方式实现数据同步

  • 更新:2024-12-15 08:41:44
  • 大小:33KB
  • 推荐:★★★★★
  • 来源:网友上传分享
  • 类别:C# - 后端
  • 格式:ZIP

资源介绍

局域网通过共享文件的方式同步数据示例小程序 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; }