How to overcome Process.Start limitation

B

Breakable

Hi, I am having a lot of pain with a simple issue:
Trying to execute a command with parameters
something like "iexplore http://google.com". I am not sure what the
command will be or how the command will be formatted, so I would love
not to have to parse it (cover all situations), however
Process.Start
gives me "The system cannot find the file specified" error when i try
to
Process.Start("iexplore http://google.com");
which I am not able to overcome.
Of course this works:
Process.Start("iexplore","http://google.com");
but now I need to parse, which I would like to avoid.
The only workaround I see is creating a batch file, which I would love
to avoid.
Is there any way to execute a command line from .net without parsing
it first?
 
J

Jackie

Hi, I am having a lot of pain with a simple issue:
Trying to execute a command with parameters
something like "iexplore http://google.com". I am not sure what the
command will be or how the command will be formatted, so I would love
not to have to parse it (cover all situations), however
Process.Start
gives me "The system cannot find the file specified" error when i try
to
Process.Start("iexplore http://google.com");
which I am not able to overcome.
Of course this works:
Process.Start("iexplore","http://google.com");
but now I need to parse, which I would like to avoid.
The only workaround I see is creating a batch file, which I would love
to avoid.
Is there any way to execute a command line from .net without parsing
it first?

This should open in the default browser...

ProcessStartInfo startInfo = new ProcessStartInfo("http://www.google.com");
startInfo.UseShellExecute = true;
Process.Start(startInfo);
 
J

Jackie

Hi, I am having a lot of pain with a simple issue:
Trying to execute a command with parameters
something like "iexplore http://google.com". I am not sure what the
command will be or how the command will be formatted, so I would love
not to have to parse it (cover all situations), however
Process.Start
gives me "The system cannot find the file specified" error when i try
to
Process.Start("iexplore http://google.com");
which I am not able to overcome.
Of course this works:
Process.Start("iexplore","http://google.com");
but now I need to parse, which I would like to avoid.
The only workaround I see is creating a batch file, which I would love
to avoid.
Is there any way to execute a command line from .net without parsing
it first?

Your code should probably work if you add the directory to iexplore.exe
into the PATH environment variable, because the system does not
automatically know exactly where iexplore.exe is located.
I believe you want to do what I suggested in my previous post, however.
 
P

Patrice

How are defined the command and its parameters ?

Assuming you want to let the user define what to start you can take the
"External Tool" feature of VS as an example. It provides the user with :
- a textbox that allows to enter the command (including a browsing button
that allows to select the file including the path)
- a text box that allows to enter the arguments

Parsing is not general (ie "a b c" could be launching "a b" with argument
"c" or launching "a" with "b c", you can assume the later but strictly
speaking there is no way to choose between both).
Also if the user doesn't provide the path to the EXE file, then you would
have to find it (or even worse you could use another EXE file that the one
intended just because it is in the search path or the same command could
change later if the search path is altered etc...).

The UI described above and that is used in VS, solves those problems (by
letting the user to enter the command including the full path and its
arguments separately).

--
Patrice

"Breakable" <[email protected]> a écrit dans le message de groupe de
discussion :
(e-mail address removed)...
 
J

Jackie

Your code should probably work if you add the directory to iexplore.exe
into the PATH environment variable, because the system does not
automatically know exactly where iexplore.exe is located.
I believe you want to do what I suggested in my previous post, however.

Maybe I didn't really need to give you any examples, probably because
the iexplorer was not found anywhere in the current directory,
system/windows directory, in any path in the PATH environment variable
or anywhere else it tries to look.
 
J

Jackie

This should open in the default browser...

ProcessStartInfo startInfo = new ProcessStartInfo("http://www.google.com");
startInfo.UseShellExecute = true;
Process.Start(startInfo);

Apparantly this works as well...

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

I made some assumptions when I (too) quickly glanced over your post. I
should have read it more carefully and given it some more thought before
posting my reply.
 
F

Family Tree Mike

Apparantly this works as well...

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

I made some assumptions when I (too) quickly glanced over your post. I
should have read it more carefully and given it some more thought before
posting my reply.

Technically, that is not the answer to what the user asked. Your code
example will open the url with the default browser. The question asked
specifies to use IE. Forcing IE to open is likely to madden many users
though.
 
J

Jackie

Technically, that is not the answer to what the user asked. Your code
example will open the url with the default browser. The question asked
specifies to use IE. Forcing IE to open is likely to madden many users
though.

Yes. I was a bit too quick (like I said). However, he said he got the
message "The system cannot find the file specified", and if you see my
other reply, I made an attempt to explain why this happens.
 
J

Jackie

Yes. I was a bit too quick (like I said). However, he said he got the
message "The system cannot find the file specified", and if you see my
other reply, I made an attempt to explain why this happens.

After reading my own reply, my tone may sound a little harsh and I did
not mean that. Please just ignore my previous replies as they do not
really help at all. I must admmit that while I wanted to help, I have
failed to really understand the OP's actual problem here.
 
J

Jackie

After reading my own reply, my tone may sound a little harsh and I did
not mean that. Please just ignore my previous replies as they do not
really help at all. I must admmit that while I wanted to help, I have
failed to really understand the OP's actual problem here.

I understand the problem now and I feel so stupid. I can get overly
excited and sometimes even do more than I needed to. It is just past 2
am here now as well and obviously a bad time for me to post anything at
all. I will give better answers in the future. I am sorry to have
cluttered the group and maybe caused inconvenience to the OP.
 
B

Breakable

Thank you everyone for trying to help, but I don't see a solution
yet.
Just to be absolutely clear "iexplore http://google.com" was provided
just as an example - to reproduce the situation - I don't need to open
pages or start internet explorer. It can be just as well "notepad
a.txt"
To clarify again the problem is that
Process.Start("notepad a.txt")
gives me "The system cannot find the file specified" exception.
Where the
Process.Start("notepad","a.txt")
works.

The PATH variable seem not to effect Process.Start, but it has some
interesting, but unrelevant effects:
Because "C:\Program Files\Internet Explorer" is not in the PATH
running "iexplore http://google.com" from cmd shell does not work,
but it still works if you run it from Start->Run (and even from
Process.Start("iexplore","http://www.on.lt")) - how can this be????

Another interesting part is that in VBA running
Shell "notepad a.txt" works. Where
Process.Start("notepad a.txt") does not
Still probably the best thing would be to get the same behavior as
Start->Run offers.
I think probably the main problem is that some code is raising this
"The system cannot find the file specified" where it should not.
Probably removing some "If File.Exists" in Process.Start would solve
all the issues.
 
B

Breakable

The PATH variable seem not to effect Process.Start, but it has some
interesting, but unrelevant effects:
Because "C:\Program Files\Internet Explorer" is not in the PATH
running "iexplore http://google.com" from cmd shell does not work,
but it still works if you run it from Start->Run (and even from
Process.Start("iexplore","http://www.on.lt")) - how can this be????
So it seems the iexplore is a special case in windows. When you
perform Process.Start and the executable name is "iexplore", windows
adds "C:\Program Files\Internet Explorer" to the beginning of the PATH
variable.
Now I just need to a way to modify the path and execute a shell
command to reproduce the Start->Run behavior.
 
B

Breakable

Ok, it seems i got somewhere with this code:
private static void RunAsRunBox(string something)
{
if (something.ToLower().Contains("IEXPLORE".ToLower()))
{
string iexploreLocation =
(string)Microsoft.Win32.Registry.GetValue(@"HKEY_CLASSES_ROOT
\Applications\iexplore.exe\shell\open\command", "",
"\"C:\\Program Files\\Internet Explorer\
\IEXPLORE.EXE\" %1");

iexploreLocation = iexploreLocation.Trim("
%1\"".ToCharArray());
iexploreLocation =
Path.GetDirectoryName(iexploreLocation);

string pathName = "path";
string pathRez =
System.Environment.GetEnvironmentVariable(pathName);
if (!pathRez.Contains(iexploreLocation))
{

System.Environment.SetEnvironmentVariable(pathName, string.Format("{0};
{1}", iexploreLocation, pathRez));
}
}


string command = something;
Microsoft.VisualBasic.Interaction.Shell(command,
Microsoft.VisualBasic.AppWinStyle.NormalFocus, false, -1);
}

Still it takes into account only one special case. I wonder is there
is more surprises?
Maybe I should work on the whole "HKEY_CLASSES_ROOT\Applications"
branch?
Or maybe there is a more simple way to solve this?
 
B

Breakable

Ok, the final code follows. I am trying to reproduce run dialog
behavior. I wish I would not have to do this, but the choices seem
clear:
1)Either parse the command line to know where are the parameters and
where is the executable
2)Or reproduce the run dialog behavior.
#1 would be better, but I am not sure how to address all the cases.

Non working methods:
1)Write a bat file - this will not work because path is not populated
2)Just run it with "Microsoft.VisualBasic.Interaction.Shell". Same as
#1
3)Adding start at the front of command (is not very good because
spaces in executable path will **** it up ie:"start c:\Program Files
\Internet Explorer\iexplore http://www.google.com".
4)Set environment from command line would not work


My code:

/// <summary>
/// Finds all paths to add to the path enviroment variable
/// </summary>
/// <param name="command">The command.</param>
/// <param name="currentPaths">The current paths.</param>
/// <returns></returns>
static string FindAllPathsToAdd(string command, string
currentPaths)
{
string paths = "";
command = command.ToLower();
Microsoft.Win32.RegistryKey key =
Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("Applications");
foreach (string keyName in key.GetSubKeyNames())
{
string noExeKeyName = RemoveExe(keyName).ToLower();
if (command.Contains(noExeKeyName))
{
string commandPath = GetCommandPath(keyName);
if (commandPath != "")
{
if (!
currentPaths.ToLower().Contains(commandPath.ToLower()))
{
if (paths != "")
{
paths += ";";
}
paths += commandPath;
}
}
}
}

if (paths == "")
{
return currentPaths;
}
else
{
return string.Format("{0};{1}", paths, currentPaths);
}
}

/// <summary>
/// Gets the path for an executable.
/// </summary>
/// <param name="keyName">Name of the key.</param>
/// <returns></returns>
private static string GetCommandPath(string keyName)
{
string location = string.Format(@"HKEY_CLASSES_ROOT
\Applications\{0}\shell\open\command", keyName);

string rezValue =
(string)Microsoft.Win32.Registry.GetValue(@"HKEY_CLASSES_ROOT
\Applications\iexplore.exe\shell\open\command", "","");

rezValue = rezValue.Trim(" %1\"".ToCharArray());
rezValue = Path.GetDirectoryName(rezValue);
return rezValue;
}

/// <summary>
/// Removes the .exe from the key name
/// </summary>
/// <param name="keyName">Name of the key.</param>
/// <returns></returns>
private static string RemoveExe(string keyName)
{
string strToRemove = ".exe";
if (keyName.ToLower().EndsWith(strToRemove))
{
return keyName.Substring(0, keyName.Length -
strToRemove.Length);
}
return keyName;
}


/// <summary>
/// Runs a command as run dialog.
/// </summary>
/// <param name="something">Something.</param>
private static void RunAsRunBox(string something)
{
string pathName = "path";
string pathRez =
System.Environment.GetEnvironmentVariable(pathName);
string added = FindAllPathsToAdd(something, pathRez);
if (added != pathRez)
{
System.Environment.SetEnvironmentVariable(pathName,
added);
}

try
{
string command = something;
Microsoft.VisualBasic.Interaction.Shell(command,
Microsoft.VisualBasic.AppWinStyle.NormalFocus, false, -1);
}
catch (FileNotFoundException)
{
//Some commands such as webpages or documents does not
work from shell
Process.Start(something);
}

if (added != pathRez)
{
//Restore the enviroment
System.Environment.SetEnvironmentVariable(pathName,
pathRez);
}
}
 
J

Jackie

Ok, the final code follows. I am trying to reproduce run dialog
behavior. I wish I would not have to do this, but the choices seem
clear:
1)Either parse the command line to know where are the parameters and
where is the executable
2)Or reproduce the run dialog behavior.
#1 would be better, but I am not sure how to address all the cases.

Non working methods:
1)Write a bat file - this will not work because path is not populated
2)Just run it with "Microsoft.VisualBasic.Interaction.Shell". Same as
#1
3)Adding start at the front of command (is not very good because
spaces in executable path will **** it up ie:"start c:\Program Files
\Internet Explorer\iexplore http://www.google.com".
4)Set environment from command line would not work


My code:

I think I would just go the parsing way. It's not that hard to do
either. I assume there are some free simple parsers out there for this
purpose if you don't want to write one on your own.

If I understand this correctly now...

A user could type in "notepad something.txt", and your application knows
that "notepad" is the executable/command and "something.txt" is a part
of the arguments.

Then you just specify the user-inputted executable/command as the first
argument to Process.Start() and the user-inputted arguments as the
second argument.

Do I understand this correctly?
 
J

John Vottero

Breakable said:
Thank you everyone for trying to help, but I don't see a solution
yet.
Just to be absolutely clear "iexplore http://google.com" was provided
just as an example - to reproduce the situation - I don't need to open
pages or start internet explorer. It can be just as well "notepad
a.txt"
To clarify again the problem is that
Process.Start("notepad a.txt")
gives me "The system cannot find the file specified" exception.
Where the
Process.Start("notepad","a.txt")
works.

I think you can do:

Process.Start("cmd.exe /C notepad a.txt")

which simple involves adding "cmd.exe /C " to the beginning of your string.
 
B

Breakable

I think I would just go the parsing way. It's not that hard to do
either. I assume there are some free simple parsers out there for this
purpose if you don't want to write one on your own.

If I understand this correctly now...

A user could type in "notepad something.txt", and your application knows
that "notepad" is the executable/command and "something.txt" is a part
of the arguments.

Then you just specify the user-inputted executable/command as the first
argument to Process.Start() and the user-inputted arguments as the
second argument.

Do I understand this correctly?
Yes, you understand correctly.
I was not able to find any parsers that are not huge (~2mb), just to
do this simple task.
 
B

Breakable

Breakable said:
I think you can do:

Process.Start("cmd.exe /C notepad a.txt")

which simple involves adding "cmd.exe /C " to the beginning of your string.
This is basically the same as adding "start " at the beginning. I
believe there can be some pitfalls, but I am not sure what they are.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top