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# 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文件的实现方法
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#写入对象或集合类型数据到xml文件的方法
本文实例讲述了C#写入对象或集合类型数据到xml文件的方法.分享给大家供大家参考.具体实现方法如下: public static string SerializeToXmlString(object objectToSerialize) { MemoryStream memoryStream = new MemoryStream(); System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serializati
-
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# 创建,读取,写入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#实现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#使用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# 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#从数据库读取数据到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
随机推荐
- javascript 鼠标滚轮事件
- asp之GetArray提取链接地址,以$Array$分隔的代码
- Bootstrap框架结合jQuery仿百度换肤功能实例解析
- Vue开发中整合axios的文件整理
- 怎样把Windows server 2003转换成工作站系统
- centos6.5下svn的使用说明
- Asp.net后台把脚本样式输出到head标签中节省代码冗余
- JavaScript对象之深度克隆介绍
- 正则方式的自动小偷抓网程序
- JBuilder2005单元测试之捆绑多个用例
- discuz图片顺序混乱解决方案
- 基于node.js的fs核心模块读写文件操作(实例讲解)
- js获取时间精确到秒(年月日)
- vtune自动安装脚本分享
- JSP application(return String)用法详例
- nginx,apache的alias和认证功能
- 利用jQuery的deferred对象实现异步按顺序加载JS文件
- 深入理解Node中的buffer模块
- apache中使用mod_log_slow分析响应慢的请求
- Android自定义View实现开关按钮