Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"could not find a part of the path" DownloadFile

Tags:

c#

I've a situation with a windows APP I'm developing where the DownloadFile is throwing an "could not find a part of the path" exception.

The method I'm using to save a remote zip file to my hard drive looks like this:

    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());
        }
    }

This method actually creates the /tmp/ folder in my application directory. It does successfully create the folder too:

    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;
    }

But when I run the process the exception looks like this:

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:line 125

I am requiring the solution to be ran as an administrator too.

Any advice to what I'm doing wrong?

like image 516
atwellpub Avatar asked Jan 18 '23 01:01

atwellpub


1 Answers

You're passing a directory name, while the method expects a file name. The attempt to create a file with that name fails, because there's already a directory with that name.

like image 139
phoog Avatar answered Jan 25 '23 20:01

phoog