本文主要介绍文件类、文件信息类、目录类、目录信息类的常用属性和方法
1、File(文件类)
1 2 3 4 5 6 7 8 9 10 11 12 13
| bool isFileExist = File.Exists(@"D:\test.txt");
File.Create(@"D:\testnew.txt"); if(isFileExist) { File.Copy(@"D:\testnew.txt", @"D:\test\testnew.txt", true); File.Move(@"D:\test1\testnew.txt", @"D:\test2\testnew.txt"); File.Delete(@"D:\testnew.txt"); }
|
2、FileInfo(文件信息类)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| FileInfo fileInfo = new FileInfo(@"D:\testfileinfo.txt");
bool isFileInfoExist = fileInfo.Exists;
if(isFileInfoExist) { fileInfo.CopyTo(@"D:\testfileinfonew.txt", true); fileInfo.MoveTo(@"D:\testfileinfonew1.txt"); fileInfo.Delete(); } #region 获取文件信息 Console.WriteLine("获取文件是否存在:" + fileInfo.Exists); if(fileInfo.Exists) { Console.WriteLine("获取文件名称:" + fileInfo.Name); Console.WriteLine("获取文件的大小:" + fileInfo.Length); Console.WriteLine("获取文件的完整目录:" + fileInfo.FullName); Console.WriteLine("获取文件的完整路径:" + fileInfo.DirectoryName); Console.WriteLine("获取文件的属性:" + fileInfo.Attributes); Console.WriteLine("获取文件是否只读:" + fileInfo.IsReadOnly); Console.WriteLine("获取文件的扩展名:" + fileInfo.Extension); Console.WriteLine("获取文件的创建时间:" + fileInfo.CreationTime); Console.WriteLine("获取文件的UTC创建时间:" + fileInfo.CreationTimeUtc); Console.WriteLine("获取文件最后一次被访问的时间:" + fileInfo.LastAccessTime); Console.WriteLine("获取文件最后一次被访问的UTC时间:" + fileInfo.LastAccessTimeUtc); Console.WriteLine("获取文件最后一次被写入的时间:" + fileInfo.LastWriteTime); Console.WriteLine("获取文件最后一次被写入的UTC时间:" + fileInfo.LastWriteTimeUtc); } #endregion
|
3、Directory(目录类)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| bool isDirectoryExist = Directory.Exists(@"D:\test");
Directory.CreateDirectory(@"D:\test");
Directory.Delete(@"D:\test"); if(isDirectoryExist) { Directory.Delete(@"D:\test", true); Directory.Move(@"D:\test\test.txt", @"D:\test1\test1.txt"); string[] files = Directory.GetFiles("d:\\"); string[] filesWithSpecifySearchMode = Directory.GetFiles("d:\\", "*.txt"); string[] directories = Directory.GetDirectories("d:\\"); string[] directoriesWithSpecifySearchMode = Directory.GetDirectories("d:\\", "test*"); string currentDirectory = Directory.GetCurrentDirectory(); string baseDirectory = AppDomain.CurrentDomain.BaseDirectory; }
|
4、DirectoryInfo(目录信息类)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| DirectoryInfo directoryInfo = new DirectoryInfo("d:\\test");
bool isDirectoryInfoExist = directoryInfo.Exists;
directoryInfo.Create();
directoryInfo.Delete();
directoryInfo.Delete(true); if(isDirectoryInfoExist) { directoryInfo.MoveTo("d:\\test1"); DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories(); FileInfo[] fileInfos = directoryInfo.GetFiles(); FileInfo[] fileInfosWithSearchMode = directoryInfo.GetFiles("*.txt"); #region 获取目录或文件信息 Console.WriteLine("获取目录名称:" + directoryInfo.Name); Console.WriteLine("获取目录或文件的完整路径:" + directoryInfo.FullName); Console.WriteLine("获取文件的扩展名:" + directoryInfo.Extension); Console.WriteLine("获取目录的根部分:" + directoryInfo.Root); Console.WriteLine("获取或设置当前文件或目录的属性:" + directoryInfo.Attributes); Console.WriteLine("获取指定子目录的父目录:" + directoryInfo.Parent); Console.WriteLine("获取或设置当前文件或目录的创建时间:" + directoryInfo.CreationTime); Console.WriteLine("获取或设置当前文件或目录的UTC创建时间:" + directoryInfo.CreationTimeUtc); Console.WriteLine("获取或设置当前文件或目录最后被访问的时间:" + directoryInfo.LastAccessTime); Console.WriteLine("获取或设置当前文件或目录最后被访问的UTC时间:" + directoryInfo.LastAccessTimeUtc); Console.WriteLine("获取或设置当前文件或目录最后被写入的时间:" + directoryInfo.LastWriteTime); Console.WriteLine("获取或设置当前文件或目录最后被写入的UTC时间:" + directoryInfo.LastWriteTimeUtc); #endregion }
|
注:本文实例代码下载:C# File、FileInfo、Directory、DirectoryInfo
以上就是.NET Core 日志记录程序和常用日志记录框架的简单使用的介绍,做此记录,如有帮助,欢迎点赞关注收藏!