【问题标题】:Copying the folder from server to the local directory将文件夹从服务器复制到本地目录
【发布时间】:2016-06-30 08:14:30
【问题描述】:

我正在开发一个用 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);
    }
}   

消息框显示“给定的路径格式不支持”。

【问题讨论】:

标签: c#


【解决方案1】:

正如我们所知,所有托管在互联网上的网站都使用 虚拟 路径(更具可读性并提供更高的安全性)来存储它们的文件和文件夹。实际文件和文件夹位于这些虚拟路径后面的服务器上。因此,要从远程服务器复制文件或文件夹,我们需要资源的实际路径。

我提供以下代码 sn-p 用于从我自己部署的服务器下载文件(我当然知道它的目录结构)

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?

【讨论】:

  • 此代码用于保存文件...我需要用于保存具有不同类型文件的网站的代码
猜你喜欢
  • 1970-01-01
  • 2012-01-22
  • 2016-09-13
  • 1970-01-01
  • 1970-01-01
  • 2011-02-21
  • 2019-08-23
  • 2012-02-17
  • 1970-01-01
相关资源
最近更新 更多