C#StreamWriter的操作解析
C# StreamWriter的操作是我們?cè)趯?shí)際C#寫(xiě)文件開(kāi)發(fā)中會(huì)使用的常見(jiàn)方法,那么C# StreamWriter的具體操作是什么呢?這里還向你介紹一個(gè)C# StreamWriter寫(xiě)文件的實(shí)例,希望對(duì)你了解C# StreamWriter寫(xiě)文件的操作有所幫助。
C# StreamWriter寫(xiě)文件的操作實(shí)例:
- //實(shí)例化一個(gè)保存文件對(duì)話框
- SaveFileDialog sf = new SaveFileDialog();
- //設(shè)置文件保存類(lèi)型
- sf.Filter = "txt文件|*.txt|所有文件|*.*";
- //如果用戶(hù)沒(méi)有輸入擴(kuò)展名,自動(dòng)追加后綴
- sf.AddExtension = true;
- //設(shè)置標(biāo)題
- sf.Title = "寫(xiě)文件";
- //如果用戶(hù)點(diǎn)擊了保存按鈕
- if (sf.ShowDialog() == DialogResult.OK)
- {
- //實(shí)例化一個(gè)文件流--->與寫(xiě)入文件相關(guān)聯(lián)
- FileStream fs = new FileStream(sf.FileName, FileMode.Create);
- //實(shí)例化一個(gè)StreamWriter-->與fs相關(guān)聯(lián)
- StreamWriter sw = new StreamWriter(fs);
- //開(kāi)始寫(xiě)入
- sw.Write(this.textBox1.Text);
- //清空緩沖區(qū)
- sw.Flush();
- //關(guān)閉流
- sw.Close();
- fs.Close();
- }
C# StreamWriter的操作細(xì)節(jié)問(wèn)題基本就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C# StreamWriter操作方面有所幫助。
【編輯推薦】