【小技巧】C#的saveFileDialog和openFileDialog的用法总结

时间:2024-06-26 14:07:32
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
StreamWriter myStream;
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string str;
str = saveFileDialog1.FileName;
myStream = new StreamWriter(saveFileDialog1.FileName);
myStream.Write(textBox1.Text);
myStream.Flush();
myStream.Close();
}
} private void button2_Click(object sender, EventArgs e)
{
string resultFile;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "D:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
resultFile = openFileDialog1.FileName;
MessageBox.Show(resultFile);
}
} private void Form1_Load(object sender, EventArgs e)
{ }
}
}