2

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.

user2998990
  • 970
  • 5
  • 18
  • 36
  • Does it know where to find `chrome.exe` once its installed? and does your app have the required permissions to launch the `chrome.exe`? – Samer Tufail Mar 04 '16 at 09:24
  • You can pass the link address to `System.Diagnostincs.Process.Start` to open the address using default browser. – Reza Aghaei Mar 04 '16 at 09:25
  • @SamerTufail : Well, I am new to winforms and I dont know what you suggesting. Can you please elaborate.? – user2998990 Mar 04 '16 at 09:26
  • @user2998990 chrome.exe resides in a particular location right locally? like this could be `System.Diagnostics.Process.Start(@"C:/mybrowser/chrome.exe", grdRelLinks.Rows[e.RowIndex].Cells[2].Value.ToString());` ? you need to provide the correct path to where `chrome.exe` is. – Samer Tufail Mar 04 '16 at 09:30
  • Ok will try this and update you. – user2998990 Mar 04 '16 at 09:32
  • @SamerTufail : I tried that also also, getting complete path from registry and passing it as a parameter. Still no luck. – user2998990 Mar 04 '16 at 09:44
  • @user2998990 u tried my suggestion ? – Felix D. Mar 04 '16 at 10:07

3 Answers3

4

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?

mwilczynski
  • 3,062
  • 1
  • 17
  • 27
0

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);
    }
}
Oceans
  • 3,445
  • 2
  • 17
  • 38
  • @user2998990 What happens if you place the url in the windows run function (Windows + R) ? Does this launch the application for you? – Oceans Mar 04 '16 at 09:53
  • I did a quick google to see what could be the cause of your problem and stumbled upon the following answer: http://stackoverflow.com/a/10504367/4579864 – Oceans Mar 04 '16 at 09:55
0

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" ?!

Felix D.
  • 4,811
  • 8
  • 38
  • 72