Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
保存图像时C#compact框架中的IOEXCEPTION_C#_.net_Compact Framework_Smartphone - Fatal编程技术网

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

保存图像时C#compact框架中的IOEXCEPTION,c#,.net,compact-framework,smartphone,C#,.net,Compact Framework,Smartphone,我正在保存一个来自web请求的图像,但发生了一些非常奇怪的事情。在我下载的8000张图片中,大约有一半出现IOEXCEPTION错误: 错误访问被拒绝(5) 无效的_参数(87) 在使用file.open保存文件之前,我会检查以确保该文件不存在。在以下代码行引发异常: fileStream=File.Open(目标,FileMode.Create,FileAccess.Write,FileShare.None) 代码如下: 公共静态bool下载文件(字符串url、字符串目标) { 布尔成功=假

我正在保存一个来自web请求的图像,但发生了一些非常奇怪的事情。在我下载的8000张图片中,大约有一半出现IOEXCEPTION错误: 错误访问被拒绝(5) 无效的_参数(87)

在使用file.open保存文件之前,我会检查以确保该文件不存在。在以下代码行引发异常:

fileStream=File.Open(目标,FileMode.Create,FileAccess.Write,FileShare.None)

代码如下:

公共静态bool下载文件(字符串url、字符串目标) { 布尔成功=假

        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;
    }
这件事我已经纠结了好几天了。任何帮助都将不胜感激。

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


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

下面是抛出的错误:System.IO.IOException:IOException at System.IO.FileStream..ctor()at System.IO.FileStream..ctor()at System.IO.StreamWriter..ctor()at System.IO.StreamWriter..ctor()at System.IO.StreamWriter..ctor()at System.IO.StreamWriter..ctor())在ImageSync.Form1.DownloadFile()在ImageSync.Form1.button1在System.Windows.Forms.Control.OnClick()在System.Windows.Forms.Button.OnClick()在System.Windows.Forms.ButtonBase.WnProc()在System.Windows.Forms.Control.在Microsoft.AGL.Forms.EVL.EnterMainLoop()在System.Windows.Forms.Application.Run()在ImageSync尝试使用Process Monitor跟踪文件系统访问错误。如何在Windows Mobile 5上使用Process Monitor?这是针对Windows Mobile 5设备的。有报告称,目录中的文件限制为1000个,但如果我从桌面将文件复制到存储卡上,它可以很好地读取文件,但它不会允许我可以创建任何新文件。有人知道解决方法吗?这是我用来调用下载文件的代码:if(!file.Exists(fileDownloadName.ToString()){if(DownloadFile(urlName.ToString(),fileDownloadName.ToString()))停机次数++;其他故障次数+++;}其他{existCount++;}