【问题标题】:"could not find a part of the path" DownloadFile“找不到路径的一部分”下载文件
【发布时间】:2012-03-27 23:30:35
【问题描述】:

我正在开发一个 Windows APP,其中 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) 

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

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

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

【问题讨论】:

    标签: c#


    【解决方案1】:

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

    【讨论】:

    • 哈不敢相信我没有看到这一点 - 恭喜你 10k ;-)
    • 你是对的,它现在可以工作了。我想它会在下载的 zip 文件名之后自动命名它。谢谢!
    • 如果这个答案解决了您的问题,您应该接受它作为对社区有益的答案。
    • 不费吹灰之力,正在等待时间缓冲区通过并分心。再次感谢 phoog,恭喜 10k。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 2021-04-20
    相关资源
    最近更新 更多