"could not find part of the path" DownloadFile

I have a situation with Windows APP. I am developing where DownloadFile throws an “cannot find part of the path” exception.

The method that I use to save the remote zip file to my hard drive is as follows:

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 the application directory. It also successfully creates the folder:

  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 start 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) 

in 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 need the solution to run as an administrator.

Any tips on what I'm doing wrong?

+4
source share
1 answer

You pass in the directory name while the method expects the file name. An attempt to create a file with this name failed because there already is a directory with that name.

+8
source

Source: https://habr.com/ru/post/1403775/


All Articles