1 /// <summary> 2 /// FileInfo相关操作 3 /// </summary> 4 public static void OperationAboutFileInfo() 5 { 6 #region 操作文件FileInfo 7 8 ////右键MyFirstTxt ==>属性==>始终复制 9 FileInfo fileInfo = new FileInfo("MyFirstTxt.txt"); 10 11 Console.WriteLine(fileInfo.Exists); 12 13 //获取文件后缀名 14 Console.WriteLine(fileInfo.Extension); 15 16 //打印文件名 17 Console.WriteLine(fileInfo.Name); 18 19 //获取文件目录 20 Console.WriteLine(fileInfo.Directory); 21 22 Console.WriteLine(fileInfo.Length); 23 24 Console.WriteLine(fileInfo.IsReadOnly); 25 26 //复制文件 27 fileInfo.CopyTo("tt.txt"); 28 29 fileInfo.Delete(); 30 31 32 33 #endregion 操作文件FileInfo 34 }
1 /// <summary> 2 /// 判断、创建文件、重命名 3 /// </summary> 4 /// <param name="fileName"></param> 5 public static void CreateFileAndRename(string fileName) 6 { 7 FileInfo fileInfo = new FileInfo(fileName); 8 if (!fileInfo.Exists) 9 { 10 fileInfo.Create(); 11 } 12 13 //移动到同名目录中 相当于重命名 14 fileInfo.MoveTo("HelloWorld.txt"); 15 }