[ WPFテキスト ファイルからデータを読み取る]をダウンロードする。
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:self="clr-namespace:WpfFileTest"
Title="MainWindow" Height="220" Width="282">
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="23,11,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
<WindowsFormsHost Visibility="Hidden">
<self:UserControl1 x:Name="myControl"/>
</WindowsFormsHost>
</Grid>
</Window>
using System;
using System.Windows;
using System.Windows.Forms;
using System.IO;
namespace WpfFileTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
if (myControl.OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
using (StreamReader sr = new StreamReader(myControl.OpenFileDialog1.FileName))
{
String FirstLine = sr.ReadLine();
String AllLine = FirstLine + "\r\n" + sr.ReadToEnd();
System.Windows.Forms.MessageBox.Show(FirstLine, "TEST", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
if (myControl.SaveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
using (StreamWriter outfile = new StreamWriter(myControl.SaveFileDialog1.FileName))
{
outfile.Write(AllLine);
}
}
}
}
}
}
}