string path = "f://哈哈"; if (Directory.Exists(path)) { MessageBox.Show("存在文件夹"); string filename = path+"//"+"CeShi.xml";//一定要在路径和文件名之间加 // if (File.Exists(filename)) { MessageBox.Show("文件存在"); } else { MessageBox.Show("不存在文件"); File.Create(filename);//创建文件 } } else { MessageBox.Show("不存在文件夹"); Directory.CreateDirectory(path);//创建路径 }
以下是代码片段可以自由组合:01 //判断文件夹的存在、创建、删除文件夹02 string path = "F://haha";//路径的正确写法03 if (Directory.Exists(path))//如果不存在就创建file文件夹04 { 05 MessageBox.Show("存在文件夹");
06 //Directory.Delete(path, false);//如果文件夹中有文件或目录,此处会报错 07 //Directory.Delete(path, true);//true代表删除文件夹及其里面的子目录和文件
08 }09 else10 { 11 MessageBox.Show("不存在文件夹");
12 Directory.CreateDirectory(path);//创建该文件夹13 }1415 //判断文件的存在、创建、删除文件16 string filename = path +"//"+ "11.txt";17 if (File.Exists(filename))18 { 19 MessageBox.Show("存在文件"); 20 File.Delete(filename);//删除该文件21 }22 else23 { 24 MessageBox.Show("不存在文件"); 25 File.Create(filename);//创建该文件,如果路径文件夹不存在,则报错。26 }
新闻热点
疑难解答