读取路径判断文件是否存在,进行删除或者创建

简单的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的更多相关文章

  1. Lua 简单的IO交互 和迷宫代码

    function room1 () print("in room1") local move = io.read() if move == "south" th ...

  2. Linux系统编程:简单文件IO操作

    使用Linux的文件API,经常看见一个东西,叫做文件描述符. 什么是文件描述符? (1)文件描述符其实实质是一个数字,这个数字在一个进程中表示一个特定的含义,当我们open打开一个文件时,操作系统在 ...

  3. 开发简单的IO多路复用web框架

    自制web框架 1.核心IO多路复用部分 # -*- coding:utf-8 -*- import socket import select class Snow(): def __init__(s ...

  4. 高性能IO模型浅析

    高性能IO模型浅析 服务器端编程经常需要构造高性能的IO模型,常见的IO模型有四种: (1)同步阻塞IO(Blocking IO):即传统的IO模型. (2)同步非阻塞IO(Non-blocking  ...

  5. IO模型

    前言 说到IO模型,都会牵扯到同步.异步.阻塞.非阻塞这几个词.从词的表面上看,很多人都觉得很容易理解.但是细细一想,却总会发现有点摸不着头脑.自己也曾被这几个词弄的迷迷糊糊的,每次看相关资料弄明白了 ...

  6. JAVA NIO Scatter/Gather(矢量IO)

    矢量IO=Scatter/Gather:   在多个缓冲区上实现一个简单的IO操作.减少或避免了缓冲区拷贝和系统调用(IO)   write:Gather 数据从几个缓冲区顺序抽取并沿着通道发送,就好 ...

  7. 泛函编程(36)-泛函Stream IO:IO数据源-IO Source & Sink

    上期我们讨论了IO处理过程:Process[I,O].我们说Process就像电视信号盒子一样有输入端和输出端两头.Process之间可以用一个Process的输出端与另一个Process的输入端连接 ...

  8. 服务器端高性能的IO模型 转自酷勤网

    服务器端编程经常需要构造高性能的IO模型,常见的IO模型有四种: (1)同步阻塞IO(BlockingIO):即传统的IO模型. (2)同步非阻塞IO(Non-blockingIO):默认创建的soc ...

  9. Linux IO Scheduler(Linux IO 调度器)

    每个块设备或者块设备的分区,都对应有自身的请求队列(request_queue),而每个请求队列都可以选择一个I/O调度器来协调所递交的request.I/O调度器的基本目的是将请求按照它们对应在块设 ...

随机推荐

  1. springboot 整合 redis

    jedis 和 lettuce 都是用来连接 redis 的客户端,jedis 如果不使用连接池是非线程安全的,lettuce 使用 netty 线程安全且并发性能更好: springboot 2.x ...

  2. Python高级特性:列表生成式

    列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. 最常见的例子: 生成list [, , , , , , , , , ]可以用li ...

  3. Android内存优化(四)LeakCanary使用详解

    LeakCanary是检测App内存泄露的工具, 内存泄露是Android开发中常见的问题, 使用程序的稳定性下降. LeakCanary 的机制如下: RefWatcher.watch() 会以监控 ...

  4. Kotlin入门(23)适配器的进阶表达

    前面在介绍列表视图和网格视图时,它们的适配器代码都存在视图持有者ViewHolder,因为Android对列表类视图提供了回收机制,如果某些列表项在屏幕上看不到了,则系统会自动回收相应的视图对象.随着 ...

  5. Python 套接字socketserver网络编程

    为什么使用socketserver 虽然Python内置的socket和threading模块能实现简单的多线程服务器,在非正式环境,随便用用还是可以的,但是如果要在生产环境中使用,那是万万不够的. ...

  6. mssql sqlserver两条求和sql脚本相加的方法分享

    转自:http://www.maomao365.com/?p=7205 摘要: 下文分享两条sql求和脚本,再次求和的方法分享 /* 例: 下文已知两条sql求和脚本,现需对两张不同表的求和记录再次求 ...

  7. JavaWeb入门笔记

    Java web笔记 一.HTTP协议 HTTP(超文本传输协议),它是一种主流B/S架构中应用的通信协议.具有以下特点: 1.无状态 服务端不会记录客户端每次提交的请求,服务器一旦相应客户端之后,就 ...

  8. 使用GDB调试gp(转载)

    使用 gdb 调试 postgres greenplum zalax3030人评论715人阅读2012-07-11 10:07:15   错误信息的获取途径有几种 :  1. 最简单的就是看Postg ...

  9. JSON Web Tokens简单学习

    JWT用于加密生成安全认证的令牌,存储登录验证成功的部分用户信息 一.安装JWT 二.加密 解密 代码 /*存储在加密字符串的信息*/ var payload = new Dictionary< ...

  10. TCP Health Checks

    This chapter describes how to configure health checks for TCP. Introduction NGINX and NGINX Plus can ...