Interested in using the browser to download the file at the direction of the local address. Those. not to specify the path to save it, and for this purpose browser settings were used. That is why copying the file is not suitable.

There is a method DownloadFile(String, String) class WebClient , which supposedly allows you to do this. But how to correctly specify both parameters - I can not figure it out.

So here. How to correctly declare the parameters in the form: - first: new Uri("c:\\myfile.txt") (if possible). - second: "c: \ myfile.txt"?

Or is another way possible to solve my question?

  • With the help of WebClient you can really "copy" a local file by specifying the path to the file and specifying the name to save (the file will be saved next to the file). That's just a question, and where does the browser? Are you going to use the web client in a browser? - Mstislav Pavlov
  • So I do not need to copy using a browser! I need to have a URI in the form of a local record "c: \ myfile.txt" download to the download folder regardless of which browser I download this file from. The problem is how to properly set the arguments of this method. - vitaliklibra
  • @vitaliklibra Do you want to write files to the browser’s cache? but what is it for? - Stack
  • Did I mention that the case concerns the cache?) When you download a file, for example from Yandex.Disk, for a direct link, do you choose where to download? Or does the default browser offer a save path? I think the second. You just have to click the "Save" button. So I need it. - vitaliklibra
  • @vitaliklibra "the default browser offers a path to save" - this path is most likely taken from the registry. - Stack

3 answers 3

In fact, you need to know which way the browser will download the file.

For Internet Explorer, this is done via P / Invoke + Shell32 (stolen from this answer ):

 [DllImport("Shell32.dll")] private static extern int SHGetKnownFolderPath( [MarshalAs(UnmanagedType.LPStruct)]Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr ppszPath); var downloadFolderGuid = new Guid("{374DE290-123F-4565-9164-39C4925E467B}"); IntPtr pathPtr; int result = SHGetKnownFolderPath( downloadFolderGuid, (uint)0x00004000, new IntPtr(0), out pathPtr); if (result >= 0) return Marshal.PtrToStringUni(pathPtr); throw new ExternalException("Unable to retrieve the known folder path. It may not " + "be available on this system.", result); 

(the full code for other cases and the value of constants, see the response by reference)

For other browsers, you will need methods specific to these browsers.


Having a path, you just need to upload the file to the desired path using webClient.DownloadFile :

 using (var c = new WebClient()) c.DownloadFile("http://www.google.com/", Path.Combine(directory, "index.html")); 

Or copy there, if you have everything within the same host:

 File.Copy(@"C:\test.html", Path.Combine(directory, "test.html")); 
  • Will Microsoft Edge work for the browser? - Stack
  • You need not "http: // ...", but a local address. - vitaliklibra
  • @Stack: I can not say (I do not have 10-ki), but I think that is fine. They aren't deprecate Shell32? - VladD
  • @vitaliklibra: Wait, how is this not http? You want to download a file from the Internet , right? - VladD
  • @VladD "(I don’t have 10s), but I think that is fine " - understandable. Edge is different from IE. - Stack

I do not understand where the browser is, but using WebClient you can do this:

 using (var client = new WebClient()) { client.DownloadFile(@"C:\source.txt", @"C:\destionation.txt"); } 

The first parameter is loaded from, the second where.

But if you need to work only with local files, then it is better to use another class:

 File.Copy(@"C:\source.txt", @"C:\destionation.txt"); 

UPD:

Download using the default path taken for Internet Explorer (from this answer https://stackoverflow.com/a/24673279/4278250 ):

 String path = String.Empty; RegistryKey rKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main"); if (rKey != null) path = (String)rKey.GetValue("Default Download Directory"); if (String.IsNullOrEmpty(path)) path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\downloads"; using (var client = new WebClient()) { client.DownloadFile(@"C:\source.txt", Path.Combine(path, "some file name.txt")); } 
  • I have no way where to save. The path should be the one that is specified by default in the browser. - vitaliklibra 4:05 pm
  • @vitaliklibra, can it %userprofile%\downloads ? - Umed
  • @vitaliklibra, if this is not the way, then this answer will help you here: stackoverflow.com/a/24673279/4278250 - Umed
  • @vitaliklibra, updated the answer - Umed

use the browser to download the file as directed by the local address.

If you need to open a file in WebBrowser, you can do this:

 using System.Windows.Forms; var f = new Form(); var b = new WebBrowser() { Parent = f, Dock = DockStyle.Fill }; b.Navigate(@"C:\Temp\test.txt"); f.ShowDialog(); 

UPDATE:

WebClient.DownloadFile (url, path) is intended for downloading files from web servers. If you do not have a web server on your computer or it is disabled or not configured to work with the specified url, then there will be an error.


The browser gets the path to Downloads by reading the value from the registry.

HKEY_CURRENT_USER \Software \Microsoft \Windows \CurrentVersion \Explorer \User Shell Folders {374DE290-123F-4565-9164-39C4925E467B} = %USERPROFILE%\Downloads

  • This information may be useful to me, but in general it is the path to the standard folder with downloads, but not the browser. Each browser has its own meaning and this is not a universal method at all. - vitaliklibra
  • @vitaliklibra Edge browser reads the value from the registry. can see for yourself. here - Stack