文件名称:局域网通过共享文件的方式同步数据示例小程序
文件大小:33KB
文件格式:ZIP
更新时间:2015-05-03 06:23:11
局域网,通过共享文件的方式,同步数据,示例小程序
局域网通过共享文件的方式同步数据示例小程序
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));
}
}
}
}
}
}
///
【文件预览】:
SyncFilesRemoteShareFolder.sln
ConsoleApplication12
----SyncFilesRemoteShareFolder.csproj(2KB)
----Program.cs(9KB)
----Properties()
--------AssemblyInfo.cs(1KB)
----obj()
--------x86()
----bin()
--------Debug()
--------Release()
SyncFilesRemoteShareFolder.suo
ConsoleApplication12.gpState
SyncFilesRemoteShareFolder.gpState