Search code examples
c#windows-explorersolidworksmicrosoft-file-explorersolidworkspdmapi

C# — How to collect a selection of files in the active window of Windows File Explorer?


I'm creating an add-in for my company's product data management "vault" which is integrated into Windows File Explorer. I've successfully created a custom File Explorer Context Menu command called SW Quick View, available only from within the Vault. However, I can not figure out how to collect the selected files in the active Explorer window (so that I can get their file paths) and right click the selection to run the SW Quick View option in the context menu pop-up. Some visual context (AXC_VAULT)

The only way I've been able to collect a selection of files is through the OpenFileDialog class. This leads to a rather clunky workflow where you need to right click anywhere in File Explorer (whitespace/folder/file makes no difference) select SW Quick Viewer, an OpenFileDialog box pops up, and then you make your selection and click Open.


I just want the user to be able to use their one (of likely many) windows of File Explorer, highlight files, right click, select SW Quick View, which then will collect all paths of the selected files and run the logic with no pop-ups or further user input required.


My working code:

    public static void OpenWithQuickViewer()
    {
        string[] fileNames;
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Multiselect = true;
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            fileNames = ofd.FileNames;
            foreach (string filePath in fileNames)
            {
                // Confirmed working "SW Quick View" logic here
            }
        }
    }

Solution

  • Thank you @bigcrazyal. From your comment I was able to get the desired result.


    To further expand on bigcrazyal's comment and show a working example:

    I used the ref EdmCmdData[] ppoData from the default structure of the Solidworks Enterprise PDM add-in as outlined here

    private void OpenWithQuickViewer(EdmCmdData[] ppoData)
        {
            ISldWorks sldWorks = new SldWorks();
            IEdmVault5 vault = new EdmVault5();
            vault.LoginAuto("AXC_VAULT", 1);
    
            IEdmFolder5 folder = (IEdmFolder5)vault.GetObject(EdmObjectType.EdmObject_Folder, ppoData[0].mlObjectID3);
    
            foreach (EdmCmdData item in ppoData)
            {
                string filePath = $@"{folder.LocalPath}\{item.mbsStrData1}";
                IDocumentSpecification docSpec = sldWorks.GetOpenDocSpec(filePath);
                string fileExt = Path.GetExtension(filePath);
    
                if (fileExt == ".SLDDRW")
                {
                    docSpec.DetailingMode = true;
                }
                else if (fileExt == ".SLDASM")
                {
                    docSpec.ViewOnly = true;
                }
                else if (fileExt == ".SLDPRT")
                {
                    docSpec.ViewOnly = true;
                }
                IModelDoc2 modelDoc2 = sldWorks.OpenDoc7(docSpec);
            }
        }