I'm creating app which has to open files from Explorator. Of course I can do it using args but Explorator opens new app for every file. I'd like to for example send args to existing app - don't open new.
Asked
Active
Viewed 108 times
0

v0id
- 151
- 1
- 12
-
Have you tried anything at all? Is this winforms? – Jcl Apr 22 '16 at 14:45
1 Answers
1
Explorer always opens up a new instance of your application. What you need to do is control whether there's any other open instance, and if it is, pass the command line to it and close your new instance.
There are some classes that may help you in the .NET framework, the easiest way is adding a reference to Microsoft.VisualBasic
(should be in the GAC... and disregard the name, it works for C# too), then you can derive from WindowsFormsApplicationBase
, which does all the boilerplate code for you.
Something like:
public class SingleAppInstance : WindowsFormsApplicationBase
{
public SingleAppInstance()
{
this.IsSingleInstance = true;
this.StartupNextInstance += StartupNextInstance;
}
void StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
{
// here's the code that will be executed when an instance
// is opened.
// the command line arguments will be in e.CommandLine
}
protected override void OnCreateMainForm()
{
// This will be your main form: i.e, the one that is in
// Application.Run() in your original Program.cs
this.MainForm = new Form1();
}
}
Then in your Program.cs
, instead of using Application.Run
, at startup, we do:
[STAThread]
static void Main()
{
string[] args = Environment.GetCommandLineArgs();
var singleApp = new SingleAppInstance();
singleApp.Run(args);
}

Jcl
- 27,696
- 5
- 61
- 92
-
I can't use WindowsFormsApplicationBase because I must edit my code all code. I hope there are solutions without implementing VB. – v0id Apr 22 '16 at 15:14
-
Because you must edit your code all code? What is that supposed to mean? You are not implementing VB in my solution at all... please be more clear, and if you have specific requirements, put them in the question – Jcl Apr 22 '16 at 15:14
-
I don't know much VB. For example there is no Invoke. I have project which should be done all in C# without implementing VB. – v0id Apr 22 '16 at 15:17
-
This is not VB at all, all the code in my answer is C# . The namespace of `WindowsFormsApplicationBase` is called `Microsoft.VisualBasic`, but that's just the namespace name... it's not VB at all. – Jcl Apr 22 '16 at 15:17
-
-
Again, you are not using any VB. This is all C# , and it's a standard class in the .NET Framework – Jcl Apr 22 '16 at 15:18
-
18 errors after change from Form to WindowsFormsApplicationBase. For example "mainForm does not contain a definition for 'Text'". – v0id Apr 22 '16 at 15:20
-
-
public partial class mainForm : WindowsFormsApplicationBase, changed from mainForm : Form – v0id Apr 22 '16 at 15:22
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109931/discussion-between-freshek-and-jcl). – v0id Apr 22 '16 at 15:24
-
And you got that code from where exactly? Nowhere in my answer says you have to change your main form at all. Please, learn to read. I gave very specific instructions (even to a question which showed no effort on research at all) which don't involve changing your main form, at all. If you can't follow directions I think you need some begginer tutorials to C# before you do what you are trying to do. I'm sorry, not interested in being your teacher, so I'm not continuing in the chat. S.O. is not here to teach you how to code, it's here to solve specific questions, and you seem to have too many. – Jcl Apr 22 '16 at 15:25
-
Ok, ok. Now I know what you mean. I have to create new class. Answer accepted. Sorry for problem. – v0id Apr 22 '16 at 15:29