MVC07
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的更多相关文章
- 快速入门系列--MVC--07与HTML5移动开发的结合
现在移动互联网的盛行,跨平台并兼容不同设备的HTML5越来越盛行,很多公司都在将自己过去的非HTML5网站应用渐进式的转化为HTML5应用,使得一套代码可以兼容不同的物理终端设备和浏览器,极大的提高了 ...
- MVC-07 案例1
>>>>>ContosoUniversity网站 ------------------------------------------- 一.并发冲突 1. 为什么会并发 ...
- MVC-07 案例2
二.电子商务网站 掌握该网站的开发流程和设计思路,并为数据模型中商品.商品分类,这两个类编写代码. 1.需求分析 2.数据模型规划 (1)商品类别 (2)商品信息 (3)会员信息 (4)购物车项目 ( ...
- MVC-07数据库
部分6:添加数据库. 创建数据库 Visual Web Developer带有免费的SQL数据库,名为SQL Server Compact. 数据库创建: 1.右键点击解决方案资源管理器中的App_D ...
- 快速入门系列--MVC--01概述
虽然使用MVC已经不少年,相关技术的学习进行了多次,但是很多技术思路的理解其实都不够深入.其实就在MVC框架中有很多设计模式和设计思路的体现,例如DependencyResolver类就包含我们常见的 ...
- 快速入门系列--MVC--02路由
现在补上URL路由的学习,至于蒋老师自建的MVC小引擎和相关案例就放在论文提交后再实践咯.通过ASP.NET的路由系统,可以完成请求URL与物理文件的分离,其优点是:灵活性.可读性.SEO优化.接下来 ...
随机推荐
- Java复习(一)——Java语言概述、开发环境、基础知识
设计模式:在各种应用系统中被大量应用,是设计的“常用套路” 作为Java WEB开发人员,需要掌握HTML/CSS/JS和数据库相关知识 常用的应用程序框架:Spring MVC,Spring Boo ...
- 吴裕雄--天生自然python学习笔记:python 用pygame模块基本绘图
绘制几何图形是游戏包的基本功能,很多游戏角色都是由基本图形组合而成的 . 绘制矩形: pygame.draw.rect Pygam巳绘制矩形的语法为: 用基本绘图绘制一个人脸 用基本绘图功能绘制人脸 ...
- activity 和 fragment 传递信息
acitvity 传递信息到fragment 初始化fragment 时可以传递arguments 该参数类型时Bundle activity 会持有fragment引用 通过通过参数的set方法 ...
- 数论入门——斐蜀定理与拓展欧几里得算法
斐蜀定理 内容 斐蜀定理又叫贝祖定理,它的内容是这样的: 若$a,bin N$,那么对于任意x,y,方程$ax+by=gcd(a,b)*k(kin N)$一定有解,且一定有一组解使$ax+by=gcd ...
- jQ给下拉框绑定事件,为什么要绑定在框(select标签)上,而不是绑定在选项(option标签)上
这是我在学习锋利的 jquery 书中 5.1.4 的代码时遇到的一个小问题,源代码如下: <head> <style type="text/css"> * ...
- CAS 5.3.x 相关信息
CAS 5.3.x 相关信息 单点登录系统 学习网站: https://www.apereo.org/projects/cas 官方网站 https://github.com/apereo/cas-o ...
- selenium中quit与close方法的区别
https://blog.csdn.net/lbxoqy/article/details/71981222
- maven项目部署到tomcat中没有classe文件的问题汇总
1.修改生成的class文件的位置
- python语法基础-函数-递归函数-长期维护
############### 递归 ############## # 递归的定义——在一个函数里再调用这个函数本身 # 递归的最大深度——998 # 二分查找算法 # 你观察这个列表,这是 ...
- dbSNP|n SwissVar|n CanProVar|CHPP|mutation assessor|
癌症蛋白质基因组学主要研究driver性质的突变,该突变有可能是转化为癌基因的突变.抑癌基因突变.药物位点突变和蛋白突变,可以使用mutation assessor 预测突变 突变导致疾病,修饰仅影响 ...