55

I am using the following code to fire the iexplore process. This is done in a simple console app.

public static void StartIExplorer()
{
    var info = new ProcessStartInfo("iexplore");
    info.UseShellExecute = false;
    info.RedirectStandardInput = true;
    info.RedirectStandardOutput = true;
    info.RedirectStandardError = true;

    string password = "password";
    SecureString securePassword = new SecureString();

    for (int i = 0; i < password.Length; i++)
        securePassword.AppendChar(Convert.ToChar(password[i]));

    info.UserName = "userName";
    info.Password = securePassword;
    info.Domain = "domain";

    try
    {
        Process.Start(info);
    }
    catch (System.ComponentModel.Win32Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

The above code is throwing the error The system cannot find the file specified. The same code when run without specifying the user credentials works fine. I am not sure why it is throwing this error.

Can someone please explain?

user1623521
  • 340
  • 1
  • 16
Rashmi Pandit
  • 23,230
  • 17
  • 71
  • 111

6 Answers6

67

Try to replace your initialization code with:

ProcessStartInfo info 
    = new ProcessStartInfo(@"C:\Program Files\Internet Explorer\iexplore.exe");

Using non full filepath on Process.Start only works if the file is found in System32 folder.

devdigital
  • 34,151
  • 9
  • 98
  • 120
Jojo Sardez
  • 8,400
  • 3
  • 27
  • 38
  • 4
    We should specify the full file name as the UseShellExecute is set to false. – Rashmi Pandit Mar 03 '10 at 06:25
  • @Rashmi Pandit - yes we should. I already encountered the same problem before :). Don't forget to accept and upvote the answer :) – Jojo Sardez Mar 03 '10 at 06:43
  • 6
    Of course, you should *really* replace it with the *actual* path to the program on the user's system. There isn't always a C: drive, and the program folder isn't always spelled "Program Files." – Rob Kennedy Mar 03 '10 at 06:45
  • 9
    -1 The code may well work but the reason given is totally wrong. – Fraser Aug 15 '13 at 13:25
  • 2
    @Fraser I encourage everyone to verify the information in the comments discussion as well – nik.shornikov Sep 26 '13 at 16:42
  • Most times I like to get to the bottom of things, really understand how they work, this was not one of those times, thank you @Jojo. – Ron.B.I Apr 29 '18 at 22:21
  • This worked. I have try this on notepad and it worked well but any idea for osk.exe, since osk.exe was same file in System32 folder. osk.exe didn't work for me. Any suggestion? – Wei Chun Mar 27 '19 at 08:30
15

You can't use a filename like iexplore by itself because the path to internet explorer isn't listed in the PATH environment variable for the system or user.

However any path entered into the PATH environment variable allows you to use just the file name to execute it.

System32 isn't special in this regard as any directory can be added to the PATH variable. Each path is simply delimited by a semi-colon.

For example I have c:\ffmpeg\bin\ and c:\nmap\bin\ in my path environment variable, so I can do things like new ProcessStartInfo("nmap", "-foo") or new ProcessStartInfo("ffplay", "-bar")

The actual PATH variable looks like this on my machine.

%SystemRoot%\system32;C:\FFPlay\bin;C:\nmap\bin;

As you can see you can use other system variables, such as %SystemRoot% to build and construct paths in the environment variable.

So - if you add a path like "%PROGRAMFILES%\Internet Explorer;" to your PATH variable you will be able to use ProcessStartInfo("iexplore");

If you don't want to alter your PATH then simply use a system variable such as %PROGRAMFILES% or %SystemRoot% and then expand it when needed in code. i.e.

string path = Environment.ExpandEnvironmentVariables(
       @"%PROGRAMFILES%\Internet Explorer\iexplore.exe");
var info = new ProcessStartInfo(path);
Fraser
  • 15,275
  • 8
  • 53
  • 104
  • 2
    Could you explain what the @ is for, and why it is red even though its outside the quotations? – kingfrito_5005 Aug 29 '17 at 19:00
  • 2
    @kingfrito_5005 It marks the string as a string literal so that anything that would normally be interpreted as an escape character  is ignored. So that you don't need to double backslash the paths - "\\path\\without" vs @"\path\with" – Fraser Aug 29 '17 at 21:14
  • Thanks! Its so fun to discover these useful new pieces of information while looking for something totally different! – kingfrito_5005 Aug 30 '17 at 13:38
0

Also, if your PATH's dir is enclosed in quotes, it will work from the command prompt but you'll get the same error message

I.e. this causes an issue with Process.Start() not finding your exe:

PATH="C:\my program\bin";c:\windows\system32

Maybe it helps someone.

Macke
  • 24,812
  • 7
  • 82
  • 118
0

I had the same problem, but none of the solutions worked for me, because the message The system cannot find the file specified can be misleading in some special cases.

In my case, I use Notepad++ in combination with the registry redirect for notepad.exe. Unfortunately my path to Notepad++ in the registry was wrong.

So in fact the message The system cannot find the file specified was telling me, that it cannot find the application (Notepad++) associated with the file type(*.txt), not the file itself.

0xBADF00D
  • 980
  • 1
  • 12
  • 25
0

I know it's a bit old and although this question have accepted an answer, but I think its not quite answer.

Assume we want to run a process here C:\Program Files\SomeWhere\SomeProcess.exe.

One way could be to hard code absolute path:

new ProcessStartInfo(@"C:\Program Files\SomeWhere\SomeProcess.exe")

Another way (recommended one) is to use only process name:

new ProcessStartInfo("SomeProcess.exe")

The second way needs the process directory to be registered in Environment Variable Path variable. Make sure to add it in System Variables instead of Current User Variables, this allows your app to access this variable.

Mohammad Barbast
  • 1,753
  • 3
  • 17
  • 28
-8

You can use the folowing to get the full path to your program like this:

Environment.CurrentDirectory
  • 3
    This does NOT give you the path to your program. This gives you the current directory, which is something completely different. The current directory can be different to start, and it can change at any time during program execution. – James Jul 26 '17 at 19:56