/// <summary>
/// 文件读写操作/// </summary>
public partial class TestIO : DevComponents.DotNetBar.Office2007Form
{
public TestIO()
{
InitializeComponent();
} /// <summary>
/// 创建文件
/// </summary>
private void btnCreateFile_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + @"\Test.txt";
FileStream fs = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("This is a test file.");
sw.WriteLine("This is second line.");
sw.Close();
fs.Close(); // 也可以这样创建 StreamWriter
// StreamWriter sw = File.CreateText(path);
} /// <summary>
/// 读取文件
/// </summary>
private void btnReadFile_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + "\\Test.txt";
textBoxX1.Text = string.Empty;
if (File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs);
// 也可以这样创建 StreamReader
// File.OpenText(path);
string str = string.Empty;
while (true)
{
str = sr.ReadLine();
if (!string.IsNullOrEmpty(str))
{
textBoxX1.Text += str + "\r\n";
}
else
{
sr.Close();
fs.Close();
break;
}
}
}
else
{
MessageBox.Show("指定的路径下不存在此文件!");
}
} /// <summary>
/// 追加文件内容
/// </summary>
private void btnAppendFile_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + "\\Test.txt";
FileStream fs = new FileStream(path, FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
// 也可以这样创建 StreamReader
// StreamWriter sw = File.AppendText(path);
sw.WriteLine("This is three line.");
sw.Close();
fs.Close();
} /// <summary>
/// 复制文件
/// </summary>
private void btnCopyFile_Click(object sender, EventArgs e)
{
string oldPath = Application.StartupPath + "\\Test.txt";
string newPath = Application.StartupPath + "\\TestClone.txt";
File.Copy(oldPath, newPath);
} /// <summary>
/// 删除文件
/// </summary>
private void btnDeleteFile_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + "\\TestClone.txt";
File.Delete(path);
} /// <summary>
/// 移动文件
/// </summary>
private void btnMoveFile_Click(object sender, EventArgs e)
{
string oldPath = Application.StartupPath + "\\Test.txt";
// 移动文件的同时也可以使用新的文件名
string newPath = "d:\\NewTest.txt";
File.Move(oldPath, newPath);
} /// <summary>
/// 创建目录
/// </summary>
private void btnCreateDirectory_Click(object sender, EventArgs e)
{
string path1 = "d:\\Jason1";
// 创建目录 Jason1
DirectoryInfo dDepth1 = Directory.CreateDirectory(path1);
// dDepth2 指向 dDepth1 创建的子目录 Jason2
DirectoryInfo dDepth2 = dDepth1.CreateSubdirectory("Jason2");
// 设置应用程序当前的工作目录为 dDepth2 指向的目录
Directory.SetCurrentDirectory(dDepth2.FullName);
// 在当前目录创建目录 Jason3
Directory.CreateDirectory("Jason3");
} private void btnDeleteDirectory_Click(object sender, EventArgs e)
{
string path = "d:\\Jason1";
DeleteDirectory(path);
} /// <summary>
/// 删除目录及其所有子目录,文件
/// </summary>
private static void DeleteDirectory(string path)
{
if (Directory.Exists(path))
{
foreach (string str in Directory.GetFileSystemEntries(path))
{
if (File.Exists(str))
{
File.Delete(str);
}
else
{
DeleteDirectory(str);
}
}
Directory.Delete(path);
}
else
{
MessageBox.Show("该目录不存在!");
}
} private void btnCopyDirectory_Click(object sender, EventArgs e)
{
string sourcePath = "d:\\Jason1";
string targetPath = "d:\\Jason2";
CopyDirectory(sourcePath, targetPath);
} /// <summary>
/// 复制目录及其所有内容
/// </summary>
/// <param name="sourcePath">源目录</param>
/// <param name="targetPath">目标目录</param>
private void CopyDirectory(string sourcePath, string targetPath)
{
// 该字符用于在反映分层文件系统组织的路径字符串中分隔目录级别(msdn)
// 在windows系统下实质上是为目录路径加上"\\"
if (targetPath[targetPath.Length - ] != Path.DirectorySeparatorChar)
{
targetPath += Path.DirectorySeparatorChar;
}
// 目标目录不存在则创建之
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
// 获取文件系统(含目录和文件)
string[] fileList = Directory.GetFileSystemEntries(sourcePath);
foreach (string fileName in fileList)
{
if (Directory.Exists(fileName))
{
// Path.GetFileName(fileName) 可取出最后一个分级目录名或文件名
CopyDirectory(fileName, targetPath + Path.GetFileName(fileName));
}
else
{
// true 为可覆盖
File.Copy(fileName, targetPath + Path.GetFileName(fileName), true);
}
}
}
}

IO操作的更多相关文章

  1. [.NET] 利用 async & await 进行异步 IO 操作

    利用 async & await 进行异步 IO 操作 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6082673.html  序 上次,博主 ...

  2. 文件IO操作..修改文件的只读属性

    文件的IO操作..很多同行的IO工具类都是直接写..但是如果文件有只读属性的话..则会写入失败..所以附加了一个只读的判断和修改.. 代码如下: /// <summary> /// 创建文 ...

  3. python之协程与IO操作

    协程 协程,又称微线程,纤程.英文名Coroutine. 协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用. 子程序,或者称为函数,在所有语言中都是层级调用,比如A调用B ...

  4. JAVASE02-Unit08: 文本数据IO操作 、 异常处理

    Unit08: 文本数据IO操作 . 异常处理 * java.io.ObjectOutputStream * 对象输出流,作用是进行对象序列化 package day08; import java.i ...

  5. JAVASE02-Unit07: 基本IO操作 、 文本数据IO操作

    基本IO操作 . 文本数据IO操作 java标准IO(input/output)操作 package day07; import java.io.FileOutputStream; import ja ...

  6. IO操作概念。同步、异步、阻塞、非阻塞

    “一个IO操作其实分成了两个步骤:发起IO请求和实际的IO操作. 同步IO和异步IO的区别就在于第二个步骤是否阻塞,如果实际的IO读写阻塞请求进程,那么就是同步IO. 阻塞IO和非阻塞IO的区别在于第 ...

  7. Java基础复习笔记系列 七 IO操作

    Java基础复习笔记系列之 IO操作 我们说的出入,都是站在程序的角度来说的.FileInputStream是读入数据.?????? 1.流是什么东西? 这章的理解的关键是:形象思维.一个管道插入了一 ...

  8. java中的IO操作总结

    一.InputStream重用技巧(利用ByteArrayOutputStream) 对同一个InputStream对象进行使用多次. 比如,客户端从服务器获取数据 ,利用HttpURLConnect ...

  9. Linux系统编程--文件IO操作

    Linux思想即,Linux系统下一切皆文件. 一.对文件操作的几个函数 1.打开文件open函数 int open(const char *path, int oflags); int open(c ...

  10. Java之IO操作总结

    所谓IO,也就是Input与Output的缩写.在java中,IO涉及的范围比较大,这里主要讨论针对文件内容的读写 其他知识点将放置后续章节 对于文件内容的操作主要分为两大类 分别是: 字符流 字节流 ...

随机推荐

  1. Nginx+PHP优化实例

    1.PHP-FPM高负载的解决办法 http://blog.haohtml.com/archives/11162 2.Nginx优化配置 http://blog.haohtml.com/archive ...

  2. (实用篇)微信支付扫码支付php版

    本文实例为大家分享了php微信扫码支付源码,供大家参考,具体内容如下 代码中包含四个文件createUrl.php.ArrayToXML.php.returnGoodsUrl.php.notifyUr ...

  3. easyui datagrid 合并单元格

    整理以前做的东西,这个合并单元格的问题再新浪博客也写过了..... 下面这段代码是列表数据 //载入排放系数管理报表数据 function LoadEmissionReportData() { //获 ...

  4. 硬件抽象层:HAL

    本章主要讲硬件抽象层:HAL,它是建立在Linux驱动之上的一套程序库.刚开始介绍了为什么要在Android中加入HAL,目的有三个,一,统一硬件的调用接口.二,解决了GPL版权问题.三,针对一些特殊 ...

  5. github:如何获取项目源代码

    github是流行的源码管理平台.这上面有很多开源的项目.作为普通的用户,如何获取这些开源项目的源码呢? 1.首先需要注册一个github账号. 2.安装windows下的git工具:下载地址: ht ...

  6. Codeforces Round #174 (Div. 2)

    A. Cows and Primitive Roots 暴力. B. Cows and Poker Game 模拟. C. Cows and Sequence 线段树维护. D. Cow Progra ...

  7. alpha 发布评论

    1.飞天小女警:礼物挑选小工具. 这一组的项目是个人最为感兴趣的,核心功能的实现比较有实际意义,希望所能挑选的礼物范围尽量足够大,界面期待完善后的效果. 2.nice!:约跑app.这一款面向喜爱运动 ...

  8. mysql配置命令 CHARACTER_SET_%字符集设置

    参照: http://blog.csdn.net/mzlqh/article/details/7621307点击打开链接 其实现在的ubuntu12. 直接sudo apt-get install M ...

  9. 【转载】ANSYS TRANSIENT ANSLYSIS [2]

    原文地址:http://sps.utm.my/wp-content/uploads/2014/12/ANSYS-day2-Transient-analysis.pdf

  10. HDU 5937 Equation

    题意: 有1~9数字各有a1, a2, -, a9个, 有无穷多的+和=. 问只用这些数字, 最多能组成多少个不同的等式x+y=z, 其中x,y,z∈[1,9]. 等式中只要有一个数字不一样 就是不一 ...