【问题标题】:Unable to download file into AppData folder无法将文件下载到 AppData 文件夹
【发布时间】:2015-06-21 23:53:30
【问题描述】:

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

例外:

“System.Net.WebException”类型的未处理异常发生在 系统.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());
                }
            }
        }

【问题讨论】:

标签: c#


【解决方案1】:

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

【讨论】:

  • 我的机器上没有使用 IIS 或任何服务器,我必须在机器上本地运行服务器吗?
  • 你在写什么样的应用程序?不是网络应用吗?
  • 不,它是 C# 中的 Windows WPF 应用程序
  • 好吧,AppData 文件夹让我很困惑。检查您的用户是否对该文件夹具有写入权限。如果它位于文件系统的“敏感”区域,您可能还需要提升运行应用程序的权限。
  • 另外,在调用 DownloadFile 之前确保 PPA 子文件夹存在。我不确定它会为你创建它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
  • 2012-01-09
  • 1970-01-01
  • 2021-05-28
  • 2019-02-20
相关资源
最近更新 更多