Search code examples
c#windowsregedit

How to execute multiple ".ext" with an associate aplication made in C#


I've made an application to do some work with an extension for example ".ext" I added the necessaries entries to the Windows "regedit" for executing the ".ext" when I open it. Now, if I have selected multiple files.ext and I oppen it; then multiples instances of my application arise.

My application receives a String[] args as parameters, so if I open a file.ext then I receive the file path in args[0], the idea is when I open multiple files then receive the first file path as args[0] and the second file path as args[1], and so on, but right now when I open multiple selected files then multiples instances of my applications arise.

The question is: How can I do for execute multiple files.ext and get the paths in my String[] args for all the selected files.ext and not in different instances of my app. Exactly like the VLC player when we select multiples files.mp3 and we open it.


Solution

  • Finally i found an alternative.
    Running only an instance of my app with "Mutex", and importing the shell32.dll i did it.
    Somthing like this...

     private static bool isAlreadyInstantiated()
        {
            new Mutex(true, "my_app", out mutex_bool);
            return !mutex_bool;
        }
    
    
        private static void getSelectedFiles()
        {
            selected_files = new List<SelectedFile>();
    
            String filename;
            // Required ref: SHDocVw (Microsoft Internet Controls COM Object)
            ShellWindows shell = new SHDocVw.ShellWindows();
    
            foreach (SHDocVw.InternetExplorer window in shell)
            {
    
                filename = Path.GetFileNameWithoutExtension(window.FullName).ToLower();
                if (filename.ToLowerInvariant() == "explorer")
                {
                    Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
    
                    if (selected_files.Count == 0)
                    {
                        foreach (Shell32.FolderItem item in items)
                        {
                            if (active_folder == Path.GetDirectoryName(item.Path))
                                selected_files.Add(new SelectedFile("\"" + item.Path + "\"", item.Name, Math.Round(((double)item.Size / (double)1024), 2)));
                            else
                                break;
                        }
                    }
                    else
                        return;
                }
            }
        }