From: Breakable on
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.
From: Breakable on
On May 20, 2:58 pm, Breakable <igalve...(a)gmail.com> wrote:
> 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.
From: Breakable on
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?
From: Breakable on
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);
}
}


From: Jackie on
On 5/20/2010 16:17, Breakable wrote:
> 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?