WPFアプリケーションで、ユーザーが特定のディレクトリでWindowsエクスプローラーを開きたいボタンをクリックした場合、どうすればよいですか?
私はこのようなものを期待します:
Windows.OpenExplorer("c:\test");
WPFアプリケーションで、ユーザーが特定のディレクトリでWindowsエクスプローラーを開きたいボタンをクリックした場合、どうすればよいですか?
私はこのようなものを期待します:
Windows.OpenExplorer("c:\test");
どうしてProcess.Start(@"c:\test");
?
Process.Start("explorer.exe" , @"C:\Users");
これを使用する必要がありました。tgtdirを指定する別の方法では、アプリケーションが終了したときにエクスプローラーウィンドウが閉じられました。
これは機能するはずです:
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の4番目のパラメーターを使用して、開始ディレクトリーを指定できます。
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.netWebサイトからのものです。
これが私のために働いたものです:
基本的にコマンドラインを使用して「startC:/ 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");