【C#-文件管理】 判断文件夹或文件是否存在,并创建文件夹或文件

 1、判断文件夹是否存在

Directory.Exists(“文件夹路径”)    返回false表示不存在,true表示存在

2、判断文件是否存在

File.Exists("文件路径")    返回false表示不存在,true表示存在

3、创建文件夹

DirectoryInfo dir=new DirectoryInfo("文件夹路径");

dir.Create();

4、创建文件

Fire.Create("文件路径");

 

            //如果文件夹不存在就创建文件夹
            string spath=@"D:\下载文件";
            if(Directory.Exists(spath)==false)
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(spath);
                directoryInfo.Create();
            }
            string filepath=@"D:\下载文件\test.xml";
            if (File.Exists(filepath) == false)
            {
                File.Create(@"D:\下载文件\test.xml");
            }

 

posted @ 2016-08-31 14:27  发明创造小能手  阅读(1235)  评论(0编辑  收藏  举报
levels of contents