2012-03-10 17 views
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) 
     { 
     } 

    } 

に基づいて私のコードをしてください変更

は、私を示唆しています。

答えて

0

これは、ディレクトリが存在することを保証するための多くのコードです。 Directory.Exists()メソッド(またはそれに似たメソッドを利用できますか)がありますか?その後、アップロードする前にEnsureDirectoryExists()を呼び出すことができます。

private bool EnsureDirectoryExists(string path) 
{ 
    // check if it exists 
    if (Directory.Exists(path)) 
     return true; 

    string parentPath = GetParentPath(path); 
    if (parentPath == null) 
     return false; 

    bool result = EnsureDirectoryExists(parentPath); 

    if (result) 
    { 
     CreateDirectory(path); 
     return true; 
    } 

    return false; 
} 

免責事項:あなたは、ロジックを少し微調整し、FTP機能を使用する必要があるかもしれませんが、私はあなたがポイントを得る願っています。

+0

おかげさまで申し訳ありませんが、私はディレクトリを作成することはできません。あなたが言及したようにftp.makedirectoryはディレクトリ全体を作成する2行のコードですが、IIS FTPで作成したい場合は、 1行のコードで、各ディレクトリを1つずつ作成する必要があります。 – Usher

+0

私は各ディレクトリを1つずつ作成しています(親ディレクトリの作成を開始します)。そして、はい、それは私が1つだけを持っているコードの2行かもしれませんが、あなたが提案したtry/catchブロックを通過するよりも簡単ですと信じています。 – thoean

+0

、本当に、私はちょうど1つを作成するパスを渡すコードの地獄の量を削減します。あなたの助けをもう一度ありがとう。 – Usher

関連する問題