1
私のftpで(filezillaを使用して)フォルダーとサブフォルダを作成するにはMakeディレクトリを使用していましたが、テストサーバー(IIS FTP)私のFTPサーバーでサブディレクトリを作成するためのコードを変更するための素早い方法はありますが、そのようなやり方はちょっと分かりません。君たちのいずれかがより良い方法を見つけた場合@MarkusFTPでサブフォルダを作成するのに非効率的です
var dir = new ConsoleApplication5.Program();
string path = "ftp://1.1.1.1/testsvr01/times/" + "testfile" + "/svr01fileName";
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] + "/";
string[] paths = { Firstpath, SecondPath, ThirdPath };
foreach (string pat in paths)
{
bool result= dir.EnsureDirectoryExists(pat);
if (result==true)
{
//do nothing
}
else
{ //create dir
dir.createdir(pat);
}
}
upload(path,filename);
}
private bool EnsureDirectoryExists(string pat)
{
try
{
//call the method the first path is exist ?
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(pat);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential("sh", "se");
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
return true;
}
}
catch (Exception ex)
{ return false; }
}
public void createdir(string pat)
{
try
{
FtpWebRequest createdir = (FtpWebRequest)FtpWebRequest.Create(new Uri(pat));
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 ex)
{
}
}
に基づいて私のコードをしてください変更
は、私を示唆しています。
おかげさまで申し訳ありませんが、私はディレクトリを作成することはできません。あなたが言及したようにftp.makedirectoryはディレクトリ全体を作成する2行のコードですが、IIS FTPで作成したい場合は、 1行のコードで、各ディレクトリを1つずつ作成する必要があります。 – Usher
私は各ディレクトリを1つずつ作成しています(親ディレクトリの作成を開始します)。そして、はい、それは私が1つだけを持っているコードの2行かもしれませんが、あなたが提案したtry/catchブロックを通過するよりも簡単ですと信じています。 – thoean
、本当に、私はちょうど1つを作成するパスを渡すコードの地獄の量を削減します。あなたの助けをもう一度ありがとう。 – Usher