I've run into a similar problem before. where I was creating an application and I only wanted one instance to run. Then I created a
Here is how I would tackled it.
I created a WCF Pipe Service:
[ServiceContract]
interface DNRIService
{
[OperationContract]
void OpenDnt(string path);
[OperationContract]
void OpenPak(string path);
[OperationContract]
bool IsOnline();
[OperationContract]
void Activate();
}
Then I did this :
public Main(String [] args) : this()
{
try
{
using (ChannelFactory<DNRIService> serviceFactory = new ChannelFactory<DNRIService>(new NetNamedPipeBinding(), new EndpointAddress(PipeService)))
{
var channel = serviceFactory.CreateChannel();
if(channel.IsOnline())
{
foreach (var argument in args)
{
var argTrim = argument.Trim() ;
if (argTrim.EndsWith(".dnt"))
channel.OpenDnt(argument);
else if (argTrim.EndsWith(".pak"))
channel.OpenPak(argument);
}
channel.Activate();
Close();
}
}
}
catch
{
@this = new ServiceHost(this, new Uri(PipeName));
@this.AddServiceEndpoint(typeof(DNRIService), new NetNamedPipeBinding(), PipeService);
@this.BeginOpen((IAsyncResult ar) => @this.EndOpen(ar), null);
foreach (var argument in args)
{
var argTrim = argument.Trim();
if (argTrim.EndsWith(".dnt"))
OpenDnt(argument);
else if (argTrim.EndsWith(".pak"))
OpenPak(argument);
}
}
}
Notice that it tries to open with the same name. If it can't then it closes and assumes that another one is already open. It then tries to send its arguments to the original.
You only need to do a slight modification to get the current user.
This question should help you with that:
How do I get the current username in .NET using C#?
The rest is for you to implement. If you want to see the full code from which I posted its located here :
https://github.com/Aelphaeis/Dragon-Nest-Resource-Inspector
(Running it will give you an idea of how it works in only allowing a single instance).