C# 文件的读取与另存为(WPF)

 

xaml:样式















CS:后台代码

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void OnOpen(object sender, ExecutedRoutedEventArgs e)
{
var dlg = new OpenFileDialog()
{
Title = "简单编辑器-打开",
CheckPathExists = true,
CheckFileExists = true,
Filter = "Text files (*.txt)|*.txt|All files|*.*",
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)
};
if (dlg.ShowDialog() == true)
{
text1.Text = File.ReadAllText(dlg.FileName);
}
}

private void OnSave(object sender, ExecutedRoutedEventArgs e)
{
var dlg = new SaveFileDialog()
{
Title = "简单编辑器-另存为",
DefaultExt = "txt",
Filter = "Text files (*.txt)|*.txt|All files|*.*",
};
if (dlg.ShowDialog() == true)
{
File.WriteAllText(dlg.FileName,text1.Text);
}
}
}

 简单读取文件另存文件(仅仅打开txt文件,其他文件没有进行转码)

转载于:https://www.cnblogs.com/zebra-bin/p/9417918.html

你可能感兴趣的:(C# 文件的读取与另存为(WPF))