using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;namespace IOZuoye
{
    class Program
    {
        public bool isPath(string path)
        {
            DriveInfo[] dr = DriveInfo.GetDrives();            bool isPath_ = false;
            foreach (DriveInfo d in dr)
            {
                if (path.ToUpper().IndexOf(d.RootDirectory.FullName.ToUpper()) == 0)
                {
                    isPath_ = true;
                    return isPath_;
                    
                   
                }
            }
            return isPath_;
        }
       
        public static void Main()
        {
            Program p = new Program();
            bool b = p.isPath("e:\\asdf/asf");
            Console .WriteLine (b);
            
        }
    }
}
还是刚才我字符串的问题,特此我把问题重新贴一下
假设有一个字符串 path ,如何判断它是不是一个合法的路径? 
PS:path 可以是不存在的路径,只要合法就可以。 
例如:path="e:\\abc\c\a.txt"; 
可能E盘下没有这个文件夹,但是它可以被创建,所以就合法。 
再如: path="f:\\a.txt";如果我的电脑根本就没有F盘,这个文件不可能被创建,也就不合法。 
谢谢了。 
满意答案马上给分! 

解决方案 »

  1.   

    System.IO.Directory.Exists(string path)
      

  2.   

    2楼的对吗?using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;namespace IOZuoye
    {
        class Program
        {
            public bool isPath1(string path)
            {
                bool isp = Directory.Exists(path);
                return isp ;
                
            }
           
            public static void Main()
            {
                Program p = new Program();
                bool c = p.isPath("h:\\abc//");
                Console.WriteLine(c);
            }
        }
    }
    返回值是TRUE
      

  3.   

    那你就待测试的路径建立一个嘛
    try一下 成功了 就再把它删除 不成功就是不合法了
      

  4.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;namespace IOZuoye
    {
        class Program
        {
            public bool isPath(string path)
            {
              private bool ExistsPath(string path)
             {
                try
                {
                    Directory.CreateDirectory(path);
                    Directory.Delete(path);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
           
            public static void Main()
            {
                Program p = new Program();
                bool d = p.ExistsPath("h:\\abc//");
            }
        }
    }
    按照4楼的说法,返回值为NULL
      

  5.   

    那你的现有一套规则,不合法的规则,然后建立一个函数,
    函数名:判断路径是否合法
    输入参数:路径,string
    返回值:是否合法,true \false内部使用路径和规则比较
      

  6.   

    直接用这个 应该行吧
              private bool ExistsPath(string path)
             {
                try
                {
                    Directory.CreateDirectory(path);
                    Directory.Delete(path);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    外面就不要再套东西了
      

  7.   

    private bool ExistsPath(string path)
    {
    if (System.IO.Directory.Exists(path))
    return true;
    try
    {
    System.IO.Directory.CreateDirectory(path);
    System.IO.Directory.Delete(path);
    return true;
    }
    catch 
    {
    return false;
    }
    }前面加个判断保险点,否则会删错