c#简单的io
读取路径判断文件是否存在,进行删除或者创建
简单的io
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; namespace ComprehensiveTest.com.myio
{
public class IoManager
{
private static IoManager instance = null; public static IoManager Instance
{
get {
if (IoManager.instance == null)
{
IoManager.instance = new IoManager();
}
return IoManager.instance;
}
}
/// <summary>
///
/// </summary>
/// <param name="targetPath"></param>
/// <returns></returns>
public bool CreateFile(string targetPath)
{
if (File.Exists(targetPath))
{
return true;
}
else
{
try
{
//使用这2种方法都可以
//FileStream file = File.Create(targetPath);
FileStream file = new FileStream(targetPath, FileMode.Create);
file.Close();
return true;
}
catch (Exception e)
{
Console.WriteLine("创建文件{0},失败 , 原因 : {1} ", targetPath, e.ToString());
return false;
}
}
}
/// <summary>
/// 获得电脑所有的驱动盘
/// </summary>
/// <returns></returns>
public string[] GetMyLogicalDrives()
{
return Directory.GetLogicalDrives();
}
/// <summary>
/// 移动数据
/// </summary>
/// <param name="oldPath"> 原始的路径 </param>
/// <param name="newPath"> 新的路径 </param>
/// <returns> 操作是否成功 </returns>
public bool MoveFile(string oldPath, string newPath)
{
if (File.Exists(oldPath))
{
try
{
File.Move(oldPath, newPath);
return true;
}
catch (Exception e)
{
Console.WriteLine("移动文件{0},失败 , 原因 : {1} " , oldPath , e.ToString() );
return false;
}
}
else
{
Console.WriteLine("Error , {0}文件不存在!!! " , oldPath );
return false;
}
}
/// <summary>
/// 复制一个文件
/// </summary>
/// <param name="oldPath"></param>
/// <param name="newPath"></param>
/// <returns></returns>
public bool CopyFile(string oldPath, string newPath)
{
if (File.Exists(oldPath))
{
try
{
File.Copy(oldPath, newPath);
return true;
}
catch (Exception e)
{
Console.WriteLine("复制文件{0},失败 , 原因 : {1} ", oldPath, e.ToString());
return false;
}
}
else
{
Console.WriteLine("Error , {0}文件不存在!!! ", oldPath);
return false;
}
}
/// <summary>
/// 删除一个文件
/// </summary>
/// <param name="targetPath"></param>
/// <returns></returns>
public bool DeleteFile( string targetPath )
{
if(File.Exists( targetPath ))
{
try
{
File.Delete(targetPath);
return true;
}
catch (Exception e)
{
Console.WriteLine("删除文件{0},失败 , 原因 : {1} ", targetPath, e.ToString());
return false;
}
}
else
{
Console.WriteLine("Error , {0}文件不存在!!! ", targetPath);
return false;
}
}
/// <summary>
/// 创建一个文件夹
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public bool CreateFolder(string path)
{
if (Directory.Exists(path))
{
Console.WriteLine("文件夹{0}已经存在", path);
return true;
}
else
{
try
{
DirectoryInfo dirInfo = Directory.CreateDirectory(path);
Console.WriteLine("创建文件夹成功 , 创建时间为{0}", Directory.GetCreationTime(path));
return true;
}
catch (Exception e)
{
Console.WriteLine("创建文件夹失败 , 失败原因{0}", e.ToString());
return false;
}
}
}
/// <summary>
/// 删除文件夹
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public bool DeleteFolder(string path)
{
if (Directory.Exists(path))
{
try
{
Directory.Delete(path);
return true;
}
catch (Exception e)
{
Console.WriteLine("删除文件夹失败 , 失败原因{0}", e.ToString());
return false;
}
}
else
{
return true;
}
}
/// <summary>
///
/// </summary>
/// <param name="oldPath"></param>
/// <param name="newPath"></param>
/// <returns></returns>
public bool MoveFolder(string oldPath , string newPath)
{
if (Directory.Exists(oldPath))
{
try
{
Directory.Move(oldPath, newPath);
return true;
}
catch (Exception e)
{
Console.WriteLine("移动文件夹{0},失败 , 原因 : {1} ", oldPath, e.ToString());
return false;
}
}
else
{
Console.WriteLine("Error , {0}文件夹不存在!!! ", oldPath);
return false;
}
}
/// <summary>
/// 读取文件( 一个个读 )老是在流以外 , 无法读到正确的值
/// </summary>
/// <param name="targetPath"></param>
/// <returns></returns>
public bool ReadOneByOneTest(string targetPath)
{
if (File.Exists(targetPath))
{
FileStream fs = new FileStream(targetPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
br.BaseStream.Seek(, SeekOrigin.Begin); //将指针设到开头
while (br.BaseStream.Position < br.BaseStream.Length)
{
try
{
Console.WriteLine(br.ReadString());
}
catch (EndOfStreamException e)
{
Console.WriteLine("已经到了结尾 {0}", e.ToString());
}
}
br.Close();
fs.Close();
return true;
}
else
{
return false;
}
}
/// <summary>
/// 读取文本
/// </summary>
/// <param name="targetPath"></param>
/// <returns></returns>
public bool ReadCommon(string targetPath)
{
if (File.Exists(targetPath))
{
//using (StreamReader sr = File.OpenText(targetPath)) // 读中文将乱码
using( StreamReader sr = new StreamReader( targetPath , UnicodeEncoding.GetEncoding("GB2312"))) // 解决中文乱码问题
{
string readStr;
while ((readStr = sr.ReadLine()) != null)
{
Console.WriteLine(readStr);
}
sr.Close();
}
return true;
}
else
{
return false;
}
}
/// <summary>
///
/// </summary>
/// <param name="targetPath"></param>
/// <param name="content"></param>
/// <param name="isNendWarp"></param>
/// <returns></returns>
public bool WriteCommon(string targetPath , string content , bool isNendWarp )
{
if (File.Exists(targetPath))
{
//using (StreamWriter sw = File.AppendText(targetPath)) // 中文乱码
using( StreamWriter sw = new StreamWriter( targetPath , true ,UnicodeEncoding.GetEncoding("GB2312"))) // 解决中文乱码问题
{
if (isNendWarp)
{
sw.WriteLine(content);
}
else
{
sw.Write(content);
}
sw.Close();
}
return true;
}
else
{
return false;
}
}
}
}
c#简单的io的更多相关文章
- Lua 简单的IO交互 和迷宫代码
function room1 () print("in room1") local move = io.read() if move == "south" th ...
- Linux系统编程:简单文件IO操作
使用Linux的文件API,经常看见一个东西,叫做文件描述符. 什么是文件描述符? (1)文件描述符其实实质是一个数字,这个数字在一个进程中表示一个特定的含义,当我们open打开一个文件时,操作系统在 ...
- 开发简单的IO多路复用web框架
自制web框架 1.核心IO多路复用部分 # -*- coding:utf-8 -*- import socket import select class Snow(): def __init__(s ...
- 高性能IO模型浅析
高性能IO模型浅析 服务器端编程经常需要构造高性能的IO模型,常见的IO模型有四种: (1)同步阻塞IO(Blocking IO):即传统的IO模型. (2)同步非阻塞IO(Non-blocking ...
- IO模型
前言 说到IO模型,都会牵扯到同步.异步.阻塞.非阻塞这几个词.从词的表面上看,很多人都觉得很容易理解.但是细细一想,却总会发现有点摸不着头脑.自己也曾被这几个词弄的迷迷糊糊的,每次看相关资料弄明白了 ...
- JAVA NIO Scatter/Gather(矢量IO)
矢量IO=Scatter/Gather: 在多个缓冲区上实现一个简单的IO操作.减少或避免了缓冲区拷贝和系统调用(IO) write:Gather 数据从几个缓冲区顺序抽取并沿着通道发送,就好 ...
- 泛函编程(36)-泛函Stream IO:IO数据源-IO Source & Sink
上期我们讨论了IO处理过程:Process[I,O].我们说Process就像电视信号盒子一样有输入端和输出端两头.Process之间可以用一个Process的输出端与另一个Process的输入端连接 ...
- 服务器端高性能的IO模型 转自酷勤网
服务器端编程经常需要构造高性能的IO模型,常见的IO模型有四种: (1)同步阻塞IO(BlockingIO):即传统的IO模型. (2)同步非阻塞IO(Non-blockingIO):默认创建的soc ...
- Linux IO Scheduler(Linux IO 调度器)
每个块设备或者块设备的分区,都对应有自身的请求队列(request_queue),而每个请求队列都可以选择一个I/O调度器来协调所递交的request.I/O调度器的基本目的是将请求按照它们对应在块设 ...
随机推荐
- 28.Odoo产品分析 (四) – 工具板块(1) – 项目(1)
查看Odoo产品分析系列--目录 "项目管理"是一个用于管理你的项目,且将它们与其他应用关联起来的非常灵活的模块,他允许您的公司管理项目阶段,分配团队,甚至跟踪与项目相关的时间和工 ...
- 轻松搭建Xposed Hook
0x2.导入xposed库文件XposedBridgeApi-XX.jar,将库文件放在app/lib目录下,自己创建一个lib目录,别放在libs目录下,否则会出错,然后导入库,修改 Scope 为 ...
- 用户登录三次机会(PYTHON)
usename=shabi password=123456 i=3 while i > 0: zh = input("请输入你的用户名:") i - = 1 if zh == ...
- oracle11在docker环境下的运行
目的 Ø 在测试的环境下oracle数据库不存在或访问不方便时,可以将这个环境快速恢复出来 Ø 开发时方便测试之用 可以在任何包含docker服务的机器上运行,具体的docker的安装可以参考如下: ...
- Docker Data Center系列(三)- DTR安装指南
本系列文章演示如何搭建一个mini的云平台和DevOps实践环境. 基于这套实践环境,可以部署微服务架构的应用栈,演练提升DevOps实践能力. 1 系统要求 1.1 硬件和软件要求 成为UCP管理的 ...
- selenium-获取一组数组进行操作(七)
selenium-获取一组数组进行操作 以 纵横中文网 中获取24小时畅销榜的书单为例 此文仅做 selenium 在自动化测试中怎么获取一组数据进行说明,不做网络爬虫解释 当然,使用爬虫得到本文 ...
- js获取地址栏中的数据
window.location.href:设置或获取整个 URL 为字符串window.location.pathname:设置或获取对象指定的文件名或路径window.location.search ...
- python第一百零九天---Django 4
session :1. Session 基于Cookie做用户验证时:敏感信息不适合放在cookie中 a. Session原理 Cookie是保存在用户浏览器端的键值对 Session是保存在服务器 ...
- win7 中 sql server2005 卸载简介
注:卸载前一定要做好备份,一定要清理干净,不然重装会出错(只针对完全卸载,没试过只删除一个版本的) 工具:①Windows Install Clean Up ②SrvInstw.exe 1.停止所有 ...
- Vue学习之路8-v-on指令学习简单事件绑定之属性
前言 上一篇文章以v-on指令绑定click事件为例介绍了v-on指令的使用方法,本文介绍一下v-on绑定事件的一些属性的使用方法. v-on绑定指令属性 .stop属性 阻止单击事件继续向上传播(简 ...