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文件的读取与写入简单实例
本文实例讲述了C#实现xml文件的读取与写入方法.分享给大家供大家参考.具体如下: //DataTable DateSet 都可以用来读取xml数据和写入xml数据 protected voidButton1_Click(object sender, EventArgs e) { DataTabledt = new DataTable("Employee"); DataColumndcID = new DataColumn("ID", typeof(string))
-
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# 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# 创建,读取,写入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文件的方法.分享给大家供大家参考.具体实现方法如下: 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文件的实现方法
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文件到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#使用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
随机推荐
- PHP 中 DOMDocument保存xml时中文出现乱码问题的解决方案
- 让sql2005运行在独立用户下出现 WMI 提供程序错误的解决方式
- JS基于MSClass和setInterval实现ajax定时采集信息并滚动显示的方法
- IOS开发环境windows化攻略
- Java实现读取文章中重复出现的中文字符串
- JAVA使用Gson解析json数据实例解析
- 微信公众平台开发之发送文本消息.Net代码解析
- Yii2框架RESTful API 格式化响应,授权认证和速率限制三部分详解
- php比较多维数组中值的大小排序实现代码
- PHP版微信第三方实现一键登录及获取用户信息的方法
- PHP中返回引用类型的方法
- Python编码爬坑指南(必看)
- Android下拉框PopupWindow使用详解
- Json_decode 解析json字符串为NULL的解决方法(必看)
- IIS 多站点 用户隔离视频教程
- Oracle删除字段中的空格、回车及指定字符的实例代码
- 基于Jquery制作的幻灯片图集效果打包下载
- Android Service中使用Toast无法正常显示问题的解决方法
- C++使用ADO实现存取图片的方法
- Python中装饰器学习总结