0

Web リクエストから画像を保存していますが、非常に奇妙なことが起こっています。ダウンロードしている 8,000 枚の画像の約半分で、IOEXCEPTION エラーが発生します: ERROR_ACCESS_DENIED (5) INVALID_PARAMETER (87)

file.open を使用してファイルを保存する前に、ファイルが存在しないことを確認します。次のコード行で例外がスローされます。

fileStream = File.Open(宛先、FileMode.Create、FileAccess.Write、FileShare.None);

以下はコードです:

public static bool DownloadFile(string url, string destination) { bool success = false;

        System.Net.HttpWebRequest request = null;
        System.Net.WebResponse response = null;
        Stream responseStream = null;
        FileStream fileStream = null;

        try
        {
            request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            request.Method = "GET";
            request.Timeout = 100000; // 100 seconds
            request.Proxy = System.Net.GlobalProxySelection.GetEmptyWebProxy();
            response = request.GetResponse();

            responseStream = response.GetResponseStream();
            fileStream = File.Open(destination, FileMode.Create, FileAccess.Write, FileShare.None);
            //fileStream = File.Create(destination);


            // read up to ten kilobytes at a time
            int maxRead = 10240;
            byte[] buffer = new byte[maxRead];
            int bytesRead = 0;
            int totalBytesRead = 0;

            // loop until no data is returned
            while ((bytesRead = responseStream.Read(buffer, 0, maxRead)) > 0)
            {
                totalBytesRead += bytesRead;
                fileStream.Write(buffer, 0, bytesRead);
            }

            // we got to this point with no exception. Ok.
            success = true;
        }
        catch (System.Net.WebException we)
        {
            // something went terribly wrong.
            success = false;
            //MessageBox.Show(exp.ToString());
            writeErrFile(we.ToString(), url);
            //Debug.WriteLine(exp);
        }
        catch (System.IO.IOException ie)
        {
            // something went terribly wrong.
            success = false;
            //MessageBox.Show(ie.InnerException.ToString());
            writeErrFile(ie.ToString(), destination + " -- " + url);
            //Debug.WriteLine(exp);
        }
        catch (Exception exp)
        {
            // something went terribly wrong.
            success = false;
            //MessageBox.Show(exp.ToString());
            writeErrFile(exp.ToString(), destination + " -- " + url);
            //Debug.WriteLine(exp);
        }
        finally
        {
            // cleanup all potentially open streams.

            if (null != responseStream)
                responseStream.Close();
            if (null != response)
                response.Close();
            if (null != fileStream)
                fileStream.Close();

        }

        // if part of the file was written and the transfer failed, delete the partial file
        if (!success && File.Exists(destination))
            File.Delete(destination);

        return success;
    }

私はこれに数日間立ち往生しています。想像を絶する桁違いの助けをいただければ幸いです。

4

1 に答える 1

0

file.exists() を使用してファイルが存在するかどうかを確認し、file.create または file.openwrite を使用してファイルを書き込みます。

あなたのコードから、ファイルが存在することを確認する方法がわかりません。

于 2009-12-24T19:20:38.880 に答える