您的位置:首页 > 编程语言 > C#

C# 文件与目录的基本操作(System.IO)

2016-07-13 16:18 218 查看
1.文件操作





[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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: