无法将文件下载到AppData文件夹中

时间:2015-06-21 23:53:31

标签: c#

我尝试编写一些下载文件并将其保存在AppData中的代码,但出于某种原因,我在DownloadFile()调用期间不断收到异常。

例外:

  

发生了'System.Net.WebException'类型的未处理异常   System.dll中

     

其他信息:WebClient期间发生异常   请求。

这是我的代码:

string remoteUri = "http://mhost.site11.com/";
            string fileName = "SysSpec.zip", myStringWebResource = null;

            string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            // Create a new WebClient instance.
            using (WebClient myWebClient = new WebClient())
            {
                try
                {
                    myStringWebResource = remoteUri + fileName;
                    // Download the Web resource and save it into the current filesystem folder.
                    myWebClient.DownloadFile(myStringWebResource, appData + "\\PPA\\" + fileName);
                }
                catch (WebException er)
                {
                    var result = GetResponceFromWebException(er);
                    if (result != null)
                    {
                        MessageBox.Show(result.ToString());
                    }

                    throw;
                }
                catch (Exception er)
                {
                    MessageBox.Show(er.ToString());     
                }
            }
        }
        private HttpRequestResponce GetResponceFromWebException(WebException e)
        {
            HttpRequestResponce result = null;
            if (e.Status == WebExceptionStatus.ProtocolError)
            {
                try
                {
                    using (var stream = e.Response.GetResponseStream())
                    {
                        if (stream != null)
                        {
                            using (var reader = new StreamReader(stream))
                            {
                                var responseString = reader.ReadToEnd();
                                var responce = ((HttpWebResponse)e.Response);

                                result = new HttpRequestResponce(responseString, responce.StatusCode);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // log exception or do nothing or throw it
                }
            }
            return result;
        }

解决方案:

将文件下载到我的程序所在的目录中,然后移动它。

string remoteUri = "http://mhost.site11.com/";
            string fileName = "SysSpec.zip", myStringWebResource = null;

            string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            // Create a new WebClient instance.
            using (WebClient myWebClient = new WebClient())
            {
                myStringWebResource = remoteUri + fileName;
                // Download the Web resource and save it into the current filesystem folder.
                myWebClient.DownloadFile(myStringWebResource, fileName);

                // Move the file
                string path = fileName;
                string path2 = appData + "\\PPA\\" + fileName;

                try
                {
                    if (!File.Exists(path))
                    {
                        // This statement ensures that the file is created, 
                        // but the handle is not kept. 
                        using (FileStream fs = File.Create(path)) { }
                    }

                    // Ensure that the target does not exist. 
                    if (File.Exists(path2))
                        File.Delete(path2);

                    // Move the file.
                    File.Move(path, path2);
                }
                catch (Exception er)
                {
                    Console.WriteLine("The process failed: {0}", er.ToString());
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

您可能会遇到写入该文件夹的权限问题。检查您的进程是否具有AppData文件夹的写权限。如果您使用的是IIS,则必须允许的用户使用IIS AppPool\DefaultAppPool格式,您必须将DefaultAppPool替换为您的应用程序池名称。