3

I would like to get process ID and I try this code:

Process myProcess = new Process();
myProcess.StartInfo.FileName = "http://somesite.com";
myProcess.Start();
logs.logging("Afetr Open" + myProcess.Id, 8);

But I get an exception in line with myProcess.Id:

no process is associated with this object

krolik1991
  • 244
  • 1
  • 4
  • 13
  • Are you sure that by the time you are calling `myProcess.Id`, your process hasn't already finished? – Darin Dimitrov Jan 04 '16 at 15:56
  • Possible duplicate of [Find process id when Process.Start returns null?](http://stackoverflow.com/questions/12077856/find-process-id-when-process-start-returns-null) – Andrew Grinder Jan 04 '16 at 15:57
  • 3
    You're using ShellExecute to open a link which won't return a process ID. Pretty sure this is intended. [This post](http://stackoverflow.com/questions/9110564/why-would-process-waitforexit-throw-a-no-process-exception-even-when-a-process) explains it better. If you want the process ID you'd have to specify which browser to use. – Equalsk Jan 04 '16 at 16:18

2 Answers2

3

If you change myProcess.StartInfo.FileName = "http://somesite.com"; to myProcess.StartInfo.FileName = "cmd"; code works. I think first code doesn't create proces, it only call system to open link.

You can manualy call browser. eg.

Process myProcess = Process.Start("iexplore", "http://somesite.com");        
var id = myProcess.Id;
BWA
  • 5,672
  • 7
  • 34
  • 45
  • It works, thanks :) but now I have the other question - can I run two other IE processes in other tabs not in other browsers? – krolik1991 Jan 05 '16 at 06:49
  • Some solution is [here](http://stackoverflow.com/questions/3713206/launch-a-url-in-a-tab-in-an-existing-ie-window-from-c-sharp) – BWA Jan 05 '16 at 08:31
1

You can try it will first get the path of the browser then it will Start it by passing URL as arguemnt.

    var path = GetStandardBrowserPath();
    var process = Process.Start(path , "http://www.google.com");
    int processId = process.Id ;

it will find the default browser path what ever is.

 private static string GetStandardBrowserPath()
        {
            string browserPath = string.Empty;
            RegistryKey browserKey = null;

            try
            {
                //Read default browser path from Win XP registry key
                browserKey = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);

                //If browser path wasn't found, try Win Vista (and newer) registry key
                if (browserKey == null)
                {
                    browserKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http", false); ;
                }

                //If browser path was found, clean it
                if (browserKey != null)
                {
                    //Remove quotation marks
                    browserPath = (browserKey.GetValue(null) as string).ToLower().Replace("\"", "");

                    //Cut off optional parameters
                    if (!browserPath.EndsWith("exe"))
                    {
                        browserPath = browserPath.Substring(0, browserPath.LastIndexOf(".exe") + 4);
                    }

                    //Close registry key
                    browserKey.Close();
                }
            }
            catch
            {
                //Return empty string, if no path was found
                return string.Empty;
            }
            //Return default browsers path
            return browserPath;
        }

see Source

sm.abdullah
  • 1,777
  • 1
  • 17
  • 34