LoginSignup
0
1

More than 5 years have passed since last update.

Visual Studio | WPF > ソフト操作 > Explorerを起動する > ShellExecute()でなくProcess.Start()を使う方法

Posted at
動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

処理概要

外部ソフトを起動したい。

https://qiita.com/7of9/items/a78d42389deda90f5aaa
で使ったようなShellExecute()を利用するかと思ったが、以下を見ていているとProcess.Start()が便利そうとのこと。

Process.Start()は内部的にはShellExecute()をコールしている。

関連

Visual Studio | WPF > 終了処理 > shutdown | reboot > System.Diagnostics.Process.Start()
にてProcess.Start()は使った。

code

MainWindow.xaml
<Window x:Class="_171110_t1525_Explorer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_171110_t1525_Explorer"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Width="200" Height="30" Content="Open Folder" Click="Button_Click"/>
    </Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _171110_t1525_Explorer
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var psi = new System.Diagnostics.ProcessStartInfo();
            psi.FileName = "Explorer.exe";
            psi.Arguments = "C:\\";
            psi.UseShellExecute = true;
            psi.CreateNoWindow = true;
            var proc = System.Diagnostics.Process.Start(psi);
        }
    }
}

実行

qiita.png

ボタンをクリックしたらExplorerが起動してCドライブを表示する。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1