在与父文件夹相同的窗口中查看子文件夹?

时间:2018-07-17 02:15:29

标签: c# windows shellexecute

我有一个父文件夹,它包含子文件夹和其他文件(文本文件,图像,可执行文件..),它们都放在同一路径中(实际上,这是其父文件夹的路径)。

SendKeys.SendWait("sub");
SendKeys.SendWait("{ENTER}");

以上两行执行我需要的操作;这是在其父文件夹视图的同一窗口中打开名为“ sub”的文件夹而不打开一个新文件夹。(图片显示了我想避免的情况,假设我想找到执行等效操作的最佳方法“选择”子文件夹,然后按Enter在同一窗口中打开它而无需启动单独的新文件夹的操作)

enter image description here

有什么建议吗?我在ShellExecuteA()中阅读了有关默认动词的信息,但这似乎对我不起作用;我的代码是

using System.Runtime.InteropServices;

namespace testFaza
{
    class Program
    {
        [DllImport("shell32.dll")]
        static extern int ShellExecuteA(int hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);

        static void Main(string[] args)
        {

            string path = @"sub\";
            ShellExecuteA(0, null, path, null, null, 1);
        }
    }
}

当我释放它然后执行它时,输出结果类似于第一张图片:

enter image description here

编辑:

string path = @"sub\";
dynamic ws = Microsoft.VisualBasic.Interaction.CreateObject("WScript.Shell");       
ws.Run("explorer.exe /select, " + (char)34 + path + (char)34);

然后用一个SendKey按下{ENTER},这是我到目前为止遇到的最好的解决方案。

1 个答案:

答案 0 :(得分:1)

Explorer.exe支持 / SELECT 命令行参数,它将选择文件/文件夹,例如子文件夹:

string sPath = "c:\bin\release\sub";

//Start a Window and reliably select a subfolder
Process.Start("explorer.exe", " /select," & sPath);

//Open the folder
SendKeys.SendWait("{ENTER}");