我有一个功能,可以通过确定String Path
是File
还是Directory
来自动创建指定的Path
。如果路径不存在,如何检查字符串路径是“文件”还是“目录”?
FileAttributes attributes = File.GetAttributes("//Path");
if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
Directory.CreateDirectory("//Path");
}
但是如果它不:
通常情况下,我想如果路径已经exists
使用?如何检查String Path
是否为File
或Directory
如果不存在?
'File.Exists'和'Directory.Exists' – 2014-09-22 13:57:49
您要检查的东西,甚至不存在?如果你想检查一个给定的名字是否是一个有效的文件名,你可以简单地检查它是否有扩展名,否则它可能是一个目录。 – HimBromBeere 2014-09-22 13:58:10
@MetroSmurf认真吗? “如果路径不存在,如何检查路径是文件还是目录?” – Rawling 2014-09-22 13:58:13