首页 文章

如何从WPF应用程序中将Windows资源管理器打开到某个目录?

提问于
浏览
128

在WPF应用程序中,当用户单击某个按钮时,我想将Windows资源管理器打开到某个目录,我该怎么做?

我希望这样的事情:

Windows.OpenExplorer("c:\test");

4 回答

  • 10

    为什么不 Process.Start(@"c:\test");

  • 13

    这应该工作:

    Process.Start(@"<directory goes here>")
    

    或者,如果您想要一种运行程序/打开文件和/或文件夹的方法:

    private void StartProcess(string path)
        {
            ProcessStartInfo StartInformation = new ProcessStartInfo();
    
            StartInformation.FileName = path;
    
            Process process = Process.Start(StartInformation);
    
            process.EnableRaisingEvents = true;
        }
    

    然后调用方法并在括号中放置文件和/或文件夹的目录或应用程序的名称 . 希望这有帮助!

  • 1

    你可以使用 System.Diagnostics.Process.Start .

    或者直接使用WinApi,如下所示,它将启动explorer.exe . 您可以使用ShellExecute的第四个参数为它提供一个起始目录 .

    public partial class Window1 : Window
    {
        public Window1()
        {
            ShellExecute(IntPtr.Zero, "open", "explorer.exe", "", "", ShowCommands.SW_NORMAL);
            InitializeComponent();
        }
    
        public enum ShowCommands : int
        {
            SW_HIDE = 0,
            SW_SHOWNORMAL = 1,
            SW_NORMAL = 1,
            SW_SHOWMINIMIZED = 2,
            SW_SHOWMAXIMIZED = 3,
            SW_MAXIMIZE = 3,
            SW_SHOWNOACTIVATE = 4,
            SW_SHOW = 5,
            SW_MINIMIZE = 6,
            SW_SHOWMINNOACTIVE = 7,
            SW_SHOWNA = 8,
            SW_RESTORE = 9,
            SW_SHOWDEFAULT = 10,
            SW_FORCEMINIMIZE = 11,
            SW_MAX = 11
        }
    
        [DllImport("shell32.dll")]
        static extern IntPtr ShellExecute(
            IntPtr hwnd,
            string lpOperation,
            string lpFile,
            string lpParameters,
            string lpDirectory,
            ShowCommands nShowCmd);
    }
    

    声明来自pinvoke.net website .

  • 263
    Process.Start("explorer.exe" , @"C:\Users");
    

    我不得不使用这个,只是指定tgt dir的另一种方法是在我的应用程序终止时关闭资源管理器窗口 .

相关问题