C # loading files from arguments

in my application, I registered a file type (.asm) with my application (this is a tabbed notebook application), and when these files are double-clicked, they open with my application through the arguments passed at startup:

static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Main(args)); } 

Now the problem is that, although it works, if one instance of my application is already running, whenever a file is opened, a new instance is created, and not a new tab opened in the current instance, I don’t want to. So I decided to check if the program is running, and if so, I would call a separate function in the main form to load this document. But the problem is that I do not know how you call the function in Main.cs from Program.cs, how do we do it?

+4
source share
2 answers

This is more complicated than just calling a function in Main.cs from Program.cs , because the OS will start the second process for you at the time you double-click your registered files. You need to somehow find out if there is another existing process that is already running, and then contact this existing process, if any.

Fortunately, the .NET Framework has a class that already does all the hard work:

Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase

See this blog for a complete example: http://windowsclient.net/blogs/suryahg/archive/2008/08/20/use-of-microsoft-visualbasic-applicationservices-part-2.aspx

+2
source
+1
source

Source: https://habr.com/ru/post/1333133/


All Articles