在一个WPF应用程序中,当用户单击按钮时,我想打开Windows资源管理器到某个目录,我该怎么做?
我希望实现的功能大概是这样:
我希望实现的功能大概是这样:
Windows.OpenExplorer("c:\test");
Windows.OpenExplorer("c:\test");
Process.Start(@"c:\test");
?Process.Start("explorer.exe" , @"C:\Users");
我必须使用这个方法,只指定目标目录的另一种方式会在我的应用程序终止时关闭资源管理器窗口。
Process.Start(new ProcessStartInfo(your_path) { UseShellExecute = true })
。它会使用你机器上默认的shell,而不是试图在基于Unix的操作系统中找到不存在的explorer.exe。 - ErhodeUseShellExecute
默认不是已经设置为 true
了吗? - Nyerguds这应该可以运行:
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;
}
然后调用该方法,在括号中放入文件和/或文件夹的目录,或应用程序的名称。希望这有所帮助!
EnableRaisingEvents
属性,然后再启动它? - Nyerguds您可以使用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 网站。
IDisposable
的类中吗? - LuckyLikeyWindowsExplorerOpen(@"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");
Process.Start("calc.exe");
将会运行计算器。您可以传递可执行文件的完整路径,它将会运行该文件。 - Jamie PenneyStart(dir)
和Start("explorer.exe", dir)
之间的另一个区别是前者如果存在窗口已经打开了dir
目录,它会聚焦到这个已有窗口,而后者则每次都会打开一个新窗口。 - dlf