C#中判断文件夹或文件是否存在的

c#中操作IO非常简单,下面介绍如何判断文件夹或文件是否存在的方法。

代码如下:

var directoryPath = @"E:\Files";
//判断文件夹是否存在
if (!System.IO.Directory.Exists(directoryPath ))
{
    
    
  try
  {
    
    
     Directory.CreateDirectory(directoryPath );
   }
   catch (Exception e)
    {
    
    
    }
}
//判断某文件是否存在
var filePath = @"E:\Files\test.txt";
if(!File.Exists(filePath ))
{
    
    
	try
	{
    
    
        File.Create(filePath );
    }
    catch (Exception e)
    {
    
    
    }
}

原文链接:http://www.codes51.com/article/detail_414.html

猜你喜欢

转载自blog.csdn.net/weixin_45023328/article/details/111694612