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);
}
}