2011-11-02 129 views
3

我試圖檢查目錄是否存在於FTP服務器上。在你說「使用ListDirectory」或「使用PrintWorkingDirectory」之前,他們不總是工作;例如,我測試了是否存在ftp://webserver/Logs,並告訴我它確實沒有。所以我下了上傳文件到目錄的路徑,如果成功,那麼該目錄就存在。c#ftp上傳到Linux

問題是,下面的方法不適用於GoDaddy的基於CentOS的服務器運行vsFTPd 2.0.7.2。在IIS7.5上使用Microsoft FTP服務器可以正常工作。

因此,我使用Wireshark監控流量,並使用Filezilla查看它在做什麼,以防我的應用無法正常工作。唯一的區別是Filezilla正在更改工作目錄,因爲我試圖在文件上面添加一個路徑。

我有一種感覺,它與上傳到服務器的路徑以及Linux的解釋有關,因爲它可能對名稱有點有趣...... :-D任何想法受到熱烈歡迎?

應用代碼

private bool DirectoryExists(string d) 
{ 
    bool exists = true; 
    try 
    { 
     string file = "directoryexists.test"; 
     string path = url + homepath + d + "/" + file; 

     //Try to save to the directory 
     req = (FtpWebRequest)WebRequest.Create(path); 
     req.ConnectionGroupName = "conngroup1"; 
     req.Method = WebRequestMethods.Ftp.UploadFile; 
     if (nc != null) req.Credentials = nc; 
     if (cbSSL.Checked) req.EnableSsl = true; 
     req.Timeout = 10000; 

     byte[] fileContents = System.Text.Encoding.Unicode.GetBytes("SAFE TO DELETE"); 
     req.ContentLength = fileContents.Length; 

     Stream s = req.GetRequestStream(); 
     s.Write(fileContents, 0, fileContents.Length); 
     s.Close(); 

     //Delete file if successful 
     req = (FtpWebRequest)WebRequest.Create(path); 
     req.ConnectionGroupName = "conngroup1"; 
     req.Method = WebRequestMethods.Ftp.DeleteFile; 
     if (nc != null) req.Credentials = nc; 
     if (cbSSL.Checked) req.EnableSsl = true; 
     req.Timeout = 10000; 

     res = (FtpWebResponse)req.GetResponse(); 
     res.Close(); 
    } 
    catch (WebException ex) 
    { 
     exists = false; 
    } 
    return exists; 
} 

Filezilla的登錄經由Wireshark的

Response: 230 Login successful. 
Request: CWD /Home/test1 
Response: 250 Directory successfully changed. 
Request: TYPE I 
Response: 200 Switching to Binary mode. 
Request: PASV 
Response: 227 Entering Passive Mode (216,69,186,142,71,209) 
Request: LIST 
Response: 150 Here comes the directory listing. 
FTP Data: 78 bytes 
Response: 226 Directory send OK. 
Request: PASV 
Response: 227 Entering Passive Mode (216,69,186,142,177,1) 
Request: STOR directoryexists.txt 
Response: 150 Ok to send data. 
Response: 226 File receive OK. 

經由Wireshark的應用日誌

Response: 230 Login successful. 
Request: OPTS utf8 on 
Response: 501 Option not understood. 
Request: PWD 
Response: 257 "/Home/" 
Request: PWD 
Response: 257 "/Home/" 
Request: TYPE I 
Response: 200 Switching to Binary mode. 
Request: PASV 
Response: 227 Entering Passive Mode (216,69,186,142,217,87) 
Request: STOR test1/directoryexists.txt 
Response: 553 Could not create file. 

它創建文件夾,如果他們不存在。

Response: 230 Login successful. 
Request: PWD 
Response: 257 "/Home/" 
Request: PWD 
Response: 257 "/Home/" 
Request: TYPE I 
Response: 200 Switching to Binary mode. 
Request: PASV 
Response: 227 Entering Passive Mode (216,69,186,142,220,60) 
Request: STOR Logs/directoryexists.txt 
Response: 553 Could not create file. 
Request: PWD 
Response: 257 "/Home/" 
Request: MKD Logs 
Response: 257 Create folder operation successful. 
Request: TYPE I 
Response: 200 Switching to Binary mode. 
Request: PASV 
Response: 227 Entering Passive Mode (216,69,186,142,255,245) 
Request: STOR Logs/LogFiles/directoryexists.txt 
Response: 553 Could not create file. 
Request: PWD 
Response: 257 "/Home/" 
Request: MKD Logs/LogFiles 
Response: 257 Create folder operation successful. 
+0

回到家路徑一般553個錯誤是與權限。我看到Wireshark拿起'STOR test1/directoryexists.txt'。是否有可能無法在FTP服務器上的/ Home /下創建另一個嵌套的目錄?一些託管服務提供商限制了您可以嵌套導演的深度,因此您可能/不/被/允許/ /放置/一個/文件夾/這個/很深或甚至/這個/很深。根據wireshark你在/ Home /,所以你應該只是能夠執行'STOR directoryexists.txt'。 –

+0

@Bryan:謝謝你的想法,但只是測試,並且工作正常。 'Response:257「/ Home/A/B/C /」' – Christian

+0

您是否嘗試將'req.Method'設置爲'WebRequestMethods.Ftp.MakeDirectory',創建目錄,然後嘗試上載文件? –

回答

3

Linux的再次叮咬......

解決的辦法是設置一個雙斜線的路徑名,這樣,當它涉及到STOR,它擁有領先的斜線......像這樣:

string url = "ftp://website/"; 
string homepath = "/Home/"; 
string d = "test1"; 
string file = "directoryexists.test"; 

string path = url + homepath + d + "/" + file; 

這樣的完整路徑看起來像ftp://website//Home/test1/directoryexists.test

req = (FtpWebRequest)WebRequest.Create("ftp://website//Home/test1/directoryexists.test"); 

這樣的STOR命令將顯示像

STOR /Home/test1/directoryexists.test 

您可以從狀態說明

req = (FtpWebRequest)WebRequest.Create(url); 
req.Method = WebRequestMethods.Ftp.PrintWorkingDirectory; 
if (nc != null) req.Credentials = nc; 
if (cbSSL.Checked) req.EnableSsl = true; 
req.Timeout = 10000; 
res = (FtpWebResponse)req.GetResponse(); 

System.Text.RegularExpressions.Regex regexp = new System.Text.RegularExpressions.Regex("\\s\"([^\"]*)\"\\s"); 
homepath = regexp.Match(res.StatusDescription).Groups[1].Value; 

res.Close(); 
+0

Linux不是FTP服務器。但很高興你有一個解決方案。 –

+0

感謝顯而易見的Merlyn;) – Christian

+0

我剛剛遇到了同樣的問題,並且使用雙斜線將FTP URL從文件路徑中分離出來。謝謝! – MikeWyatt