Single Instance Application with Associated File Extension

Posted on 2009-04-20
Last Modified: 2012-05-06

Hopefully someone might be able to point me in the right direction.  I am currently writing an application that must be single instance (already coded) but has the ability to open with an associated file extension.  Now, everything works fine if I double-click on an associated file but the problem I am running into is if the application is already running and extension is double-clicked.

Basically, I am trying to figure out a way to get the file information when the application is already running.

Question by:jason-bullard
    LVL 15

    Expert Comment

    How are you managing the Singleton functionality? Are you enumerating the processes in the entry point (public static void Main).

    "I am trying to figure out a way to get the file information", do you mean the file that has just been double-clicked, or the file that was double clicked to open the existing instance?

    Author Comment

    I found a snippet on CodeProject which is below.  All I need to do is get the path to the file that was double-clicked to open the application.  So if I click on hello.txt it would return the path to that specified file so I can open it even if the application is already running.
    namespace MyApplication
        using System;
        using System.IO;
        using System.Text;
        using System.Resources;
        using System.Threading;
        using System.Reflection;
        using System.Diagnostics;
        using System.Windows.Forms;
        using System.Runtime.InteropServices;
        static class Program
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            static void Main(string[] args)
        internal static class SingleInstance
            private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
            private static extern int SetForegroundWindow(IntPtr hWnd);
            private static extern int IsIconic(IntPtr hWnd);
            private static IntPtr GetCurrentInstanceWindowHandle()
                IntPtr hWnd = IntPtr.Zero;
                Process process = Process.GetCurrentProcess();
                Process[] processes = Process.GetProcessesByName(process.ProcessName);
                foreach (Process _process in processes)
                    if (_process.Id != process.Id &&
                        _process.MainModule.FileName == process.MainModule.FileName &&
                        _process.MainWindowHandle != IntPtr.Zero)
                        hWnd = _process.MainWindowHandle;
                return hWnd;
            private static void SwitchToCurrentInstance()
                IntPtr hWnd = GetCurrentInstanceWindowHandle();
                if (hWnd != IntPtr.Zero)
                    if (IsIconic(hWnd) != 0)
                        ShowWindow(hWnd, SW_RESTORE);
                    // Set foreground window.
            public static bool Run()
                if (IsAlreadyRunning())
                    //set focus on previously running app
                    return false;
                Application.Run(new FormMain());
                return true;
            private static bool IsAlreadyRunning()
                string strLoc = Assembly.GetExecutingAssembly().Location;
                FileSystemInfo fileInfo = new FileInfo(strLoc);
                string sExeName = fileInfo.Name;
                bool bCreatedNew;
                mutex = new Mutex(true, "Global\\" + sExeName, out bCreatedNew);
                if (bCreatedNew)
                return !bCreatedNew;
            static Mutex mutex;
            const int SW_RESTORE = 9;

    Open in new window

    LVL 15

    Expert Comment

    Use the args array passed by Main. The 1st item should contain the filename double-clicked on.

    Windows should always pass the filename as the 1st argument to the executable, however you'd have to check the file associations you've created to confirm that.

    Accepted Solution

    Actually, I found the solution to what I was looking for.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video discusses moving either the default database or any database to a new volume.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now