• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Single Instance Application with Associated File Extension

All,

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.

TIA,
Jason
0
jason-bullard
Asked:
jason-bullard
  • 2
  • 2
1 Solution
 
oobaylyCommented:
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?
0
 
jason-bullardAuthor Commented:
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>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            SingleInstance.Run();
        }
    }
 
    internal static class SingleInstance
    {
        [DllImport("user32.dll")]
        private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
 
        [DllImport("user32.dll")]
        private static extern int SetForegroundWindow(IntPtr hWnd);
 
        [DllImport("user32.dll")]
        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;
                    break;
                }
            }
            return hWnd;
        }
 
        private static void SwitchToCurrentInstance()
        {
            IntPtr hWnd = GetCurrentInstanceWindowHandle();
            if (hWnd != IntPtr.Zero)
            {
                if (IsIconic(hWnd) != 0)
                {
                    ShowWindow(hWnd, SW_RESTORE);
                }
 
                // Set foreground window.
                SetForegroundWindow(hWnd);
            }
        }
 
        public static bool Run()
        {
            if (IsAlreadyRunning())
            {
                //set focus on previously running app
                SwitchToCurrentInstance();
                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)
                mutex.ReleaseMutex();
 
            return !bCreatedNew;
        }
 
        static Mutex mutex;
        const int SW_RESTORE = 9;
    }
}

Open in new window

0
 
oobaylyCommented:
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.
0
 
jason-bullardAuthor Commented:
Actually, I found the solution to what I was looking for.

http://www.codeproject.com/KB/cs/ipc_wmcopy.aspx
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now