C#使用Streamwriter打開文件的方法
更新時間:2015年04月08日 14:31:47 作者:heishui
這篇文章主要介紹了C#使用Streamwriter打開文件的方法,涉及C#操作文件的技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了C#使用Streamwriter打開文件的方法。分享給大家供大家參考。具體如下:
using System;
using System.IO;
public class KtoD1 {
public static void Main() {
string str;
StreamWriter fstr_out;
// Open the file directly using StreamWriter.
try {
fstr_out = new StreamWriter("test.txt");
}
catch(IOException exc) {
Console.WriteLine(exc.Message + "Cannot open file.");
return ;
}
Console.WriteLine("Enter text ('stop' to quit).");
do {
Console.Write(": ");
str = Console.ReadLine();
if(str != "stop") {
str = str + "\r\n"; // add newline
try {
fstr_out.Write(str);
} catch(IOException exc) {
Console.WriteLine(exc.Message + "File Error");
return ;
}
}
} while(str != "stop");
fstr_out.Close();
}
}
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
C#線程委托BeginInvoke與EndInvoke的用法
這篇文章介紹了C#線程委托BeginInvoke與EndInvoke的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
C#構(gòu)造函數(shù)在基類和父類中的執(zhí)行順序
這篇文章介紹了C#構(gòu)造函數(shù)在基類和父類中的執(zhí)行順序,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
C#?wpf使用DockPanel實現(xiàn)制作截屏框
做桌面客戶端的時候有時需要實現(xiàn)截屏功能,能夠在界面上框選截屏,本文就來為大家介紹一下wpf如何使用DockPanel制作截屏框吧,感興趣的可以了解下2023-09-09

