簡體   English   中英

如何從我的應用程序打開網頁?

[英]How to open a web page from my application?

我想讓我的 WPF 應用程序打開默認瀏覽器並轉到某個網頁。 我怎么做?

對於 .NET 的桌面版本:

System.Diagnostics.Process.Start("http://www.webpage.com");

對於 .NET Core, ProcessStartInfo.UseShellExecute的默認值已從true更改為false ,因此您必須將其顯式設置為true才能正常工作:

System.Diagnostics.Process.Start(new ProcessStartInfo
    {
        FileName = "http://www.webpage.com",
        UseShellExecute = true
    });

更復雜的是,對於 UWP 應用程序,不能將此屬性設置為true (因此這些解決方案都不適用於 UWP)。

接受的答案不再適用於.NET Core 3 要使其工作,請使用以下方法:

var psi = new ProcessStartInfo
{
    FileName = url,
    UseShellExecute = true
};
Process.Start (psi);

我一直在使用這一行來啟動默認瀏覽器:

System.Diagnostics.Process.Start("http://www.google.com"); 

雖然已經給出了一個很好的答案(使用Process.Start ),但將其封裝在檢查傳遞的字符串確實是 URI 的函數中更安全,以避免在機器上意外啟動隨機進程。

public static bool IsValidUri(string uri)
{
    if (!Uri.IsWellFormedUriString(uri, UriKind.Absolute))
        return false;
    Uri tmp;
    if (!Uri.TryCreate(uri, UriKind.Absolute, out tmp))
        return false;
    return tmp.Scheme == Uri.UriSchemeHttp || tmp.Scheme == Uri.UriSchemeHttps;
}

public static bool OpenUri(string uri) 
{
    if (!IsValidUri(uri))
        return false;
     System.Diagnostics.Process.Start(uri);
     return true;
}

我想讓我的 WPF 應用程序打開默認瀏覽器並轉到某個網頁。 我怎么做?

您無法從提升的應用程序啟動網頁。 這將引發 0x800004005 異常,可能是因為 explorer.exe 和瀏覽器正在非提升運行。

要在非提升的 Web 瀏覽器中從提升的應用程序啟動網頁,請使用Mike Feng 制作代碼 我試圖將 URL 傳遞給 lpApplicationName 但這沒有用。 當我將 CreateProcessWithTokenW 與 lpApplicationName = "explorer.exe"(或 iexplore.exe)和 lpCommandLine = url 一起使用時,也不會。

以下解決方法確實有效:創建一個具有一項任務的小型 EXE 項目:Process.Start(url),使用 CreateProcessWithTokenW 運行此 .EXE。 在我的 Windows 8 RC 上,這可以正常工作並在 Google Chrome 中打開網頁。

這是我如何打開的完整代碼。

有2個選項:

  1. 使用默認瀏覽器打開(行為就像在瀏覽器窗口中打開一樣)

  2. 通過默認命令選項打開(行為就像你使用“RUN.EXE”命令)

  3. 通過“資源管理器”打開(行為就像您在文件夾窗口網址中寫入網址一樣)

[可選建議] 4.使用iexplore進程位置打開需要的url

代碼:

internal static bool TryOpenUrl(string p_url)
    {
        // try use default browser [registry: HKEY_CURRENT_USER\Software\Classes\http\shell\open\command]
        try
        {
            string keyValue = Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Classes\http\shell\open\command", "", null) as string;
            if (string.IsNullOrEmpty(keyValue) == false)
            {
                string browserPath = keyValue.Replace("%1", p_url);
                System.Diagnostics.Process.Start(browserPath);
                return true;
            }
        }
        catch { }

        // try open browser as default command
        try
        {
            System.Diagnostics.Process.Start(p_url); //browserPath, argUrl);
            return true;
        }
        catch { }

        // try open through 'explorer.exe'
        try
        {
            string browserPath = GetWindowsPath("explorer.exe");
            string argUrl = "\"" + p_url + "\"";

            System.Diagnostics.Process.Start(browserPath, argUrl);
            return true;
        }
        catch { }

        // return false, all failed
        return false;
    }

和助手功能:

internal static string GetWindowsPath(string p_fileName)
    {
        string path = null;
        string sysdir;

        for (int i = 0; i < 3; i++)
        {
            try
            {
                if (i == 0)
                {
                    path = Environment.GetEnvironmentVariable("SystemRoot");
                }
                else if (i == 1)
                {
                    path = Environment.GetEnvironmentVariable("windir");
                }
                else if (i == 2)
                {
                    sysdir = Environment.GetFolderPath(Environment.SpecialFolder.System);
                    path = System.IO.Directory.GetParent(sysdir).FullName;
                }

                if (path != null)
                {
                    path = System.IO.Path.Combine(path, p_fileName);
                    if (System.IO.File.Exists(path) == true)
                    {
                        return path;
                    }
                }
            }
            catch { }
        }

        // not found
        return null;
    }

希望我有所幫助。

老派的方式;)

public static void openit(string x) {
   System.Diagnostics.Process.Start("cmd", "/C start" + " " + x); 
}

使用: openit("www.google.com");

我測試過它的工作完美

我一直在使用這一行來啟動默認瀏覽器:

System.Diagnostics.Process.Start("http://www.google.com");

我有這個解決方案,因為我今天有一個類似的問題。

假設您想從以管理員權限運行的應用程序打開http://google.com

ProcessStartInfo startInfo = new ProcessStartInfo("iexplore.exe", "http://www.google.com/");
Process.Start(startInfo); 
    string target= "http://www.google.com";
try
{
    System.Diagnostics.Process.Start(target);
}
catch (System.ComponentModel.Win32Exception noBrowser)
{
    if (noBrowser.ErrorCode==-2147467259)
    MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other)
{
    MessageBox.Show(other.Message);
}

我想讓我的WPF應用程序打開默認瀏覽器並轉到某個網頁。 我怎么做?

我認為這個是最常用的:

System.Diagnostics.Process.Start("http://www.google.com");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM