C# 文件与目录的基本操作(System.IO)
2016-07-13 16:18
218 查看
1.文件操作
[/code]
[/code]
[code] [code]///<summary>
///文件读写操作
///为简化代码供大家学习,暂不考虑捕捉异常
///</summary>
publicpartialclassTestIO:DevComponents.DotNetBar.Office2007Form
{
publicTestIO()
{
InitializeComponent();
}
///<summary>
///创建文件
///</summary>
privatevoidbtnCreateFile_Click(objectsender,EventArgse)
{
stringpath=Application.StartupPath+@"\Test.txt";
FileStreamfs=newFileStream(path,FileMode.Create);
StreamWritersw=newStreamWriter(fs);
sw.WriteLine("Thisisatestfile.");
sw.WriteLine("Thisissecondline.");
sw.Close();
fs.Close();
//也可以这样创建StreamWriter
//StreamWritersw=File.CreateText(path);
}
///<summary>
///读取文件
///</summary>
privatevoidbtnReadFile_Click(objectsender,EventArgse)
{
stringpath=Application.StartupPath+"\\Test.txt";
textBoxX1.Text=string.Empty;
if(File.Exists(path))
{
FileStreamfs=newFileStream(path,FileMode.Open);
StreamReadersr=newStreamReader(fs);
//也可以这样创建StreamReader
//File.OpenText(path);
stringstr=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>
privatevoidbtnAppendFile_Click(objectsender,EventArgse)
{
stringpath=Application.StartupPath+"\\Test.txt";
FileStreamfs=newFileStream(path,FileMode.Append);
StreamWritersw=newStreamWriter(fs);
//也可以这样创建StreamReader
//StreamWritersw=File.AppendText(path);
sw.WriteLine("Thisisthreeline.");
sw.Close();
fs.Close();
}
///<summary>
///复制文件
///</summary>
privatevoidbtnCopyFile_Click(objectsender,EventArgse)
{
stringoldPath=Application.StartupPath+"\\Test.txt";
stringnewPath=Application.StartupPath+"\\TestClone.txt";
File.Copy(oldPath,newPath);
}
///<summary>
///删除文件
///</summary>
privatevoidbtnDeleteFile_Click(objectsender,EventArgse)
{
stringpath=Application.StartupPath+"\\TestClone.txt";
File.Delete(path);
}
///<summary>
///移动文件
///</summary>
privatevoidbtnMoveFile_Click(objectsender,EventArgse)
{
stringoldPath=Application.StartupPath+"\\Test.txt";
//移动文件的同时也可以使用新的文件名
stringnewPath="d:\\NewTest.txt";
File.Move(oldPath,newPath);
}
///<summary>
///创建目录
///</summary>
privatevoidbtnCreateDirectory_Click(objectsender,EventArgse)
{
stringpath1="d:\\Jason1";
//创建目录Jason1
DirectoryInfodDepth1=Directory.CreateDirectory(path1);
//dDepth2指向dDepth1创建的子目录Jason2
DirectoryInfodDepth2=dDepth1.CreateSubdirectory("Jason2");
//设置应用程序当前的工作目录为dDepth2指向的目录
Directory.SetCurrentDirectory(dDepth2.FullName);
//在当前目录创建目录Jason3
Directory.CreateDirectory("Jason3");
}
privatevoidbtnDeleteDirectory_Click(objectsender,EventArgse)
{
stringpath="d:\\Jason1";
DeleteDirectory(path);
}
///<summary>
///删除目录及其所有子目录,文件
///</summary>
privatestaticvoidDeleteDirectory(stringpath)
{
if(Directory.Exists(path))
{
foreach(stringstrinDirectory.GetFileSystemEntries(path))
{
if(File.Exists(str))
{
File.Delete(str);
}
else
{
DeleteDirectory(str);
}
}
Directory.Delete(path);
}
else
{
MessageBox.Show("该目录不存在!");
}
}
privatevoidbtnCopyDirectory_Click(objectsender,EventArgse)
{
stringsourcePath="d:\\Jason1";
stringtargetPath="d:\\Jason2";
CopyDirectory(sourcePath,targetPath);
}
///<summary>
///复制目录及其所有内容
///</summary>
///<paramname="sourcePath">源目录</param>
///<paramname="targetPath">目标目录</param>
privatevoidCopyDirectory(stringsourcePath,stringtargetPath)
{
//该字符用于在反映分层文件系统组织的路径字符串中分隔目录级别(msdn)
//在windows系统下实质上是为目录路径加上"\\"
if(targetPath[targetPath.Length-1]!=Path.DirectorySeparatorChar)
{
targetPath+=Path.DirectorySeparatorChar;
}
//目标目录不存在则创建之
if(!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
//获取文件系统(含目录和文件)
string[]fileList=Directory.GetFileSystemEntries(sourcePath);
foreach(stringfileNameinfileList)
{
if(Directory.Exists(fileName))
{
//Path.GetFileName(fileName)可取出最后一个分级目录名或文件名
CopyDirectory(fileName,targetPath+Path.GetFileName(fileName));
}
else
{
//true为可覆盖
File.Copy(fileName,targetPath+Path.GetFileName(fileName),true);
}
}
}
}
[/code]
[/code]
相关文章推荐
- C# 多线程详解 Part.03 (定时器)
- C#中三种定时器对象的比较
- C# QQ邮箱发邮件
- C# 16进制与字符串、字节数组之间的转换
- xml与txt文件格式互换
- C#接口实例详解
- [C#自学视频]认识windows窗体
- C#中对文件的操作
- C#:复杂条件判断类型(练习)
- C# 中DateTime的各种使用(增加一年、一月、一个季度等等用法)
- 多个DataTable的合并成一个新表
- [压缩]C#下使用SevenZipSharp压缩解压文本
- C# 委托应用总结
- 泛型+反射 List<任意类型>序列化+反序列化
- BloomFilter(布隆过滤器)的C#实现
- csharp: Speech
- C# 同步两个ListBox滚动条
- C# dll文件引入运行包exe中。
- C# 匿名方法
- C#hashtable使用说明 以及 Hashtable和HashMap的区别【总结性】