-
C#对于文件读写的封装其实做的是很好的,如果想要写字符串,这里简单提供一个case。
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace IO { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void StreamWriter_Click(object sender, RoutedEventArgs e) { string path = @"C:\\Users\\15475\\Desktop\\fileInfo\\test.txt"; //Input file path,注意这里要用双斜杠 //创建StreamWriter 类的实例 StreamWriter streamWriter = new StreamWriter(path); //可以指定打开的方式,这里默认创建,注意路径写上后缀 //向文件中写入姓名 streamWriter.WriteLine("yang"); //WriteLine会自动换行,如果write的话需要手动添加换行\r\n //向文件中写入QQ号 streamWriter.WriteLine("102365447898"); //刷新缓存 streamWriter.Flush(); //调用Flush和Close之前文件里面是看不到东西 //关闭流 streamWriter.Close(); //因为有时不确定close方法是否隐式调用了Dispose所以最好还是养成 streamWriter.Dispose(); } } }
StreamWriter 入门case
最新推荐文章于 2023-05-11 11:22:07 发布