对目录操作例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace DirectoryandDirectoryInfo { class Program { static void Main(string[] args) { string dirPath = "d:\\dir2"; string filePath = string.Format("{0}\\{1}", dirPath, "test.txt"); //判断当前目录是否存在 if (!Directory.Exists(dirPath)) { //创建目录 Directory.CreateDirectory(dirPath); Console.WriteLine("创建一个目录{0}", dirPath); } else { Console.WriteLine("目录{0}已存在", dirPath); } //实例化文件对象 FileInfo file = new FileInfo(filePath); //判断文件是否存在 if (!file.Exists) { //创建文件 file.Create(); Console.WriteLine("创建文件{0}",filePath); } else { Console.WriteLine("文件{0}已存在", filePath); } Console.ReadKey(); } } }
时间: 2024-11-05 20:34:03