开发者

C# loading files from arguments

开发者 https://www.devze.com 2023-02-01 02:18 出处:网络
in my application I\'ve registered a file type (.asm) with my application (it\'s a tabbed n开发者_如何学Gootepad application), and when those files are double-clicked they open with my application thr

in my application I've registered a file type (.asm) with my application (it's a tabbed n开发者_如何学Gootepad application), and when those files are double-clicked they open with my application through the arguments passed when it's loaded:

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

Now the problem is, while this does work, if one instance of my application is already running, whenever a file is opened, a new instance of it is created rather than a new tab being opened in the current instance which I don't want. So I thought of checking if the program is already running, and if it is then I would call a separate function in the main form to load that document instead. But the problem is, I don't know how you call a function in Main.cs from Program.cs, how do we do that?


It's more complicated than just calling a function in Main.cs from Program.cs because the OS will launch a second process for you at the time you double-click your registered files. You need some way to find out if there is another existing process running already and then communicate with that existing process, if there is one.

Fortunately, there is a class in the .NET Framework that already does all of the hard work for you:

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


The technique in this question might help:

C# : how to - single instance application that accepts new parameters?

0

精彩评论

暂无评论...
验证码 换一张
取 消