保存图像时C#compact框架中的IOEXCEPTION

时间:2009-12-24 15:18:15

标签: c# .net compact-framework smartphone

我正在从网页请求中保存图片,而且发生了一些非常奇怪的事情。在我正在下载的8,000张图片中的大约一半中,我收到了IOEXCEPTION错误: ERROR_ACCESS_DENIED(5) INVALID_PARAMETER(87)

在使用file.open保存文件之前,我检查以确保该文件不存在。这行代码抛出异常:

fileStream = File.Open(destination,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;
    }

我被困在这几天了。任何帮助都会受到无法想象的数量级的赞赏。

1 个答案:

答案 0 :(得分:0)

使用file.exists()检查文件是否存在,使用file.create或file.openwrite写入文件。

从你的代码中我看不出你是如何检查文件存在的。