C# 创建文本文件写入读取实现代码
第一次运行时:

第二次运行时:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace 文件操作
{
class Program
{
static void Main(string[] args)
{
//创建一个文本文件,最好先判断一下
StreamWriter sw;
if (!File.Exists("templog.txt"))
{
//不存在就新建一个文本文件,并写入一些内容
sw = File.CreateText("templog.txt");
sw.Write("第一个字");
sw.WriteLine(" 跟随老大的.");
sw.WriteLine("当前日期是:");
sw.WriteLine(DateTime.Now);
}
else
{
//如果存在就添加一些文本内容
sw = File.AppendText("templog.txt");
for (int i = 0; i < 10; i++)
{
sw.WriteLine("可以像平时输出到屏幕一样输出{0}", i);
}
}
sw.Close();
//创建一个读取器
StreamReader sr = new StreamReader("templog.txt");
//一次性读取完
Console.WriteLine(sr.ReadToEnd());
Console.ReadLine();
}
}
}
相关推荐
-
在C#中创建和读取XML文件的实现方法
1.创建简单的XML文件为了便于测试,我们首先创建控制台应用程序,项目命名为CreateXml,Program.cs代码如下: 复制代码 代码如下: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml; namespace CreateXml{ class Program { static void Main(string[]
-
c#使用linq技术创建xml文件的小例子
复制代码 代码如下: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Xml;using System.Xml.Linq; namespa
-
c# winform读取xml文件创建菜单的代码
复制代码 代码如下: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using WinformMenu.Helper;using System.Xml; namespace WinformMen
-
c#读取xml文件到datagridview实例
复制代码 代码如下: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Xml;using System.Xml.Linq; namespa
-
C# Linq读取XML文件的实例
1.示例XML文件:Demo.xml 复制代码 代码如下: <?xml version="1.0" encoding="utf-8" ?><note> <conf> <to>infozero</to> <from>lerroy</from> <heading>测试信息</heading> <body>第一条测试信息<
-
C#实现xml文件的读取与写入简单实例
本文实例讲述了C#实现xml文件的读取与写入方法.分享给大家供大家参考.具体如下: //DataTable DateSet 都可以用来读取xml数据和写入xml数据 protected voidButton1_Click(object sender, EventArgs e) { DataTabledt = new DataTable("Employee"); DataColumndcID = new DataColumn("ID", typeof(string))
-
C#写入对象或集合类型数据到xml文件的方法
本文实例讲述了C#写入对象或集合类型数据到xml文件的方法.分享给大家供大家参考.具体实现方法如下: public static string SerializeToXmlString(object objectToSerialize) { MemoryStream memoryStream = new MemoryStream(); System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serializati
-
C#简单写入xml文件的方法
本文实例讲述了C#简单写入xml文件的方法.分享给大家供大家参考.具体实现方法如下: static void writeTree(XmlNode xmlElement, int level) { String levelDepth = ""; for(int i=0;i<level;i++) { levelDepth += " "; } Console.Write("\n{0}<{1}",levelDepth,xmlElement.N
-
C# 创建,读取,写入XML文件
Xml文件如下: <?xml version="1.0" encoding="utf-8"?> <Advertisements> <Ad> <ImageUrl>001.jpg</ImageUrl> <NavigateUrl>001.aspx</NavigateUrl> <Impressions>10</Impressions> <Keyword>g
-
C#从数据库读取数据到DataSet并保存到xml文件的方法
本文实例讲述了C#从数据库读取数据到DataSet并保存到xml文件的方法.分享给大家供大家参考.具体实现方法如下: DataSet有一个WriteXml方法可以直接将数据保存到xml文件 using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.IO; public class TestWriteXML { public static void Main() { St
随机推荐
- go1.8之安装配置具体步骤
- PowerShell实现批量重命名文件
- Openstack 节点维护详细讲解
- ASP.NETWeb服务器验证控件如何使用
- 简介Android 中的AsyncTask
- Mysql SQL服务器模式介绍
- vue2.0实现倒计时的插件(时间戳 刷新 跳转 都不影响)
- 如何计算出当前日期属于定义时间段内的第几星期?
- Mongodb实现定时备份与恢复的方法教程
- MyBatis 执行动态 SQL语句详解
- jquery获取tr并更改tr内容示例代码
- JavaScript中的操作符类型转换示例总结
- JS 按钮点击触发(兼容IE、火狐)
- c# 适配器模式
- C#中哈希表(HashTable)用法实例详解(添加/移除/判断/遍历/排序等)
- Cisco路由技术基础知识详解
- Java BigDecimal和double示例及相关问题解析
- python 3.6 tkinter+urllib+json实现火车车次信息查询功能
- mysql show操作简单示例
- Python read函数按字节(字符)读取文件的实现

