“找不到路径的一部分”DownloadFile

时间:2012-03-27 23:30:36

标签: c#

我遇到了一个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) 

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

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

对我做错的任何建议?

1 个答案:

答案 0 :(得分:8)

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