获取WebBrowser .Url属性时访问冲突运行时错误

时间:2012-12-28 18:07:54

标签: c# outlook add-in

尝试在我正在编写的Outlook加载项中获取WebBrowser的.Url属性时出现“访问冲突”运行时错误。需要一些帮助来解决这个问题。研究表明我可能需要设置“FullTrust”权限集,试过没有成功。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security;
using System.Security.Permissions;

namespace MyAddin1
{

public partial class authForm : Form
{

    public string currentUrl;

    public authForm()
    {
        InitializeComponent();
    }


    private void formLoaded(object sender, EventArgs e)
    {
        WebBrowser browser = new WebBrowser();
        browser.Location = new System.Drawing.Point(0, 0);
        browser.Width = 870;
        browser.Height = 510;
        browser.Navigate("https://www.yammer.com/dialog/oauth?client_id=<redacted>&response_type=token");
        browser.Show();

        //runtime error occurs on following line
        currentUrl = browser.Url.ToString();


        browser.Url = new Uri("http://www.yahoo.com");

        browser.Navigated += new WebBrowserNavigatedEventHandler(checkForToken);


        this.Controls.Add(browser);
    }

    private void checkForToken(object sender, EventArgs e)
    {
        MessageBox.Show(currentUrl);
    }




}

}

1 个答案:

答案 0 :(得分:1)

browser.Url最初为null,请按以下方式更改您的代码。

if (browser.Url != null)
{
    currentUrl = browser.Url.ToString();
}