1. 讲解ASP.net MVC的I/O操作

新建一个控制台程序,输入代码如下

using System;
using System.IO; namespace IO
{
class Program
{
static void Main(string[] args)
{
// 判断文件是否存在
Console.WriteLine(File.Exists(@"C:\Users\ASUS\Desktop\memo\a.txt"));
// 判断目录是否存在
Console.WriteLine(Directory.Exists(@"C:\"));
// 下面例子将展示如何查找某一目录下所有exe文件的信息
// . 表示当前目录
string path = ".";
if (args.Length > )
{
// 如果需要在其他目录执行则打开控制行,之后进入项目下的Debug目录,见下图
path = args[];
}
else
{
Console.WriteLine("Directory not found");
} DirectoryInfo dir = new DirectoryInfo(path);
foreach(FileInfo f in dir.GetFiles("*.exe"))
{
string name = f.Name;
long size = f.Length;
DateTime creationTime = f.CreationTime;
Console.WriteLine(name);
Console.WriteLine(size);
Console.WriteLine(creationTime);
Console.WriteLine("------------");
}
}
}
}

File,Directory为一个静态的Class,无法实例化。

2.写入文件

using System;
using System.IO; namespace IO
{
class Program
{
private const string FILE_NAME = "a.txt";
static void Main(string[] args)
{
if (File.Exists(FILE_NAME))
{
Console.WriteLine("already exists.");
return;
} FileStream fs = new FileStream(FILE_NAME, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs); for(int i = ; i < ; i++)
{
w.Write("a");
}
w.Close();
fs.Close();
}
}
}

 

如果文件已存在,我们需要覆盖内容到里面怎么办?

using System;
using System.IO; namespace IO
{
class Program
{
private const string FILE_NAME = "a.txt";
static void Main(string[] args)
{
using(StreamWriter w = File.AppendText("test.txt"))
{
Log("Hi,is me.", w);
Log("how are u", w);
w.Close();
} }
// 方法,用于写入数据
public static void Log(string logMessage,TextWriter w)
{
w.Write("\r\nLog Entry");
w.WriteLine(":{0}", logMessage);
w.Flush(); }
}
}

using 内代码执行完毕后会自动释放资源,常用于读写文件以及连接数据库

2.读取文件

using System;
using System.IO; namespace IO
{
class Program
{
private const string FILE_NAME = "a.txt";
static void Main(string[] args)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist!",FILE_NAME);
return;
}
// 路径,操作类别,权限
FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
       // 读取前5个字符
for(int i = ; i < ; i++)
{
Console.WriteLine(r.ReadString());
}
r.Close();
fs.Close();
} }
}

完整读取某一文件:

using System;
using System.IO; namespace IO
{
class Program
{
private const string FILE_NAME = "test.txt";
static void Main(string[] args)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist!",FILE_NAME);
return;
}
using(StreamReader sr = File.OpenText(FILE_NAME))
{
string input;
while((input = sr.ReadLine())!=null)
{
Console.WriteLine(input);
}
Console.WriteLine("ended");
sr.Close();
}
} }
}

MVC07的更多相关文章

  1. 快速入门系列--MVC--07与HTML5移动开发的结合

    现在移动互联网的盛行,跨平台并兼容不同设备的HTML5越来越盛行,很多公司都在将自己过去的非HTML5网站应用渐进式的转化为HTML5应用,使得一套代码可以兼容不同的物理终端设备和浏览器,极大的提高了 ...

  2. MVC-07 案例1

    >>>>>ContosoUniversity网站 ------------------------------------------- 一.并发冲突 1. 为什么会并发 ...

  3. MVC-07 案例2

    二.电子商务网站 掌握该网站的开发流程和设计思路,并为数据模型中商品.商品分类,这两个类编写代码. 1.需求分析 2.数据模型规划 (1)商品类别 (2)商品信息 (3)会员信息 (4)购物车项目 ( ...

  4. MVC-07数据库

    部分6:添加数据库. 创建数据库 Visual Web Developer带有免费的SQL数据库,名为SQL Server Compact. 数据库创建: 1.右键点击解决方案资源管理器中的App_D ...

  5. 快速入门系列--MVC--01概述

    虽然使用MVC已经不少年,相关技术的学习进行了多次,但是很多技术思路的理解其实都不够深入.其实就在MVC框架中有很多设计模式和设计思路的体现,例如DependencyResolver类就包含我们常见的 ...

  6. 快速入门系列--MVC--02路由

    现在补上URL路由的学习,至于蒋老师自建的MVC小引擎和相关案例就放在论文提交后再实践咯.通过ASP.NET的路由系统,可以完成请求URL与物理文件的分离,其优点是:灵活性.可读性.SEO优化.接下来 ...

随机推荐

  1. ELK_疑难杂症处理

    一.ELK实用知识点总结 1.编码转换问题 这个问题,主要就是中文乱码. input中的codec=>plain转码: codec => plain {charset => &quo ...

  2. Python_面试题_更新中

    Python-面试题 线上操作系统 centos py2和py3的区别 每种数据类型,列举你了解的方法 3 or 9 and 8 字符串的反转 is 和 == 的区别? git流程 v = (1) / ...

  3. swift中的坑

    1.NSClassFromString //获取工程名称 let group = Bundle.main.infoDictionary let fileName = group?[kCFBundleE ...

  4. 56)PHP,模型类的设计思想

    一张表对应一个模型类-----Mode

  5. JDK5.0 Annotation学习笔记(一)

    背景知识:         从JDK5开始提供名为Annotation(注释)的功能,它被定义为JSR-175规范.注释是以"@注释名"在代码中存在的,还可以添加一些参数值,例如: ...

  6. 关于用struts2框架中iframe对应的jsp页面的不到action的值的问题

    我们做web项目经常会用到frameset.frame以及iframe,这大大方便了我们页面的构建以及模块功能的划分.但是,再使用这些技术的同时也会遇到各种各样的问题,其中一个就是子页面中得不到str ...

  7. iOS简单音乐实现、React-Native完整项目、仿闲鱼京东列表分页、语音识别、网络加载过度动画等源码

    iOS精选源码 iOS快速入手语音识别.听写.评测.播报 网络加载数据的过渡动画(仿简书网页) iOS 封装跑马灯和轮播效果 crash防护组件,适用常见常用的数组,字典等crash保护 iOS:高仿 ...

  8. StartDT AI Lab | 视觉智能引擎——从Face ID说起,浅析顾客数字化

    “顾客就是上帝”,这句西谚揭示了顾客占据着商业活动中心地位这一客观规律.为了能更好地服务顾客,优化商家自身的服务与产品,对顾客的分析与需求调研一直是商业经营分析中的重中之重. 在商业互联网化.社会数字 ...

  9. iOS天气动画、高仿QQ菜单、放京东APP、高仿微信、推送消息等源码

    iOS精选源码 TYCyclePagerView iOS上的一个无限循环轮播图组件 iOS高仿微信完整项目源码 想要更简单的推送消息,看本文就对了 ScrollView嵌套ScrolloView解决方 ...

  10. Skipping MapperFactoryBean with name 'sysUserMapper' and 'com.buding.system.mapper.SysUserMapper' mapperInterface. Bean already defined with the same name!

    前几天整体看了一下SpringBoot的简介,觉得看不如自己动手做,于是动手做一个简单的用户管理.启动的时候遇到了Skipping MapperFactoryBean with name 'sysUs ...