本文介绍了在ftp服务器中创建子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我试图使用下面的代码创建一个目录和子目录,但它会抛出异常。



Hi,

Am trying to create a directory and subdirectory using the below code but it throw exception.

string Path :"ftp://1.1.1.1/server01/develop/test/files/name";
FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
requestDir.Method = WebRequestMethods.Ftp.MakeDirectory;
requestDir.Credentials = new NetworkCredential("sh", "se");
requestDir.UsePassive = true;
requestDir.UseBinary = true;
requestDir.KeepAlive = false;

FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse();
Stream ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();





获得例外:



Am getting the exception :

"The remote server returned an error: (550) File unavailable (e.g., file not found, no access).".





这段代码就像传递路径一样工作



this code is work to do like pass the path like below

path1:"ftp://1.1.1.1/server01"
path2:"ftp://1.1.1.1/server01/develp"
path3:"ftp://1.1.1.1/server01/develp/test"
path4:"ftp://1.1.1.1/server01/develp/files"
path5:"ftp://1.1.1.1/server01/develp/files/name".





我只是想知道为什么它在创建子目录时失败了?请帮助。



先谢谢



Am just wondering why its failing when it creates subdirectory ? any help please.

Thanks in Advance

推荐答案


private static bool CreateFTPDirectory(string directory, string folders)
        {

            bool exito = true;

            string[] lstfolders = folders.Split('/');

            string pathftp = directory;
            foreach (string fol in lstfolders)
            {

                if (fol != "")
                {

                    try
                    {
                        pathftp += "/" + fol;
                        //create the directory
                        FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(pathftp));
                        requestDir.Method = WebRequestMethods.Ftp.MakeDirectory;
                        requestDir.Credentials = new NetworkCredential("ftpbienesraices", "b13n3sr@1c3s");
                        requestDir.UsePassive = true;
                        requestDir.UseBinary = true;
                        requestDir.KeepAlive = false;
                        FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse();
                        Stream ftpStream = response.GetResponseStream();

                        ftpStream.Close();
                        response.Close();
                    }
                    catch (WebException ex)
                    {
                        FtpWebResponse response = (FtpWebResponse)ex.Response;
                        if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                        {
                            response.Close();
                            exito = true;
                        }
                        else
                        {
                            response.Close();
                            exito = false;
                        }
                    }
                }
            }

            return exito;

        }


这篇关于在ftp服务器中创建子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 10:56