1

i'm new in C# programming so I beg your pardon. I just tried the solution proposed in a previous question, but it didn't work. By the way I stil need help. I'm trying to write a simple consolle app in C# that opens a web page, aka http://www.libero.it. Here you are my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

class OpenWeb {

     static void Main(string[] args) {
        var prs = new ProcessStartInfo("chrome.exe");
        prs.Arguments = "http://www.libero.it";
      //  Process.Start(prs);
       try
    {
        Process.Start(prs);
    }
    catch (System.ComponentModel.Win32Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
     }
}

I don't understand why my code doesn't work, there is always an exeception, without the try-catch is the following:

Unhandled Exception: System.ComponentModel.Win32Exception: The specified file         
could not be found
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo     
startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at OpenWeb.Main(String[] args) in C:\Users\p3rtp\myProve\Prove.cs:line 12

I use Windows 7x64, with .Net core sdk 2.1.301 e MS Visual Studio Code 1.24.1.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
anthomer
  • 89
  • 1
  • 9
  • 2
    chrome.exe is not in PATH - or even, it might not be installed. – Theraot Jul 09 '18 at 13:37
  • Do you have chrome.exe in your path, i.e. would you expect this program to find it from just chrome.exe? You could pass the URL to the shell instead so that it will use its configured browser, and it would know the path to that. – Rup Jul 09 '18 at 13:37
  • 2
    Possible duplicate of [How to open a web page from my application?](https://stackoverflow.com/questions/502199/how-to-open-a-web-page-from-my-application) – Theraot Jul 09 '18 at 13:38
  • 2
    you need to define the absolute path of Chrome.exe for example "C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe" – S.Fragkos Jul 09 '18 at 13:38
  • 1
    chrome.exe is very unlikely to be in the same folder as your .NET application. If you don't specify a folder, .NET will look for this file in the folder where it's running, and in the Windows PATH variable for the current user. So unless chrome.exe is also in the user's PATH variable, then this will fail. Either add it to PATH or use the full folder name as well. Also there's no guarantee Chrome will be installed on a user's machine - maybe you need a fallback mechanism to make it run I.E. instead if Chrome can't be found. – ADyson Jul 09 '18 at 13:57
  • unfortunately, chrome is already in the PATH. I already tried to write the full path of chrome instead of chrome.exe: without success! – anthomer Jul 10 '18 at 08:35

3 Answers3

4

I've been using this line to launch the default browser:

System.Diagnostics.Process.Start("http://www.google.com");
Feras Al Sous
  • 1,073
  • 1
  • 12
  • 23
1

The code is really simple as below:

using System
using System.Collections.Generic
using System.Text
using System.Diagnostics
namespace example
{
   class examp
   {
      static void Main(string[] args)
      {
         Process.Start("http://www.google.com");
      }
   }
}
DeshDeep Singh
  • 1,817
  • 2
  • 23
  • 43
1

Ok, I found the right answer:

var prs = new ProcessStartInfo(@"C:\Program Files 
   (x86)\Google\Chrome\Application\chrome.exe");

The problem isn't the Path, but in the app or folder: just the quotes with "@" before the full path of chrome I finally managed to solve the problem. Thanks to all anyway.

Feras Al Sous
  • 1,073
  • 1
  • 12
  • 23
anthomer
  • 89
  • 1
  • 9