c# - "could not find a part of the path"下载文件

标签 c#

我遇到了一个正在开发的 Windows 应用程序的情况,其中 DownloadFile 抛出“找不到路径的一部分”异常。

我用来将远程 zip 文件保存到我的硬盘驱动器的方法如下所示:

    private void f_begin_download(string remoteURL)
    {
        string directoryName = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        //MessageBox.Show(directoryName);

        filePath = directoryName + "\\tmp\\";
        filePath = f_make_directory(filePath);

        Uri remoteURI = new Uri(remoteURL);
        System.Net.WebClient downloader = new System.Net.WebClient();

        downloader.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(f_downloader_DownloadFileCompleted);
        try
        {
            downloader.DownloadFile(remoteURI, filePath);
        }
        catch (Exception ex)
        {
            MessageBox.Show(remoteURI.ToString());
            MessageBox.Show(filePath);
            MessageBox.Show(ex.ToString());
        }
    }

此方法实际上在我的应用程序目录中创建了/tmp/文件夹。它也成功创建了文件夹:

    public static string f_make_directory(string path)
    {
        try
        {
            System.IO.DirectoryInfo newFolder = new System.IO.DirectoryInfo(path);
            if (!newFolder.Exists)
            {
                newFolder.Create();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        return path;
    }

但是当我运行这个过程时,异常看起来是这样的:

System.Net.WebException: An exception occurred during a WebClient request. -->     System.IO.DirectoryNotFoundException: Could not find a part of the path' C:\Users\Hudson Atwell\Desktop\The Big Test Folder\tmp\'. 
at System.IO.__Error.WinIOError(Int32 errorCode,String maybeFullPath) 
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRight, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msyPath, Boolean bFromProxy, Boolean useLongPath) 
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) at System.Net.WebClient.DownloadFile(Uri address, String fileName) 

在 WindowsFormsApplication1.window_updater.f_begin_download(String remoteURL) 在 C:\Users\Hudson Atwell\Documents\Visual Studio\Projects\Test Project\Test Project]Windows\window_update.cs:line 125

我也要求以管理员身份运行该解决方案。

对我做错了什么有什么建议吗?

最佳答案

您传递的是一个目录名,而该方法需要一个文件名。尝试创建具有该名称的文件失败,因为已经存在具有该名称的目录。

关于c# - "could not find a part of the path"下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9899169/

相关文章:

c# - 使用动态参数的 Web 部署 XML 文件参数化

c# - Kendo 小部件和 Bootstrap 输入组插件

c# - 在 .NET 中使用 key 的独占锁

c# - 在标签中使用尖括号

c# - 如何检查列表框中的项目类型

c# - 如何重命名动态创建的 TreeView 项目的标题

c# - ValidationRule 的执行集-C# 类设计 - 更好的方法

c# - 如何将 System.Web.Cache 传递到我的服务层

c# - 带有 IXmlSerializable 的命名空间前缀

c# - 为什么我的 MVC 操作没有模型绑定(bind)我的集合?