I wonder if someone can provide either a link or an explanation on how Windows Shell passes, for example, a url to the default application, Internet Explorer, Google Chrome, or a parameter through to a custom built application.
I have looked at examples like: Run shell commands using c# and get the info into string
The problem with the above link and other links like it, are they do not work as is expected or do not actually answer the actual question. For Example...
This Code work's as expected:
private string EXEPath = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe";
Process process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = EXEPath,
Arguments = "http://www.google.com.au",
UseShellExecute = true,
RedirectStandardOutput = false,
CreateNoWindow = true
}
};
process.Start();
This link gives a good example of how to set the application to a single instance application: http://social.msdn.microsoft.com/Forums/vstudio/en-US/a5bcfc8a-bf69-4bbc-923d-f30f9ecf5f64/single-instance-application
Processing the args like so:
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(args));
}
and in the Form1 then handling the args like so:
private string[] Args;
public MINION(string[] args)
{
InitializeComponent();
// Assign the incoming Arguments...
this.AppArgs = args;
}
public string[] AppArgs
{
set
{
this.Args = value;
if (this.Args.Length > 0)
{
foreach (string arg in this.Args)
{
// Do the processing here of each arg...
}
}
}
}
I am using Click Once. I have the app set with the VB Single Instance code in the above link. This Code works and only a Single Instance of the Application runs.
My Problem:
If I pass an argument to my Application, tests show the Process ID is not the same as the Process ID of the running instance. Thus showing that the instance of the Application that is already running is not the instance that Args get passed to with the code:
private string EXEPath = @"C:\Program Files (x86)\My App\My App.exe"; // Or actual path to EXE.
Process process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = EXEPath,
Arguments = "http://www.google.com.au",
UseShellExecute = true,
RedirectStandardOutput = false,
CreateNoWindow = true
}
};
process.Start();
So this code starts a new instance even if the code is in place to set the Application to a single instance app. The existing Application Instance running does not process the Args passed.
The new instance will display a Coded Messagebox("I have run..."), but then exits after that.
EDIT @loopedcode - First Answer. This is the Code I use to ensure I am talking to the same EXE at all times. This works a treat also. Unfortunately I have covered your suggestion already.
Process[] runningProcess = Process.GetProcessesByName("MyEXEName.exe");
Process proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = runningProcess[0].MainModule.FileName,
Arguments = "http://www.google.com.au",
UseShellExecute = true,
RedirectStandardOutput = false,
CreateNoWindow = true
}
};
proc.Start();
I can verify that exactly the same path is valid for the Running Instance and also the Shell Call to the EXE's Path.
EDIT - 11.08.14
I would like to expand the question a little.
When Passing the Parameter, I get all the way to the Processing of the argument in the new Process ID, then the code that switches the Instance to Single Instance Application back to the existing Instance kicks in. At this stage the Args are not passed to the existing instance.
If I use:
MessageBox.Show("Program ID: " + (Process.GetCurrentProcess().Id) + " Arg Passed: " + this.Args[0]);
It runs on the new Instance but not the Existing Instance.