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# 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文件的方法.分享给大家供大家参考.具体实现方法如下: public static string SerializeToXmlString(object objectToSerialize) { MemoryStream memoryStream = new MemoryStream(); System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serializati
-
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文件
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文件的小例子
复制代码 代码如下: 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#从数据库读取数据到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文件的方法
本文实例讲述了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文件到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文件的实现方法
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[]
随机推荐
- Apache 获取真实ip的配置的实现方法
- 详解Java编程中对线程的中断处理
- iOS高仿微信相册界面翻转过渡动画效果
- JavaScript观察者模式(经典)
- PHP网站开发中常用的8个小技巧
- js 开发之autocomplete="off"在chrom中失效的解决办法
- Android登录注册功能 数据库SQLite验证
- JavaScript学习笔记之定时器
- asp下Response.Buffer提速
- 微信小程序之页面跳转和参数传递的实现
- 微信小程序 配置顶部导航条标题颜色的实现方法
- JS设计模式之数据访问对象模式的实例讲解
- JavaScript跨域调用基于JSON的RESTful API
- Oracle case函数使用介绍
- Bootstrap3.0建站教程(一)之bootstrap表单元素排版
- java导出csv方法实现讲解
- Node.js 制作实时多人游戏框架
- vue获取当前激活路由的方法
- Linux下截屏并编辑的最佳工具
- 详解vue中移动端自适应方案