C# 如何更改权限级别,下载文件

C# 如何更改权限级别,下载文件,c#,winforms,url,browser,download,C#,Winforms,Url,Browser,Download,我尝试创建一个web浏览器。目前,我尝试实现一个功能,如果用户想要下载一些文件,将显示一个附加窗口,其中包含已下载文件的列表。如果文件已经加载,则会显示一条消息(只是一个想法) 到目前为止,我在主窗体中获得了指向文件位置的链接,并将其发送到另一个窗体: DownLoadFile dlf = new DownLoadFile(); ... WebBrowser wb = new WebBrowser(); wb.Navigating += new WebBrowse

我尝试创建一个web浏览器。目前,我尝试实现一个功能,如果用户想要下载一些文件,将显示一个附加窗口,其中包含已下载文件的列表。如果文件已经加载,则会显示一条消息(只是一个想法)

到目前为止,我在主窗体中获得了指向文件位置的链接,并将其发送到另一个窗体:

DownLoadFile dlf = new DownLoadFile();
...
        WebBrowser wb = new WebBrowser();
        wb.Navigating += new WebBrowserNavigatingEventHandler(wb_Navigating);
...
    private void wb_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
...
        if (e.Url.ToString().EndsWith(".mp3"))
        {
            dlf.DownloadPath = e.Url;
            dlf.Show();
        }
    }
在新表单中,我尝试使用此链接下载文件:

public Uri DownloadPath { get; set; }
...

private void DownLoadFile_Load(object sender, EventArgs e)
    {
        string filePath = null;

        //get FileName from URL 
        string[] ArrayForName;
        ArrayForName = DownloadPath.ToString().Split('/');
        saveFileDialogFile.FileName = 
            ArrayForName[ArrayForName.Length-1].Replace("%"," ").Trim();

        if (saveFileDialogFile.ShowDialog() == DialogResult.OK)
        {
            WebClient client = new WebClient();
            //get Url
            Uri url = new Uri(DownloadPath.ToString());     
            //get place where want to save with default name
            filePath = saveFileDialogFile.FileName;
            //event for result
            client.DownloadFileCompleted += 
                new System.ComponentModel.AsyncCompletedEventHandler (client_DownloadFileCompleted);
            //download
            client.DownloadFileAsync(url, filePath);
        }
    }

    void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        MessageBox.Show("Compleated");
    }
我的问题是:

  • 关于
    if(例如:Url.ToString().EndsWith(“.mp3”)
    -我该怎么办 更改此选项不仅是为了了解用户尝试下载mp3文件的时间, 但是所有类型的文件-也许有更好的方法

  • 如果我想直接使用某个链接下载文件,我会收到一条消息“当前您不需要该链接的权限”-我如何才能下载 更改我的web浏览器的权限级别

  • 如果我最终得到一个指向该文件的链接并开始下载它,那么结果就是文件名(文件大小为0 kb)-我错了

  • 我的解决方案(可能不是最好的)

    为webBrowser创建事件

    wb.Navigating += new WebBrowserNavigatingEventHandler(wb_Navigating);
    
    在这种情况下,使用next

            if (GetWorkingWebBrowser().StatusText != null)
            {
                try
                {
                    WebRequest request = WebRequest.Create(GetWorkingWebBrowser().StatusText);
                    request.Method = "HEAD";
    
                    using (WebResponse response = request.GetResponse())
                    {
                        if (response.ContentLength > 0 && 
                             !response.ContentType.ToString().ToLower().Contains("text/html"))
                        {
                            dlf.DownloadPath = e.Url; //move url to my form for dwnload
                            dlf.Show(); //show form
                        }
                    }
                }
                catch (UriFormatException)
                {
                }
                catch (WebException)
                {
                }
            }
    

    GetWorkingWebBrowser()
    -返回选项卡上当前活动webBrowser的方法,表示
    webBrowser

    是否对url的构造进行了单元测试?否,能否提供一些url测试示例?