C# 远程访问并复制文件
2013-09-08 11:45
966 查看
以下代码可以实现将远程主机上的共享文件复制到本地。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace FileCopyFromRemoteComputer
{
class Program
{
static void Main(string[] args)
{
if (Ping("10.71.1.67"))
{
string remote = @"\\10.71.1.67\gongxiang222";
string aimPath = @"D:\copytest";
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar) //给目标路径添加\\
aimPath += Path.DirectorySeparatorChar;
if (!Directory.Exists(aimPath))
//如果目标文件夹不存在,则创建
Directory.CreateDirectory(aimPath);
string[] fileList = Directory.GetFileSystemEntries(remote);
//获取远程路径下的所有文件列表
DateTime dtStart = DateTime.Now;
//开始复制的时间
foreach (string file in fileList)
{
File.Copy(file, aimPath + Path.GetFileName(file), true);
//复制文件到目标路径
}
DateTime dtEnd = DateTime.Now;
//结束时间
int time = dtEnd.Subtract(dtStart).Seconds;
//耗时统计
Console.WriteLine(time);
Console.ReadKey();
}
}
public static bool Ping(string remoteHost)
//Ping远程主机
{
bool Flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string dosLine = @"ping -n 1 " + remoteHost;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (proc.HasExited == false)
{
proc.WaitForExit(500);
}
string pingResult = proc.StandardOutput.ReadToEnd();
if (pingResult.IndexOf("(0% loss)") != -1)
{
Flag = true;
}
proc.StandardOutput.Close();
}
catch (Exception ex)
{
}
finally
{
try
{
proc.Close();
proc.Dispose();
}
catch
{
}
}
return Flag;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace FileCopyFromRemoteComputer
{
class Program
{
static void Main(string[] args)
{
if (Ping("10.71.1.67"))
{
string remote = @"\\10.71.1.67\gongxiang222";
string aimPath = @"D:\copytest";
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar) //给目标路径添加\\
aimPath += Path.DirectorySeparatorChar;
if (!Directory.Exists(aimPath))
//如果目标文件夹不存在,则创建
Directory.CreateDirectory(aimPath);
string[] fileList = Directory.GetFileSystemEntries(remote);
//获取远程路径下的所有文件列表
DateTime dtStart = DateTime.Now;
//开始复制的时间
foreach (string file in fileList)
{
File.Copy(file, aimPath + Path.GetFileName(file), true);
//复制文件到目标路径
}
DateTime dtEnd = DateTime.Now;
//结束时间
int time = dtEnd.Subtract(dtStart).Seconds;
//耗时统计
Console.WriteLine(time);
Console.ReadKey();
}
}
public static bool Ping(string remoteHost)
//Ping远程主机
{
bool Flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string dosLine = @"ping -n 1 " + remoteHost;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (proc.HasExited == false)
{
proc.WaitForExit(500);
}
string pingResult = proc.StandardOutput.ReadToEnd();
if (pingResult.IndexOf("(0% loss)") != -1)
{
Flag = true;
}
proc.StandardOutput.Close();
}
catch (Exception ex)
{
}
finally
{
try
{
proc.Close();
proc.Dispose();
}
catch
{
}
}
return Flag;
}
}
}
相关文章推荐
- C#访问远程服务器共享目录下载文件到本地
- [转]C# FTP,GetResponse(),远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件)
- C#操作FTP报错,远程服务器返回错误:(550)文件不可用(例如,未找到文件,无法访问文件)的解决方法
- C# FTP,GetResponse(),远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件)
- C#远程共享文件路径访问
- C# FTP,GetResponse(),远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件)
- C# FTP远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件)
- C# 连接远程共享文件及文件复制
- c#net无法将文件 复制到 对路径访问被拒绝或源文件 未能打开(未指定的错误)
- C# FTP,GetResponse(),远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件)
- C#文件复制小工具
- web服务器访问远程主机文件问题
- C# 文件夹以及文件复制
- c#查看远程文件是否存在
- java远程访问局域网内其他机器的共享文件
- Linux复制远程文件到本地命令
- PHP远程访问文件
- C# 获取文件的修改时间、访问时间、创建时间
- [C#] C#复制文件和文件夹
- C#添加文件和文件夹访问用户并设定权限