如何复制所有文件夹从服务器到本地驱动器使用C#

问题描述:

我取从Web.config文件的路径:如何复制所有文件夹从服务器到本地驱动器使用C#

<appSettings>  
    <add key="Server" value="http://admin.xxxxxxx.com/1upload/*All File And Folder Copy this directory *"/> 
    <add key="Local" value="C:\\Users\\IND_COM\\Desktop\\xxxx\\1upload\\paste The Copyed File"/> 
</appSettings> 

在按钮单击事件我将源和目标路径:

protected void btnUpdate_Click(object sender, EventArgs e) 
{ 
    string Source_path = ConfigurationManager.AppSettings["server"].ToString(); 
    string Detination_Path = "@" + ConfigurationManager.AppSettings["Local"].ToString(); 
    DirectoryCopy(Source_path, Detination_Path, true); 
} 

private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) 
{   
    DirectoryInfo dir = new DirectoryInfo(sourceDirName);//Exception Through In this Section.. 

    if (!dir.Exists) 
    { 
     throw new DirectoryNotFoundException("Source directory does not exist or could not be found: "+ sourceDirName); 
    } 


    DirectoryInfo[] dirs = dir.GetDirectories(); 
    if (!Directory.Exists(destDirName)) 
    { 
     Directory.CreateDirectory(destDirName); 
    } 


    FileInfo[] files = dir.GetFiles(); 
    foreach (FileInfo file in files) 
    { 
     string temppath = Path.Combine(destDirName, file.Name); 
     file.CopyTo(temppath, false); 
    } 

    if (copySubDirs) 
    { 
     foreach (DirectoryInfo subdir in dirs) 
     { 
      string temppath = Path.Combine(destDirName, subdir.Name); 
      DirectoryCopy(subdir.FullName, temppath, copySubDirs); 
     } 
    } 
} 

在我的托管服务器上,我有一个文件夹和所有图像和XML保存在不同的文件夹。例如,我有一个名为1Upload的目录。在这个目录里我有3个子目录,每个子目录都有不同的文件。我尝试从服务器复制所有3个目录和文件。这一段代码给出

URI不支持

+0

HTTP不是文件系统。您可以使* web请求*从*下载内容*并将*保存为文件。但是,您不能对不是文件系统的文件系统执行文件系统复制操作。 – David

+0

源代码Directtory的路径“http://admin.xxxxxxx.com/1upload/ –

+0

里面1上传Directore有3个目录和3个目录有一些内容 –

HTTP不是一个文件系统中的异常。

你要找什么做的是执行文件系统复制操作,而是下载从网站的内容和保存它到文件系统。为此,您需要诸如the WebClient.DownloadFile() method之类的东西。

例如:

var client = new WebClient(); 
client.DownloadFile(sourceURL, destinationFile); 

每个文件将是一个单独的请求。您目前正在尝试列表目录内容,这显然不起作用(因为HTTP不是文件系统)。但是,如果您创建了文件列表,则只要拥有该列表,就可以遍历它并为每个文件调用DownloadFile(source, destination)

如果您需要网站提供文件列表,您必须将该功能添加到网站(如果它不在那里,我们不知道)。如果您需要在一个操作中下载所有文件,则该网站需要在一个文件中提供它们(例如.zip文件)。

+0

感谢您的Helf .. –

+0

我试图从您的Consept下载这些文件帮助但我得到了一个异常在Destinationon PAth ..访问路径'C:\ Users \ IND_COM \ Desktop \ New Folder \ 1upload'被拒绝 –