将文件夹从服务器复制到本地目录
我正在开发用于在C#中下载网站的软件,但在将文件夹从服务器复制到本地目录时遇到了一些麻烦。为此目的,我正在执行以下代码;将文件夹从服务器复制到本地目录
public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
{
try
{
foreach (DirectoryInfo dir in source.GetDirectories())
CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
foreach (FileInfo file in source.GetFiles())
file.CopyTo(Path.Combine(target.FullName, file.Name));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Form2", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
而且函数调用
private void button4_Click(object sender, EventArgs e)
{
try
{
CopyFilesRecursively(new DirectoryInfo(@"https:facebook.com"), new DirectoryInfo(@"G:\Projects\"));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Form2", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
消息框显示“指定的路径格式不支持。”
我们大家都知道,所有托管的互联网上的网站使用虚拟路径的(这是更具可读性和提供更多的安全性)为他们的文件和文件夹。实际的文件和文件夹位于这些虚拟路径后面的服务器上。因此,要从远程服务器复制文件或文件夹,我们需要资源的实际路径。
我提供了下面的代码片段下载从我的自我部署在服务器上的文件(我知道它当然的目录结构)
string filename = "MyPage.xml";
string filesource = Server.MapPath("~/MyFiles/") + filename; // server file "MyPage.xml" available in server directory "files"
System.IO.FileInfo fi = new System.IO.FileInfo(filesource);
string filedest = System.IO.Path.GetTempPath()+"MyFile.xml";
fi.CopyTo(filedest);
这些都是一些其他SO张贴你可以看看;
How to download a file from a URL in C#?
Copy image file from web url to local folder?
how to copy contents of a website using a .net desktop application
how to copy all text from a certain webpage and save it to notepad C#
How do you archive an entire website for offline viewing?
此代码用于保存文件...我需要代码保存网站与不同类型的文件 –
'DirectoryInfo'是本地或网络路径,而不是为某个HTTPS资源。 – Rob
也许你需要身份验证才能看到远程路径,请看 http://stackoverflow.com/questions/5433570/access-a-remote-directory-from-c-sharp –
Rob!什么用于从https资源下载文件夹? –