1

In a visual basic (.NET) project I am using CefSharp (Nugget). How can I change the browser download folder? In the official CefSharp documentation the developers talk about the IDownloadHandler interface. This class is used to handle CefSharp file downloads. Based on what I investigated, I could try to instantiate IDownloadHandler like this:

public class DownloadHandler : IDownloadHandler
{
    public event EventHandler<DownloadItem> OnBeforeDownloadFired;

    public event EventHandler<DownloadItem> OnDownloadUpdatedFired;

    public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
    {
        var handler = OnBeforeDownloadFired;

        if (handler != null)
        {
            handler(this, downloadItem);
        }

        if (!callback.IsDisposed)
        {
            using (callback)
            {
                callback.Continue(@"C:\Users\" + 
                        System.Security.Principal.WindowsIdentity.GetCurrent().Name. + 
                        @"\Downloads\" + 
                        downloadItem.SuggestedFileName, 
                    showDialog: false);
            }
        }
    }

    public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
    {
        var handler = OnDownloadUpdatedFired;
        if (handler != null)
        {
            handler(this, downloadItem);
        }
    }
}

My question is, where should I apply this code, I am guessing not in my visual basic program, which makes me think the changes need to be done on the source code of CerfSharp? If so, how can I do it?

Thanks a lot everyone!

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • You implement idownloadhandler in your code, then you set the ChromiumWebBrowser.DownloadHandler property to a new instance of your class. – amaitland Jul 03 '20 at 07:22
  • Yes, in your VB program. That is C# code so you would have to write the VB equivalent. You define a class that implements that `IDownloadHandler` interface. If you don't know how to implement interfaces in VB then you have some reading to do, but I'm sure that there are examples around for this interface in VB. You can name it what you like but `DownloadHandler` is a logical choice. – jmcilhinney Jul 03 '20 at 07:54
  • By the way, that's not the best way to get the current user's Downloads folder. I have a dedicated data drive and that code would not work for me. See [here](https://stackoverflow.com/questions/3795023/how-to-programmatically-derive-windows-downloads-folder-userprofile-downloads) for a method to get the correct folder path. – jmcilhinney Jul 03 '20 at 07:59

0 Answers0