直接来自MSDN教程:
string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);
是否有一种简单的方法可以下载文件夹及其文件的子文件夹?我知道如何使用dos来做到这一点,但你如何在C#中做到这一点?必须有一个简单的公式。
using System.Net;
WebClient webClient = new WebClient();
webClient.DownloadFile("http://www.website.com/tools/update/*.*", @"c:\downloads");
有点像做:
xcopy source destination /E
但是来自网络目录
答案 0 :(得分:1)
在我搜索您的案例时,您可能的赌注是使用WGET
命令行工具。你可以从here获得它。
使用this来引用递归下载。
使用WGET的样本;
ProcessStartInfo startInfo = new ProcessStartInfo("CMD.exe");
Process p = new Process();
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
p = Process.Start(startInfo);
p.StandardInput.WriteLine(@"wget -r http://www.website.com/tools/update");
p.StandardInput.WriteLine(@"EXIT");
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
p.Close();