在FTP中创建子目录的任何好的方法 [英] Any good approach to create sub dir in FTP
本文介绍了在FTP中创建子目录的任何好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在使用Make目录在ftp中创建文件夹和子文件夹(使用filezilla),但工作正常,但是当我尝试在测试服务器(IIS FTP)中运行时,抛出550,找不到文件或无法访问.so只是一种在我的ftp服务器中更改代码以创建子目录的快速方法可以正常工作,但我知道它是一种类似的方法.
有人请建议第二种思路.
Hi All,
I was using Make directory to create a folders and subfolder in my ftp (using filezilla) works fine,but when i try to do in my test server (IIS FTP) doesn''t work ,throws 550,file not found or no access.so just a quick way to change the code to create subdirctory in my ftp server works fine but i know its a kinda sh*tty way to do like that.
Some one please advice the second thought to approach.
string path = "ftp://1.1.1.1/media/times/" + Name + "/test/fileName";
string[] pathsplit = path.ToString().Split('/');
string Firstpath = pathsplit[0] + "/" + pathsplit[1] + "/" + pathsplit[2] + "/" + pathsplit[3];
string SecondPath = Firstpath + "/" + pathsplit[4];
string ThirdPath = SecondPath + "/" + pathsplit[5];
int count = pathsplit.Count();
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential("sh", "se");
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
// Okay.
upload();
}
}
catch (WebException ex)
{
try
{
//create the first directory if its not there
//If already there call the catch
FtpWebRequest createdir = (FtpWebRequest)FtpWebRequest.Create(new Uri(Firstpath));
createdir.Method = WebRequestMethods.Ftp.MakeDirectory;
createdir.Credentials = new NetworkCredential("sh", "se");
createdir.UsePassive = true;
createdir.UseBinary = true;
createdir.KeepAlive = false;
FtpWebResponse response1 = (FtpWebResponse)createdir.GetResponse();
Stream ftpStream1 = response1.GetResponseStream();
ftpStream1.Close();
response1.Close();
}
catch (Exception e)
{
try
{
//create the second directory if its not there
//If already there call the catch
FtpWebRequest createdir = (FtpWebRequest)FtpWebRequest.Create(new Uri(SecondPath));
createdir.Method = WebRequestMethods.Ftp.MakeDirectory;
createdir.Credentials = new NetworkCredential("sh", "se");
createdir.UsePassive = true;
createdir.UseBinary = true;
createdir.KeepAlive = false;
FtpWebResponse response1 = (FtpWebResponse)createdir.GetResponse();
Stream ftpStream1 = response1.GetResponseStream();
ftpStream1.Close();
response1.Close();
}
catch (Exception el)
{
try
{
//create the third directory if its not there
//If already there call the catch
FtpWebRequest createdir = (FtpWebRequest)FtpWebRequest.Create(new Uri(ThirdPath));
createdir.Method = WebRequestMethods.Ftp.MakeDirectory;
createdir.Credentials = new NetworkCredential("sh", "se");
createdir.UsePassive = true;
createdir.UseBinary = true;
createdir.KeepAlive = false;
FtpWebResponse response1 = (FtpWebResponse)createdir.GetResponse();
Stream ftpStream1 = response1.GetResponseStream();
ftpStream1.Close();
response1.Close();
}
catch
{ }
}
}
if (ex.Response != null)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
// Directory not found.
}
}
}
推荐答案
分割后是否检查路径是否正确?
无论如何,我认为您有太多的代码,您可以为FtpWebRequest编写一个简单的方法,在其中可以检查文件/目录是否存在.
致以最诚挚的问候
Hi,
do you checked if path is correct after splitting?
Anyway, you have too much code in my opinion, you could write a simple method for FtpWebRequest(s) where you can check if File/Directory Exists.
With Best Regards
这篇关于在FTP中创建子目录的任何好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文