我在C#3.5中使用了一個類來處理所有的FTP傳輸,但是因爲我已經升級到了框架4,所以我遇到了一些問題。c#4:檢查FTP目錄是否存在
我在谷歌搜索但沒有找到解決方案。
特別是隨着一個方法來檢查是否存在一個目錄:
public bool DirectoryExists(string directory)
{
bool directoryExists = false;
if (directory.Substring(0, 1) != "/")
directory = "/" + directory;
FtpWebRequest request = GetFtpWebRequest(host + directory, WebRequestMethods.Ftp.PrintWorkingDirectory);
try
{
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
directoryExists = true;
}
}
catch (WebException)
{
directoryExists = false;
}
return directoryExists;
}
private FtpWebRequest GetFtpWebRequest(string url, string method)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
request.UseBinary = true;
request.KeepAlive = true;
request.UsePassive = (mode == Modes.Passive);
request.Timeout = Timeout.Infinite;
request.ServicePoint.ConnectionLimit = 6;
request.ReadWriteTimeout = Timeout.Infinite;
if (credential == null)
credential = new NetworkCredential(login, password);
request.Credentials = credential;
request.Method = method;
return request;
}
的方法DirectoryExists總是返回真值(即使該目錄不存在),但只在框架4,前一個例外是由GetFtpWebRequest拋出如果該目錄不存在。
有沒有人有這個問題?
請不要告訴我使用其他圖書館引起我的所有程序取決於這個的,我不希望更新所有...
您是否嘗試過在返回TRUE之前檢查響應的內容; – 2011-01-31 14:21:01
您是否將項目更新到.NET 4.0?如果是這樣,請檢查您是否在項目屬性中使用.NET 4.0 Framework而不是.NET 4.0 Client Profile。這已經修復了迄今爲止所遇到的所有兼容性問題的75%。 – Weegee 2011-01-31 14:23:18