C#使用FTP上传整个目录我想做的是在C#(CSharp)中使用FTP上传一个网站。所以我需要上传文件夹中的所有文件和文件夹,并保持其结构。我正在使用这个FTP类:http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class进行实际上传。我得出的结论是我需要编写一个递归方法来遍历主目录的每个子目录并上传其中的所有文件和文件夹。这应该将我的文件夹的精确副本复制到FTP。问题是……我不知道怎么写这样的方法。我以前写过递归方法,但我是FTP部分的新手。到目前为止,这是我所拥有的:privatevoidrecursiveDirectory(stringdirectoryPath){string[]filePaths=null;字符串[]子目录=null;filePaths=Directory.GetFiles(directoryPath,"*.*");subDirectories=Directory.GetDirectories(directoryPath);if(filePaths!=null&&subDirectories!=null){foreach(子目录中的字符串目录){ftpClient.createDirectory(目录);}foreach(文件路径中的字符串文件){ftpClient.upload(Path.GetDirectoryName(directoryPath),文件);但它远未完成,我不知道如何进行。我绝对需要比我更了解这一点!提前致谢:)哦,如果它报告了它的进度,那就太好了:)(我正在使用进度条)编辑:可能不清楚...如何使用FTP上传包含所有子目录和文件的目录?问题已经解决了!?好的,所以我设法编写了myslef方法。如果有人需要,请随意复制:privatevoidrecursiveDirectory(stringdirPath,stringuploadPath){string[]files=Directory.GetFiles(dirPath,"*.*");string[]subDirs=Directory.GetDirectories(dirPath);foreach(文件中的字符串文件){ftpClient.upload(uploadPath+"/"+Path.GetFileName(file),file);}foreach(stringsubDirinsubDirs){ftpClient.createDirectory(uploadPath+"/"+Path.GetFileName(subDir));recursiveDirectory(subDir,uploadPath+"/"+Path.GetFileName(subDir));它工作得很好:)我写了一个FTPclasse并将它包装在一个WinForms用户控件中。您可以在文章AnFtpClientClassandWinFormControl中看到我的代码。我写了一个可重用的类来将整个目录上传到windows服务器上的ftp站点,该程序还重命名了旧版本的文件夹(我用它来为我的windows服务程序上传到服务器)。也许有些人需要这个:classMyFtpClient{protectedstringFtpUser{get;放;}受保护的字符串FtpPass{get;放;}受保护的字符串FtpServerUrl{get;放;}受保护的字符串DirPathToUpload{get;放;}受保护的字符串BaseDirectory{get;放;}publicMyFtpClient(stringftpuser,stringftppass,stringftpserverurl,stringdirpathtoupload){this.FtpPass=ftppass;this.FtpUser=ftpuser;this.FtpServerUrl=ftpserverurl;this.DirPathToUpload=dirpathtoupload;varspllitedpath=dirpathtoupload.Split('\').ToArray();//最后一个索引必须是服务器上的“基本”目录this.BaseDirectory=spllitedpath[spllitedpath.Length-1];}publicvoidUploadDirectory(){//重命名旧文件夹版本(如果存在)RenameDir(BaseDirectory);//在服务器上创建父文件夹CreateDir(BaseDirectory);//上传路径最外部目录下的文件UploadAllFolderFiles(DirPathToUpload,BaseDirectory);//遍历子目录中的所有文件toriesforeach(stringdirPathinDirectory.GetDirectories(DirPathToUpload,"*",SearchOption.AllDirectories)){//创建文件夹CreateDir(dirPath.Substring(dirPath.IndexOf(BaseDirectory),dirPath.Length-dirPath.IndexOf(BaseDirectory)));Console.WriteLine(dirPath.Substring(dirPath.IndexOf(BaseDirectory),dirPath.Length-dirPath.IndexOf(BaseDirectory)));UploadAllFolderFiles(dirPath,dirPath.Substring(dirPath.IndexOf(BaseDirectory),dirPath.Length-dirPath.IndexOf(BaseDirectory))}}privatevoidUploadAllFolderFiles(stringlocalpath,stringremotepath){字符串[]files=Directory.GetFiles(localpath);//仅获取文件名并连接到远程路径foreach(stringfileinfiles){//完整的远程路径varfullremotepath=remotepath+"\"+Path.GetFileName(file);//本地路径varfulllocalpath=Path.GetFullPath(file);//上传到服务器Upload(fulllocalpath,fullremotepath);}}publicboolCreateDir(stringdirname){try{WebRequestrequest=WebRequest.Create("ftp://"+FtpServerUrl+"/"+dirname);request.Method=WebRequestMethods.Ftp.MakeDirectory;request.Proxy=newWebProxy();request.Credentials=newNetworkCredential(FtpUser,FtpPass);使用(varresp=(FtpWebResponse)request.GetResponse()){if(resp.StatusCode==FtpStatusCode.PathnameCreated){returntrue;}else{返回错误;}}}catch{返回false;}}publicvoidUpload(stringfilepath,stringtargetpath){using(WebClientclient=newWebClient()){client.Credentials=newNetworkCredential(FtpUser,FtpPass);客户端.Proxy=null;varfixedpath=targetpath.Replace(@"","/");client.UploadFile("ftp://"+FtpServerUrl+"/"+fixedpath,WebRequestMethods.Ftp.UploadFile,文件路径);}}publicboolRenameDir(stringdirname){varpath="ftp://"+FtpServerUrl+"/"+dirname;字符串serverUri=路径;尝试{FtpWebRequest请求=(FtpWebRequest)WebRequest.Create(serverUri);请求.Method=WebRequestMethods.Ftp.重命名;请求.Proxy=null;request.Credentials=newNetworkCredential(FtpUser,FtpPass);//更改旧文件夹的名称旧文件夹request.RenameTo=DateTime.Now.ToString("yyyyMMddHHmmss");FtpWebResponse响应=(FtpWebResponse)request.GetResponse();使用(varresp=(FtpWebResponse)request.GetResponse()){if(resp.StatusCode==FtpStatusCode.FileActionOK){returntrue;}else{返回错误;}}}catch{返回false;}}}创建此类的一个实例:staticvoidMain(string[]args){MyFtpClientftp=newMyFtpClient(ftpuser,ftppass,ftpServerUrl,@"C:Usersxxxxxxxxxxx");ftp.上传目录();Console.WriteLine("完成");控制台.ReadLine();}除非您这样做是为了乐趣或自我提升,否则请使用我可以推荐Chilkat的商业模块,但我相信还有其他模块。注意:我很确定这确实解决了上述问题,我想做的是在C#(CSharp)上使用FTP上传网站。以上是C#学习教程:C#使用FTP上传整个目录共享的所有内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
