【问题标题】:Open link in browser from code behind of installed application C#从已安装应用程序 C# 后面的代码在浏览器中打开链接
【发布时间】:2016-06-17 23:01:25
【问题描述】:

我做了一个winform应用程序。当我在 Visual Studio 中运行该应用程序时,以下代码可以从 DataGridView 链接列打开一个链接。

System.Diagnostics.Process.Start("chrome.exe",
grdRelLinks.Rows[e.RowIndex].Cells[2].Value.ToString());

但是当我安装构建并尝试做同样的事情时,什么也没有发生。我还需要做其他设置吗?

请帮忙。

【问题讨论】:

  • 安装后是否知道在哪里可以找到chrome.exe?您的应用是否具有启动chrome.exe 所需的权限?
  • 您可以将链接地址传递给System.Diagnostincs.Process.Start以使用默认浏览器打开地址。
  • @SamerTufail :嗯,我是 winforms 的新手,我不知道你的建议。你能详细说明一下吗?
  • @user2998990 chrome.exe 是否位于本地的特定位置?像这样可能是System.Diagnostics.Process.Start(@"C:/mybrowser/chrome.exe", grdRelLinks.Rows[e.RowIndex].Cells[2].Value.ToString());?您需要提供chrome.exe 所在位置的正确路径。
  • Ok 会试试这个并更新你。

标签: c# winforms


【解决方案1】:

如果您想从DataGridView 打开链接,您实际上应该传递 url 而不是网络浏览器,即:

System.Diagnostics.Process.Start(grdRelLinks.Rows[e.RowIndex].Cells[2].Value.ToString());

它最终会尝试使用操作系统的默认浏览器打开给定的 url。 Ofc 确保来自 url 的 url 链接格式正确。

如果chrome.exe 无法启动,不妨尝试缩短一个:chrome? 你能否确认Win+R(又名Run...)和chrome.exe实际上打开了Chrome? 如果没有,你能检查一下 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\ 包含 chrome.exe 条目?

如果是这样,可能是 url 格式错误?

【讨论】:

  • 但我不知道这是哪个链接。 grdRelLinks.Rows[e.RowIndex].Cells[2].Value.ToString() 这一行给我网址
  • 确保链接以httphttps 开头是值得的。否则,它将失败。
  • 这就是我最初所做的。但它不起作用,当我使用 vs 时,它可以工作,但安装时它不起作用。
  • 他提到他希望它运行 chrome。也许应该说清楚。
【解决方案2】:

您可以在浏览器中使用以下 sn-ps 打开一个 URL:

Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = "http://google.com";
process.Start();

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

在您的示例中,为了允许用户从 DataGridView 启动它,您应该像这样简单地定义一个点击事件:

private void grdRelLinks_CellContentClick(object pSender, DataGridViewCellEventArgs pArgs)
{
    if (pArgs.RowIndex > -1 && pArgs.ColumnIndex == 2)
    {
        string url = grdRelLinks.Rows[pArgs.RowIndex].Cells[pArgs.ColumnIndex].Value.ToString();

        if(!string.IsNullOrWhiteSpace(url))
            System.Diagnostics.Process.Start(url);
    }
}

【讨论】:

  • @user2998990 如果将 url 放在 windows 运行函数 (Windows + R) 中会发生什么?这会为您启动应用程序吗?
  • 我做了一个快速的谷歌搜索,看看你的问题可能是什么原因,并偶然发现了以下答案:stackoverflow.com/a/10504367/4579864
【解决方案3】:

这对我有用。

    private void OnGridViewContentClick(object sender, EventArgs e)
    {
        string chromeExePath = CheckIfChromeIsInstalled();
        if (!string.IsNullOrEmpty(chromeExePath))
        {
            MessageBox.Show("Yayy Chrome.exe was found !");
            //Path is not null:
            Process.Start(chromeExePath, "http://www.google.de");//Here you can also enter the URL you get from your GridView
            string url = grdRelLinks.Rows[e.RowIndex].Cells[2].Value.ToString();
            if(!url.StartsWith("http")
            {
               url = $"http://{url}";
            }
            Process.Start(chromeExePath, url);
        }
        else
        {
            MessageBox.Show("Chrome.exe not found");
        }
    }

    private string CheckIfChromeIsInstalled()
    {
        DirectoryInfo programFiles = new DirectoryInfo(Environment.GetEnvironmentVariable("PROGRAMFILES"));//Find your Programs folder
        DirectoryInfo[] dirs = programFiles.GetDirectories();
        List<FileInfo> files = new List<FileInfo>();
        Parallel.ForEach(dirs, (dir) =>
        {
            files.AddRange(dir.GetFiles("chrome.exe", SearchOption.AllDirectories)); //Search for Chrome.exe
        });
        //files should only contain 1 entry
        //Return path of chrom.exe or null
        return (files.Count > 0) ? files[0].FullName : null;
    }

注意:在一个额外的线程中启动它可能很有用!

编辑: 你能检查一下cmd.exe 是否与 start chrome.exe "your URL" 一起使用吗?!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    相关资源
    最近更新 更多