LoginSignup
0
1

More than 5 years have passed since last update.

Visual Studio / WPF > ダイアログ > SaveFileDialog > FileNamesとFileNameで選択ファイルを取得できる | MultiSelectプロパティはない

Last updated at Posted at 2017-04-26
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

@ WPF 4.5入門 by 大田一希さん
No.4717 / 9985

SaveFileDialog
OpenFileDialog

SaveFileDialogを使ってみた。

SaveFileDialogにはMultiSelectプロパティはない。複数ファイルに書き込みをする、ということは普通はしないから。
一方で、OpenFileDialogにはMultiSelectプロパティがある。複数ファイルを処理することはする場合があるから。

XAML
<Window x:Class="_170426_t1115_SaveFileDialog.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:_170426_t1115_SaveFileDialog"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="button1" Content="Save" 
                Height="50" Click="ShowDialog"></Button>
    </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;
using Microsoft.Win32;

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

        private void ShowDialog(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sdlg = new SaveFileDialog();
            Nullable<bool> res = sdlg.ShowDialog();
            if (res == false)
            {
                return;
            }

            string msg = "";
            foreach(var elem in sdlg.FileNames)
            {
                msg += ",";
                msg += elem;
            }
            MessageBox.Show(msg);
        }
    }
}

work.png

work.png

work.png

work.png

SaveFileDialogの場合、既存のファイルを選択時に「XXは既に存在します。置き換えますか?」という確認ダイアログが表示される。

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