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文件
Xml文件如下: <?xml version="1.0" encoding="utf-8"?> <Advertisements> <Ad> <ImageUrl>001.jpg</ImageUrl> <NavigateUrl>001.aspx</NavigateUrl> <Impressions>10</Impressions> <Keyword>g
-
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文件到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# 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文件的方法
本文实例讲述了C#写入对象或集合类型数据到xml文件的方法.分享给大家供大家参考.具体实现方法如下: public static string SerializeToXmlString(object objectToSerialize) { MemoryStream memoryStream = new MemoryStream(); System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serializati
-
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
-
在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#简单写入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#使用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#实现xml文件的读取与写入简单实例
本文实例讲述了C#实现xml文件的读取与写入方法.分享给大家供大家参考.具体如下: //DataTable DateSet 都可以用来读取xml数据和写入xml数据 protected voidButton1_Click(object sender, EventArgs e) { DataTabledt = new DataTable("Employee"); DataColumndcID = new DataColumn("ID", typeof(string))
随机推荐
- HTML中不支持静态Expando的元素的问题
- Jquery easyui开启行编辑模式增删改操作
- js 获取今天以及过去日期
- Nginx服务器初期基本配置指南
- 使用regini.exe修改注册表命令
- Spring jpa和mybatis整合遇到的问题解析
- jquery精度计算代码 jquery指定精确小数位
- iOS之UIWebView无法获取web标题的解决方法
- asp.net log4net的使用方法
- android自定义倒计时控件示例
- php实现的mysqldb读写分离操作类示例
- 详解Python2.x中对Unicode编码的使用
- Shell脚本实现关闭多余的tty终端
- Linux UDP socket 设置为的非阻塞模式与阻塞模式区别
- 隐藏在SQLServer 字段中的超诡异字符解决过程
- jQuery CSS()方法改变现有的CSS样式
- 基于jQuery 实现bootstrapValidator下的全局验证
- java删除文件和文件夹具体实现
- Windows系统法宝:设备管理器技巧
- 深入浅析Android接口回调机制

