C# 文件与目录的基本操作
2016-11-10 15:50
148 查看
1. 文件的创建和写入
public void BtnCreateFile_Click()
{
string path = Application.dataPath + @"\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);
}
2.读取文件,文件内容
/// <summary>
/// 读取文件
/// </summary>
public void b移动文件tnReadFile_Click()
{
string path = Application.dataPath + "\\Test.txt";
text.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))
{
text.text += str + "\r\n";
}
else
{
sr.Close();
fs.Close();
break;
}
}
}
else
{
text.text = "指定的路径下不存在此文件!";
}
}
3. 追加文件内容
/// <summary>
/// 追加文件内容
/// </summary>
public void BtnAppendFile_Click()
{
string path = Application.dataPath + "\\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();
}4.复制文件
/// <summary>
/// 复制文件
/// </summary>
public void BtnCopyFile_Click()
{
string oldPath = Application.dataPath + "\\Test.txt";
string newPath = Application.dataPath + "\\TestClone.txt";
File.Copy(oldPath, newPath);
}5.删除文件
/// <summary>
/// 删除文件
/// </summary>
public void BtnDeleteFile_Click()
{
string path = Application.dataPath + "\\TestClone.txt";
File.Delete(path);
}6.移动文件
/// <summary>
/// 移动文件
/// </summary>
public void BtnMoveFile_Click()
{
string oldPath = Application.dataPath + "\\Test.txt";
// 移动文件的同时也可以使用新的文件名
string newPath = "C:\\NewTest.txt";
File.Move(oldPath, newPath);
}7.创建目录
public void BtnCreateFile_Click()
{
string path = Application.dataPath + @"\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);
}
2.读取文件,文件内容
/// <summary>
/// 读取文件
/// </summary>
public void b移动文件tnReadFile_Click()
{
string path = Application.dataPath + "\\Test.txt";
text.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))
{
text.text += str + "\r\n";
}
else
{
sr.Close();
fs.Close();
break;
}
}
}
else
{
text.text = "指定的路径下不存在此文件!";
}
}
3. 追加文件内容
/// <summary>
/// 追加文件内容
/// </summary>
public void BtnAppendFile_Click()
{
string path = Application.dataPath + "\\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();
}4.复制文件
/// <summary>
/// 复制文件
/// </summary>
public void BtnCopyFile_Click()
{
string oldPath = Application.dataPath + "\\Test.txt";
string newPath = Application.dataPath + "\\TestClone.txt";
File.Copy(oldPath, newPath);
}5.删除文件
/// <summary>
/// 删除文件
/// </summary>
public void BtnDeleteFile_Click()
{
string path = Application.dataPath + "\\TestClone.txt";
File.Delete(path);
}6.移动文件
/// <summary>
/// 移动文件
/// </summary>
public void BtnMoveFile_Click()
{
string oldPath = Application.dataPath + "\\Test.txt";
// 移动文件的同时也可以使用新的文件名
string newPath = "C:\\NewTest.txt";
File.Move(oldPath, newPath);
}7.创建目录
/// <summary> /// 创建目录 /// </summary> public void BtnCreateDirectory_Click() { string path1 = "C:\\Jason1"; // 创建目录 Jason1 DirectoryInfo dDepth1 = Directory.CreateDirectory(path1); // dDepth2 指向 dDepth1 创建的子目录 Jason2 DirectoryInfo dDepth2 = dDepth1.CreateSubdirectory("Jason2"); // 设置应用程序当前的工作目录为 dDepth2 指向的目录 Directory.SetCurrentDirectory(dDepth2.FullName); // 在当前目录创建目录 Jason3 Directory.CreateDirectory("Jason3"); }8.
相关文章推荐
- C# 文件与目录的基本操作(System.IO)
- C# 文件与目录的基本操作(System.IO)
- C# 文件与目录的基本操作(System.IO)
- 实验楼linux学习笔记(四:Linux 目录结构及文件基本操作
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表...
- Linux-目录结构及文件基本操作
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件
- C#文件操作源码 递归复制一个目录 递归删除文件 和 文件夹
- Linux学习笔记——目录路径和文件基本操作
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- C# 文件的一些基本操作(转)//用C#读写ini配置文件
- [转]LINQ对文件目录的基本操作
- C# 文件操作(全部) 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名
- C#路径/文件/目录/I/O常见操作汇总
- Linux目录结构及文件基本操作
- Linux学习笔记--4.Linux 目录结构及文件基本操作
- C#目录文件复制、创建操作
- 有关C#中的文件操作之目录管理 个人整理