【小技巧】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 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM