2013-03-10 64 views
3

我想從ftp服務器下載圖像,但當我去「FtpWebResponse」時, 。遠程服務器返回錯誤:(550)文件不可用(例如,文件未找到,無法訪問)

下面是我的下載功能

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(myStringWebSource); 
request.Method = WebRequestMethods.Ftp.DownloadFile; 
request.Credentials = new NetworkCredential(this.FtpUserID, this.FtpPassword); 
FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
System.IO.Stream responseStream = response.GetResponseStream(); 
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream); 
System.IO.StreamWriter writer = new System.IO.StreamWriter(SaveFile); 
writer.Write(reader.ReadToEnd()); 

writer.Close(); 
reader.Close(); 
response.Close(); 

我有谷歌這個例外幾天,但我仍然無法弄清楚,爲什麼這讓錯誤的。如果沒有足夠的信息,請告訴我。

回答

0

當您要下載的文件正在被另一個進程使用時,您將會收到此錯誤。

0

爲了解決此問題,需要強制System.Net.FtpWebRequest命令恢復到以前在.Net Framework 2.0/3.5中使用的舊行爲,並在發出額外的CWD命令之前發佈實際的命令。

爲了做到這一點,需要在調用System.Net.FtpWebRequest類的任何實例之前放置以下代碼。下面的代碼只需要調用一次,因爲它改變了整個應用程序域的設置。

private static void SetMethodRequiresCWD() 
{ 
    Type requestType = typeof(FtpWebRequest); 
    FieldInfo methodInfoField = requestType.GetField("m_MethodInfo", BindingFlags.NonPublic | BindingFlags.Instance); 
    Type methodInfoType = methodInfoField.FieldType; 


    FieldInfo knownMethodsField = methodInfoType.GetField("KnownMethodInfo", BindingFlags.Static | BindingFlags.NonPublic); 
    Array knownMethodsArray = (Array)knownMethodsField.GetValue(null); 

    FieldInfo flagsField = methodInfoType.GetField("Flags", BindingFlags.NonPublic | BindingFlags.Instance); 

    int MustChangeWorkingDirectoryToPath = 0x100; 
    foreach (object knownMethod in knownMethodsArray) 
    { 
     int flags = (int)flagsField.GetValue(knownMethod); 
     flags |= MustChangeWorkingDirectoryToPath; 
     flagsField.SetValue(knownMethod, flags); 
    } 
} 

http://support.microsoft.com/kb/2134299

相關問題