LoginSignup
1
0

More than 5 years have passed since last update.

Visual Studio | WPF > Drag & Drop > ファイルの中身を表示する実装 | Pathのあいまいな参照

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

TCP通信実装で「ファイルの内容を送信」するときにあると便利なのが、Drag & Dropでの送信ファイルの指定。

@gushwell さんのコードを参考に学習してみた。
WPFサンプル:エクスプローラーからファイルをドラッグ&ドロップする

処理内容

  • Drag & Dropした時にファイル内容を表示する

code

MainWindow.xaml
<Window x:Class="_171208_t2010_dragDrop.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:_171208_t2010_dragDrop"
        mc:Ignorable="d"
        AllowDrop="True"
        Drop="Window_Drop"
        PreviewDragOver="Window_PreviewDragOver"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Name="T_content" Text="[File Content]"  Height="300"
                 TextWrapping="Wrap"/>
    </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 _171208_t2010_dragDrop
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Drop(object sender, DragEventArgs e)
        {
            string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
            if (files == null)
            {
                return;
            }
            // 1つだけファイルを開く
            if (System.IO.File.Exists(files[0]) == false)
            {
                return;
            }
            this.Title = System.IO.Path.GetFileName(files[0]);
            T_content.Text = System.IO.File.ReadAllText(files[0]);
        }

        private void Window_PreviewDragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
            {
                e.Effects = DragDropEffects.Copy;
            } else
            {
                e.Effects = DragDropEffects.None;
            }
            e.Handled = true;
        }
    }
}

実行例

起動直後
qiita.png

ファイルDrag & Drop後
qiita.png

関連

備考 > Pathのあいまいな参照

using System.IOとしてPath.GetFileName()を使おうとすると以下のエラーとなった。

'Path'は、'System.Windows.Shapes.Path'と'System.IO.Path'間のあいまいな参照です。

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