2个回答
展开全部
private void CheckBox_是否保存为文本文件_Checked(object sender, RoutedEventArgs e)
{
string fileName = string.Empty;
if (string.IsNullOrEmpty(this.TextBox_文件位置.Text))
{
var dialog = new SaveFileDialog { DefaultExt = "txt", AddExtension = true, FileName = DateTime.Now.ToString("yyyyMMdd_HHmmss", DateTimeFormatInfo.CurrentInfo) + "发票" };
if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
System.Windows.MessageBox.Show("请选择要保存的文件名称");
return;
}
fileName = dialog.FileName;
}
else
{
fileName = TextBox_文件位置.Text;
}
using (var writer = new StreamWriter(fileName, false, Encoding.UTF8))
{
writer.Write(TextBox_发票.Text);
}
}
private void Button_选择_Click(object sender, RoutedEventArgs e)
{
ClearText();
var dialog = new OpenFileDialog { Multiselect = false, Filter = "TxtFiles|*.txt" };
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.TextBox_文件位置.Text = dialog.FileName;
using (var stream = new StreamReader(dialog.FileName, Encoding.Default))
{
TextBox_发票.Text = stream.ReadToEnd();
}
}
}
{
string fileName = string.Empty;
if (string.IsNullOrEmpty(this.TextBox_文件位置.Text))
{
var dialog = new SaveFileDialog { DefaultExt = "txt", AddExtension = true, FileName = DateTime.Now.ToString("yyyyMMdd_HHmmss", DateTimeFormatInfo.CurrentInfo) + "发票" };
if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
System.Windows.MessageBox.Show("请选择要保存的文件名称");
return;
}
fileName = dialog.FileName;
}
else
{
fileName = TextBox_文件位置.Text;
}
using (var writer = new StreamWriter(fileName, false, Encoding.UTF8))
{
writer.Write(TextBox_发票.Text);
}
}
private void Button_选择_Click(object sender, RoutedEventArgs e)
{
ClearText();
var dialog = new OpenFileDialog { Multiselect = false, Filter = "TxtFiles|*.txt" };
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.TextBox_文件位置.Text = dialog.FileName;
using (var stream = new StreamReader(dialog.FileName, Encoding.Default))
{
TextBox_发票.Text = stream.ReadToEnd();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询