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

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 using System.IO;
11 
12 namespace WindowsFormsApplication1
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         private void button1_Click(object sender, EventArgs e)
22         {
23             StreamWriter myStream;
24             saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
25             saveFileDialog1.RestoreDirectory = true;
26             if (saveFileDialog1.ShowDialog() == DialogResult.OK)
27             {
28                 string str;
29                 str = saveFileDialog1.FileName;
30                 myStream = new StreamWriter(saveFileDialog1.FileName);
31                 myStream.Write(textBox1.Text); 
32                 myStream.Flush();
33                 myStream.Close();
34             }
35         }
36 
37         private void button2_Click(object sender, EventArgs e)
38         {
39             string resultFile;
40             OpenFileDialog openFileDialog1 = new OpenFileDialog();
41             openFileDialog1.InitialDirectory = "D:\\";
42             openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
43             openFileDialog1.RestoreDirectory = true;
44             if (openFileDialog1.ShowDialog() == DialogResult.OK)
45             {
46                 resultFile = openFileDialog1.FileName;
47                 MessageBox.Show(resultFile);
48             }
49         }
50 
51         private void Form1_Load(object sender, EventArgs e)
52         {
53         
54         }
55     }
56 }

 

posted on 2016-09-29 16:26  HelloWorldTotti  阅读(17030)  评论(1编辑  收藏  举报

导航