user2998990
user2998990

Reputation: 980

Open link in browser from code behind of installed application C#

I have made a winform application. When I run the app in visual studio, following code works to open a link from DataGridView link column.

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

But when I install the build and try to do the same thing, nothing happens. Is there any other setting that I need to make.

Please help.

Upvotes: 2

Views: 4023

Answers (3)

Felix D.
Felix D.

Reputation: 5103

This worked for me.

    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;
    }

NOTE: Starting this in an extra Thread could be useful !

EDIT : Can you please check if cmd.exe works with start chrome.exe "your URL" ?!

Upvotes: 0

mwilczynski
mwilczynski

Reputation: 3082

If you want to open link link from your DataGridView, you should actually pass url not web browser, ie.:

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

It will end up trying to open given url with default browser for OS. Ofc make sure that link of url from url is properly formatted.

If chrome.exe doesn't work for launching, maybe try shortened one: chrome? Can you also confirm that Win+R (a.k.a. Run...) and then chrome.exe actually opens up Chrome? If not, can you check if HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\ contains chrome.exe entry?

If so, maybe url formatting is wrong?

Upvotes: 4

Oceans
Oceans

Reputation: 3509

You can open a URL in browser with the following snippets:

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

or

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

In your example, to allow users to launch it from the DataGridView, you should simply define a click event like this:

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);
    }
}

Upvotes: 0

Related Questions