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