C# – 如何在Windows文件资源管理器的活动窗口中收集文件选择?

huangapple go评论73阅读模式
英文:

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

问题

我正在为我的公司的产品数据管理“vault”创建一个插件,该“vault”集成到Windows文件资源管理器中。我已成功创建了一个自定义文件资源管理器上下文菜单命令,命名为“SW Quick View”,仅在“vault”内部可用。但是,我无法弄清如何收集活动资源管理器窗口中选择的文件(以便获取它们的文件路径),然后右键单击所选内容以在上下文菜单弹出窗口中运行“SW Quick View”选项。

一些可视上下文(AXC_VAULT)

我唯一能够收集文件选择的方法是通过OpenFileDialog类。这导致了一种相当笨拙的工作流程,您需要在文件资源管理器中的任何位置右键单击(空白区域/文件夹/文件都无关紧要),选择“SW Quick Viewer”,然后会弹出一个OpenFileDialog对话框,然后您进行选择并单击“打开”。

我只希望用户能够使用他们的一个(可能有很多)文件资源管理器窗口,在其中突出显示文件,右键单击,选择“SW Quick View”,然后将收集所选文件的所有路径并运行逻辑,无需弹出或进一步的用户输入。

我的工作代码:

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)
        {
            // 在此处确认工作的“SW Quick View”逻辑
        }
    }
}
英文:

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
            }
        }
    }

答案1

得分: 1

感谢 @bigcrazyal。从您的评论中,我能够获得所需的结果。


为了进一步扩展bigcrazyal的评论并展示一个工作示例:

我使用了默认的Solidworks Enterprise PDM插件结构中的 ref EdmCmdData[] ppoData,如此处所述:

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);
    }
}



<details>
<summary>英文:</summary>

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


----------
To further expand on bigcrazyal&#39;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][1]

    private void OpenWithQuickViewer(EdmCmdData[] ppoData)
        {
            ISldWorks sldWorks = new SldWorks();
            IEdmVault5 vault = new EdmVault5();
            vault.LoginAuto(&quot;AXC_VAULT&quot;, 1);

            IEdmFolder5 folder = (IEdmFolder5)vault.GetObject(EdmObjectType.EdmObject_Folder, ppoData[0].mlObjectID3);

            foreach (EdmCmdData item in ppoData)
            {
                string filePath = $@&quot;{folder.LocalPath}\{item.mbsStrData1}&quot;;
                IDocumentSpecification docSpec = sldWorks.GetOpenDocSpec(filePath);
                string fileExt = Path.GetExtension(filePath);

                if (fileExt == &quot;.SLDDRW&quot;)
                {
                    docSpec.DetailingMode = true;
                }
                else if (fileExt == &quot;.SLDASM&quot;)
                {
                    docSpec.ViewOnly = true;
                }
                else if (fileExt == &quot;.SLDPRT&quot;)
                {
                    docSpec.ViewOnly = true;
                }
                IModelDoc2 modelDoc2 = sldWorks.OpenDoc7(docSpec);
            }
        }


  [1]: https://help.solidworks.com/2022/english/api/epdmapi/csharpaddins.htm

</details>



huangapple
  • 本文由 发表于 2023年7月14日 02:34:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定