質問

構築を行っているFTPユーティリティクラスC#.している場合に WebException スローされるの呼び出し FtpWebRequest.GetResponse(), 私の場合、例外がスローされます要求されたファイルにおいて、既存のリモートサーバの FtpWebResponse 変数は対象外です。

もっともだからと言ってることをここに宣言しますの変数以外の try..catch ブロックを取得しますコンパイルエラーが"利用の割り当てられないローカル変数の対応について'"が調査を実施しているのは、同じ伝える方法はありません割り当てまでを割り当ての対応を FtpWebRequest.GetResponse() 方法。

で誰か教えてください、私を見落とさないよう明らかな?

よろしく!

ここには現在の方法:

private void Download(string ftpServer, string ftpPath, string ftpFileName, string localPath, 
                           string localFileName, string ftpUserID, string ftpPassword)
    {
        FtpWebRequest reqFTP;
        FtpWebResponse response;
        try
        {
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"
               + ftpServer + "/" + ftpPath + "/" + ftpFileName));
            reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(ftpUserID,
                                                       ftpPassword);

            /* HERE IS WHERE THE EXCEPTION IS THROWN FOR FILE NOT AVAILABLE*/
            response = (FtpWebResponse)reqFTP.GetResponse();
            Stream ftpStream = response.GetResponseStream();


            FileStream outputStream = new FileStream(localPath + "\\" +
               localFileName, FileMode.Create);

            long cl = response.ContentLength;
            int bufferSize = 2048;
            int readCount;
            byte[] buffer = new byte[bufferSize];

            readCount = ftpStream.Read(buffer, 0, bufferSize);
            while (readCount > 0)
            {
                outputStream.Write(buffer, 0, readCount);
                readCount = ftpStream.Read(buffer, 0, bufferSize);
            }

            ftpStream.Close();
            outputStream.Close();
            response.Close();
        }
        catch (WebException webex)
        {
            /*HERE THE response VARIABLE IS UNASSIGNED*/
            if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) { 
                //do something
            }
        }
役に立ちましたか?

解決

として一般的な方法で解決すでに割り当て null に応答してからチェックをキャッチブロックの場合 null.

    FtpWebResponse response = null;
    try
    {
...
    }
    catch (WebException webex)
    {
        if ((response != null) && (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)) { 
            //do something
        }
    }

しかし、この特定の場合、すべての性質から必要な WebException インスタンスを含む サーバー対応)!

他のヒント

正しい解決の問題でこの質問はこちら
確認方法の場合ファイルに存在するFTP前FtpWebRequest

短:
お'response'変数は常にnullでエラーになります。を検査する必要があり、FtpWebResponseからwebex.応答'(キャスト)のStatusCode.

もきれいに割り当ての変数:

FtpWebRequest reqFTP = null;
FtpWebResponse response = null;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top