C# 给定一个路径字符串,怎样判断是文件还是文件夹?

        string str = @"c:\test";
            if (System.IO.Directory.Exists(str))
            {
                //文件夹
            }
            else if(System.IO.File.Exists(str)) { 
                //文件
            }

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/// <summary>
        /// 判断目标是文件夹还是目录(目录包括磁盘)
        /// </summary>
        /// <param name="filepath">文件名</param>
        /// <returns></returns>
        public static bool IsDir(string filepath)
        {
            FileInfo fi = new FileInfo(filepath);
            if ((fi.Attributes & FileAttributes.Directory) != 0)
                return true;
            else
            {
                return false;
            }
        }
posted @ 2017-10-27 16:29  sky20080101  阅读(3617)  评论(0)    收藏  举报