码迷,mamicode.com
首页 > 其他好文 > 详细

C# 判断路径是否存在

时间:2014-05-24 05:43:08      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:class   blog   c   code   color   a   

1
2
3
4
5
6
7
定义文件状态枚举:0-路径为空,1-存在文件,2-路径不为空,但文件不存在
 public enum FileExsitStatus
 {
       NoPath=0,
       FileExsit=1,
       NoFile=2
 }

利用File文件操作类判断文件是否存在:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/// <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.  

  

C# 判断路径是否存在,布布扣,bubuko.com

C# 判断路径是否存在

标签:class   blog   c   code   color   a   

原文地址:http://www.cnblogs.com/the-three/p/3736450.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!