在 WPF 应用程序中,当用户单击按钮时,我想将 Windows 资源管理器打开到某个目录,我该怎么做?
我会期待这样的事情:
Windows.OpenExplorer("c:\test");
在 WPF 应用程序中,当用户单击按钮时,我想将 Windows 资源管理器打开到某个目录,我该怎么做?
我会期待这样的事情:
Windows.OpenExplorer("c:\test");
为什么不Process.Start(@"c:\test");
呢?
Process.Start("explorer.exe" , @"C:\Users");
我不得不使用它,另一种仅指定 tgt 目录的方式会在我的应用程序终止时关闭资源管理器窗口。
这应该有效:
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;
}
然后调用该方法并在括号中放置文件和/或文件夹的目录或应用程序的名称。希望这有帮助!
您可以使用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 网站。
这对我有用:
基本上是用命令行调用“start C:/path”然后退出终端,所以“start c:/path && exit”
WindowsExplorerOpen(@"C:/path");
public static void WindowsExplorerOpen(string path)
{
CommandLine(path, $"start {path}");
}
private static void CommandLine(string workingDirectory, string Command)
{
ProcessStartInfo ProcessInfo;
Process Process;
ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command + " && exit");
ProcessInfo.WorkingDirectory = workingDirectory;
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = true;
ProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process = Process.Start(ProcessInfo);
Process.WaitForExit();
}
这些都不适合我:
Process.Start(@"c:\test");
Process.Start("explorer.exe" , @"C:\Users");