我们偶尔会用到文件操作,其中File、Path和Directory这三个类是比较常见的,今天写了一个测试demo,也是顺便学习一下,记录一二。
BTW,使用这几个类的时候需要引用using System.IO命名空间。
class Program
{
static void Main(string[] args)
{
//ProcessPath();
//ProcessFile();
//ReadFileByByteArray();
//ReadFileByAllLines();
//ReadFileByAllText();
//WriteFileByByteArray();
//WriteFileByLines();
//WriteFileByText();
UseDirectory();
Console.ReadKey();
}
/// <summary>
/// path类只是在string层面进行操作
/// </summary>
private static void ProcessPath()
{
string path = @"C:\Users\Administrator\Desktop\XX-Net.txt";
string directory = Path.GetDirectoryName(path);
string fileName = Path.GetFileName(path);
string fileNamenoExtension = Path.GetFileNameWithoutExtension(path);
string fileExtension = Path.GetExtension(path);
string ss = Path.ChangeExtension(path, "csv");
Console.WriteLine($"directory is {directory},fileName is {fileName},file without extension is{fileNamenoExtension},extension is {fileExtension},new file is{ss} ");
}
private static void ProcessFile()
{
string path = @"C:\Users\Administrator\Desktop\2.txt";
//File.Create(path);
//File.Delete(path);
//File.Copy(path, @"C:\Users\Administrator\Desktop\3.txt");
File.Move(path, @"C:\Users\Administrator\Desktop\4.txt");
Console.WriteLine("creat successfully...");
}
private static void ReadFileByByteArray()
{
string path = @"C:\Users\Administrator\Desktop\3.txt";
byte[] fileByte = File.ReadAllBytes(path);
string fileByByte = Encoding.Default.GetString(fileByte);
Console.WriteLine(fileByByte);
}
private static void ReadFileByAllLines()
{
string path = @"C:\Users\Administrator\Desktop\3.txt";
string[] fileLines = File.ReadAllLines(path,Encoding.Default);
foreach (var line in fileLines)
{
Console.WriteLine(line);
}
}
private static void ReadFileByAllText()
{
string path = @"C:\Users\Administrator\Desktop\3.txt";
string fileText = File.ReadAllText(path,Encoding.Default);
Console.WriteLine(fileText);
}
private static void WriteFileByByteArray()
{
string path = @"C:\Users\Administrator\Desktop\1.txt";
string fileStr = "大家好,我来了,我的号码是1388888888";
byte[] fileByte = Encoding.Default.GetBytes(fileStr);
File.WriteAllBytes(path, fileByte);
Console.WriteLine("Write Sucessfully...");
}
private static void WriteFileByLines()
{
string path = @"C:\Users\Administrator\Desktop\1.txt";
string[] fileArray = { "你好","我的名字","叫Tom","我来自安徽省阜阳市太和县","他们","都知道我是个好人" };
File.WriteAllLines(path, fileArray);
Console.WriteLine("Write Sucessfully...");
}
private static void WriteFileByText()
{
string path = @"C:\Users\Administrator\Desktop\1.txt";
//File.WriteAllText(path, "落霞与孤鹜齐飞,秋水共长天一色。");
File.AppendAllText(path, "宜将剩勇追穷寇,不可沽名学霸王!");
Console.WriteLine("Write Sucessfully...");
}
private static void UseDirectory()
{
//Directory.CreateDirectory(@"C:\Users\Administrator\Desktop\NewFolder");
//Directory.Delete(@"C:\Users\Administrator\Desktop\NewFolder",true);
string[] files = Directory.GetFiles(@"D:\迅雷下载", "*.rmvb",SearchOption.TopDirectoryOnly);
foreach (var file in files)
{
Console.WriteLine(file);
}
Console.WriteLine("Get File Successfully...");
}
}