给定一字符串如何区分文件和路径
没搞定

解决方案 »

  1.   

    FileInfo file = new FileInfo("");
                if(!string.IsNullOrEmpty(file.Extension))
                { isFile;             }else{
    }
      

  2.   

    判断文件的扩展名是不是空,这个做法不行的。因为有些文件是没有扩展名的。
    我能想到的办法,
    bool isExist = File.Exist(string filepath)
    不过这个只能判断文件是不是存在,而不能判断是不是文件名。
    个人觉得,如果是C:\\aaa这种字符串,是没有办法仅通过判断字符串的规则来确定是否是文件名或者目录。
    但是可以用File.Exist或者Directory的类似函数,看对应的文件或者目录是否真实存在。
      

  3.   

    在没有调用api的情况下
    hellomartin你的方法太棒了,我再等等
      

  4.   

    文件、目录必须存在才可以判定的。目录名也可以是C:\test.txt。string filename = "x:\abc\def";
    FileAttributes fa = File.GetAttributes(filename);
    if(fa & FileAttributes.Directory)
        // it's a directory
    else
        // it's a file
      

  5.   

    刚才的代码错一点:
    if( (fa & FileAttributes.Directory) = FileAttributes.Directory)
      

  6.   

    啊!!!还是错了,应该是==:
    if( (fa & FileAttributes.Directory) == FileAttributes.Directory)
      

  7.   

    方法还是蛮多的最好根据文件的属性来判断,因为文件夹也是可以命名成:×××××.exe等的,所以这个东西是文件夹,不是exe文件
    楼上几个都不妨试试,但是绝对不能根据字符串来判断
      

  8.   

    edyang 
    great!
    结了
      

  9.   


                string path = @"D:\test\add\a.exe";
                bool isDir = false;
                int index = path.LastIndexOf('\\');
                DirectoryInfo d = new DirectoryInfo(path.Substring(0, index));
                foreach (DirectoryInfo ds in d.GetDirectories())
                {
                    if (ds.Name.CompareTo(path.Substring(index + 1))==0)
                    {
                        isDir = true;
                        break;
                    }
                }