简体   繁体   中英

The remote server returned an error: (550) File unavailable (e.g., file not found, no access)

I want to download an image from ftp server, but I just got this exception as title when I went to "FtpWebResponse".

Below is my download function

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();

I have google this exception for a few days but I still cannot figure out why this got wrong. If there is not enough information, plz tell me.

当您要下载的文件正由另一个进程使用时,您将收到此错误。

In order to resolve this issue, it is required to force the System.Net.FtpWebRequest command to revert back to the old behavior of how it used to work in .Net Framework 2.0/3.5 and issue the extra CWD command before issuing the actual command.

In order to do this, the following code needs to be placed before any instance of the System.Net.FtpWebRequest class is invoked. The code below only needs to be called once, since it changes the settings of the entire application domain.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM