检查文件夹是否存在,如果不使用C#则创建 [英] Check folder exists and create if not using C#

查看:271
本文介绍了检查文件夹是否存在,如果不使用C#则创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我用按钮创建了一个窗体。

我需要检查路径(例如,c:\ test \)是否可以有文件夹。如果不是意味着创建一个名为1_date戳的新文件夹(例如1_20120711)。



如果是,则表示创建一个自动增量文件夹(即2_20120711)。



请帮帮我。



谢谢,

Murugavel S

解决方案

尝试



string foldername = DateTime.Now.ToString();

  if (!Directory.Exists( @  C:/ test / + foldername)
Directory.CreateDirectory( @ C: / test / + foldername);





文件夹名称基于日期 - 但它不是您的格式。



如果日期已存在的文件夹,请创建一个增加数字的新文件夹,

根据上面的代码,你总是可以自己尝试。


你可以使用: -

Quote:

foreach(args中的字符串路径)

{

if( File.Exists(路径))

{

//此路径是一个文件

ProcessFile(路径);

}

else if(Directory.Exists(path))

{

//这条路径是目录

ProcessDirectory(路径);

}

其他

{

控制台.WriteLine({0}不是有效的文件或目录。,路径);

}

}

}



你学习更多关于linq的内容; -

http://msdn.microsoft.com/en-us/library/system.io.directory.exists.aspx [ ^ ]


使用

 System.IO.Directory.CreateDirectory 





目录。 CreateDirectory(@C:/ test /+ folde RNAME); 





如果目录不存在,CreateDirectory已经处理了检查


Hi,

I have create a windows form with button.
I need to check whether the path (eg, c:\test\) can have folder are not. If not means create a new folder called 1_date stamp (eg 1_20120711).

If yes means create a auto increment folder (ie. 2_20120711).

Please help me.

Thanks,
Murugavel S

解决方案

Try

string foldername = DateTime.Now.ToString();

if (!Directory.Exists(@"C:/test/" + foldername)
       Directory.CreateDirectory(@"C:/test/" + foldername);



The folder name is based on date - however it is not in your format.

If a folder with the date already exists, create a new one with an incremented number,
Based on the code above, you can always try this on your own.


you can used :-

Quote:

foreach(string path in args)
{
if(File.Exists(path))
{
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
else
{
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}


and you study more into following linq;-
http://msdn.microsoft.com/en-us/library/system.io.directory.exists.aspx[^]


use

System.IO.Directory.CreateDirectory



Directory.CreateDirectory(@"C:/test/" + foldername);



CreateDirectory already handles the check if the directory does not exists


这篇关于检查文件夹是否存在,如果不使用C#则创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆