定义文件状态枚举:0-路径为空,1-存在文件,2-路径不为空,但文件不存在
public enum FileExsitStatus
{
NoPath=0,
FileExsit=1,
NoFile=2
}
利用File文件操作类判断文件是否存在:
/// <summary>
/// 判断指定路径的文件是否存在
/// </summary>
/// <param name="path">文件路径</param>
/// <returns>0-路径为空,1-存在文件,2-路径不为空,但文件不存在</returns>
protected FileExsitStatus IsFileExsit(object path)
{
if (path == null || Convert.IsDBNull(path) || string.IsNullOrEmpty(path.ToString()))
{
return FileExsitStatus.NoPath;
}
else
{
if (System.IO.File.Exists(Server.MapPath(@path.ToString())))
{
return FileExsitStatus.FileExsit;
}
return FileExsitStatus.NoFile;
}
}
需要注意的是,File类判断文件的路径必须为物理路径,相对路径返回的永远为false.
转载于:https://www.cnblogs.com/the-three/p/3736450.html
最后
以上就是追寻小白菜最近收集整理的关于C# 判断路径是否存在的全部内容,更多相关C#内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复