C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

时间:2023-03-09 09:09:21
C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

 

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

 

1.管理文件系统


一般而言,应用程序都会有保存数据、检索数据的需求。

1.1 使用 path 类来访问文件路径

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

【path常用的方法】:http://www.cnblogs.com/tangge/archive/2012/10/30/2746458.html#a3

 

1.2 使用 File 和 FileInfo 类访问文件

1.2.1 File 类

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

 

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

        static void Main(string[] args)
{
string sourceFileName = @"F:\a.txt"; //源文件
string destFileName = @"c:\a.txt"; //目标文件
//如果源文件不存在
if (!File.Exists(sourceFileName))
{
File.Create(sourceFileName).Close();
}
//如果目标文件存在,先删除
if (File.Exists(destFileName))
{
File.Delete(destFileName);
} File.Copy(sourceFileName, destFileName);
File.Delete(sourceFileName);
}

 

重复记录

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

string sourceFileName = @"F:\a.txt"; //源文件
string destFileName = @"c:\a.txt"; //目标文件 StreamWriter sw = File.AppendText(destFileName);
sw.WriteLine(string.Format("{0}复制完毕", DateTime.Now));
sw.Flush();
sw.Close();

 

1.2.2 FileInfo 类

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

 

Length

private static void Main(string[] args)
{
string path = @"E:\中天IT\视频\DVD-ASP.NET\DVD-张波.NETC1001\IO详解——张波.NETC1001\1上次复习_作业讲解.avi";
FileInfo fi = new FileInfo(path);
Console.WriteLine(
string.Format("本文件为{0:#.00}M", fi.Length / (1024 * 1024)));
}

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

 

1.3 使用Directory 和 DirectoryInfo 类访问目录

1.3.1 Directory 类

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

 

string path = @"F:\tt\aeg\www";
if (Directory.Exists(path))
{
Directory.Delete(path);
}
else
{
Directory.CreateDirectory(path);
}

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

 

 

string path = @"F:\tt\aeg";

            //GetFiles 检索文件列表
string[] aa= Directory.GetFiles(path);
foreach (var a in aa)
{
Console.WriteLine(a);
} Console.WriteLine("------------");
//GetDirectories 检索文件夹列表
foreach (var s in Directory.GetDirectories(path))
{
Console.WriteLine(s);
} Console.WriteLine("------------");
//GetDirectories 检索文件夹和文件列表
foreach (var s in Directory.GetFileSystemEntries(path))
{
Console.WriteLine(s);
}

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

 

1.3.2 DirectoryInfo 类

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

 

 

1.4 使用 DriveInfo 类访问驱动器

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

Console.WriteLine("驱动器{0},类型为{1},",dr.Name,dr.DriveType);
//if (dr.IsReady)
//{
Console.WriteLine("可用空间为{0}", dr.AvailableFreeSpace);
//}

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

 

 

Console.WriteLine("驱动器{0},类型为{1},", dr.Name, dr.DriveType);
if (dr.IsReady)//设备已经准备好
{
Console.WriteLine("\t可用空间为{0}G", dr.AvailableFreeSpace/(1024*1024*1024)); //41G
Console.WriteLine("\t分区格式为{0}\n",dr.DriveFormat); //NTFS
}

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

 

1.5 FileSystemWatcher 类

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

 

 

 

2.使用字节流

3.管理应用程序数据

4.高效操作字符串