LoginSignup
0
0

More than 5 years have passed since last update.

Visual Studio | WPF > フォルダをエクスプローラで開く

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

c++ builder > デバッグ > 処理対象フォルダの表示 > ShellExecute(NULL, NULL, L"explorer.exe", dstDir, NULL, SW_SHOWNORMAL);
をVisual Studio + WPFでどう実装するか。

参考: 外部アプリケーションを起動する、ファイルを関連付けられたソフトで開く @ DOBON.NETプログラミング道

ShellExecute()の代わりにSystem.Diagnostics.Process.Start()をコールする方法を使ってみる。

@-quoted形式を使うが、二重引用符の数は不要と思われる分は減らした。
Visual Studio | WPF > C# > string > @ > @-quoted 形式 > Pythonのraw stringに相当?

code

MainWindow.xaml
<Window x:Class="_171211_t1950_openFolder.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:_171211_t1950_openFolder"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="B_open" Content="Open" Height="30" Click="B_open_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 _171211_t1950_openFolder
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void B_open_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("explorer.exe", @"C:\Windows");
        }
    }
}

実行

ボタンを押すことでC:\Windowsが開くことを確認した。

qiita.png

0
0
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
0