【C#】C# System.IO的一些文件操作
2016-12-14 17:22
519 查看
文章转载:http://www.manew.com/thread-98270-1-1.html
/// <summary>
/// 创建文件夹“MyTest”和文件“1.txt”
/// </summary>
void CreateFileAndDirector()
{
// 指定一个“当前活动文件夹”
string activeDir = @"d:\";
// 合并路径字符串 组合成:d:\MyTest
string newPath = Path.Combine(activeDir, "MyTest");
// 创建文件夹
Directory.CreateDirectory(newPath);
// 创建一个新文件的名字
string newFileName = "1.txt";
// 指定该文件位置
newPath = Path.Combine(newPath, newFileName);
// 创建文件
if (!File.Exists(newPath))
{
File.Create(newPath);
}
}
/// <summary>
/// 创建文件夹“MyTest”和文件“1.txt”
/// </summary>
void CreateFileAndDirector()
{
// 指定一个“当前活动文件夹”
string activeDir = @"d:\";
// 合并路径字符串 组合成:d:\MyTest
string newPath = Path.Combine(activeDir, "MyTest");
// 创建文件夹
Directory.CreateDirectory(newPath);
// 创建一个新文件的名字
string newFileName = "1.txt";
// 指定该文件位置
newPath = Path.Combine(newPath, newFileName);
// 创建文件
if (!File.Exists(newPath))
{
File.Create(newPath);
}
}
/// <summary> /// 写入文件 /// </summary> void WriteTextFile() { string path = @"d:\MyTest\1.txt"; string text = "A class is the most powerful data type in C#. Like structures, " + "a class defines the data and behavior of the data type. "; // 不分行 File.WriteAllText(path, text); string[] lines = { "First line", "Second line", "Third line" }; // 写到三行 File.WriteAllLines(@"d:\MyTest\1.txt", lines); }
/// <summary> /// 读取文件 /// </summary> void ReadTextFile() { // 读取所有内容 string text = File.ReadAllText(@"d:\MyTest\1.txt"); Debug.Log(text); // 分行读取所有内容 string[] lines = File.ReadAllLines(@"d:\MyTest\1.txt"); Debug.Log(lines[0]); }
/// <summary> /// 复制和移动文件 /// </summary> void CopyAndMoveFile() { string fileName = "1.txt"; string sourcePath = @"d:\MyTest"; string targetPath = @"d:\MyTest\SubDir"; string sourceFile = Path.Combine(sourcePath, fileName); string destFile = Path.Combine(targetPath, fileName); if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } // 复制文件,TRUE为如果目标目录已存在该文件,则覆盖;FALSE已存在该文件 则取消复制 File.Copy(sourceFile, destFile, true); // 移动文件 //File.Move(sourceFile, destFile); }
using UnityEngine;
using System.Collections;
using System.IO;
public class Test : MonoBehaviour {
void Start()
{
CreateFileAndDirector();
WriteTextFile();
ReadTextFile();
CopyAndMoveFile();
}
/// <summary>
/// 创建文件夹“MyTest”和文件“1.txt”
/// </summary>
void CreateFileAndDirector()
{
// 指定一个“当前活动文件夹”
string activeDir = @"d:\";
// 合并路径字符串 组合成:d:\MyTest
string newPath = Path.Combine(activeDir, "MyTest");
// 创建文件夹
Directory.CreateDirectory(newPath);
// 创建一个新文件的名字
string newFileName = "1.txt";
// 指定该文件位置
newPath = Path.Combine(newPath, newFileName);
// 创建文件
if (!File.Exists(newPath))
{
File.Create(newPath);
}
}
/// <summary> /// 写入文件 /// </summary> void WriteTextFile() { string path = @"d:\MyTest\1.txt"; string text = "A class is the most powerful data type in C#. Like structures, " + "a class defines the data and behavior of the data type. "; // 不分行 File.WriteAllText(path, text); string[] lines = { "First line", "Second line", "Third line" }; // 写到三行 File.WriteAllLines(@"d:\MyTest\1.txt", lines); }
/// <summary> /// 读取文件 /// </summary> void ReadTextFile() { // 读取所有内容 string text = File.ReadAllText(@"d:\MyTest\1.txt"); Debug.Log(text); // 分行读取所有内容 string[] lines = File.ReadAllLines(@"d:\MyTest\1.txt"); Debug.Log(lines[0]); }
/// <summary> /// 复制和移动文件 /// </summary> void CopyAndMoveFile() { string fileName = "1.txt"; string sourcePath = @"d:\MyTest"; string targetPath = @"d:\MyTest\SubDir"; string sourceFile = Path.Combine(sourcePath, fileName); string destFile = Path.Combine(targetPath, fileName); if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } // 复制文件,TRUE为如果目标目录已存在该文件,则覆盖;FALSE已存在该文件 则取消复制 File.Copy(sourceFile, destFile, true); // 移动文件 //File.Move(sourceFile, destFile); }
}
相关文章推荐
- C# System.IO.Directory、System.IO.File文件操作
- C# System.IO文件操作整理
- System.IO 路径,文件,目录,I/O常见操作汇总(C#)
- C# System.IO文件操作整理
- System.IO 路径,文件,目录,I/O常见操作汇总(C#)
- C# 文件与目录的基本操作(System.IO)
- C# 文件与目录的基本操作(System.IO)
- C# 文件与目录的基本操作(System.IO)
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- 有关对文件的操作(System.IO)
- C# System.IO.FileStream 文件正由另一进程使用,因此该进程无法访问该文件
- 文件IO一些操作
- C# System.IO.FileStream 读取被其他程序打开的文件提示“文件正由另一进程使用,因此该进程无法访问该文件。”
- C#文件IO操作(转)
- C#中的文件的IO原理及操作
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- System.IO之windows文件操作
- c#中文件操作的一些相关函数
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据