string _uninstallPath = GetPath();
DataSet ds = Resolve();
try 
   {
      string[] ChildModelDir = System.IO.Directory.GetDirectories(_uninstallPath);
      for(int i=0;i<ChildModelDir.Length;i++)
      {
for(int j=0;j<ds.Tables[0].Rows.Count;j++)
{
   DataRow dr = ds.Tables[0].Rows[i];
   foreach(string directory in ChildModelDir)
   {
      string ChildModelPath = _uninstallPath + "\\"+dr[1].ToString();
      if(directory == ChildModelPath )
      {
return true;
      }
   }
}
      }
   }
   catch
  {}
  return false;

解决方案 »

  1.   

    string _uninstallPath = GetPath();
    DataSet ds = Resolve();
    try 
       {
          string[] ChildModelDir = System.IO.Directory.GetDirectories(_uninstallPath);
          for(int i=0;i<ChildModelDir.Length;i++)//遍历子目录,i为数组索引
          {
    for(int j=0;j<ds.Tables[0].Rows.Count;j++)//遍历DataTable,j为行索引
    {
       DataRow dr = ds.Tables[0].Rows[i];//数组索引怎么引用行集内容?狂晕!
       foreach(string directory in ChildModelDir)//再次遍历子目录,何意?
       {
          string ChildModelPath = _uninstallPath + "\\"+dr[1].ToString();
          if(directory == ChildModelPath )
          {
    return true;
          }
       }
    }
          }
       }
       catch
      {}
      return false;
      

  2.   

    写得有问题,DataRow dr = ds.Tables[0].Rows[i];这句,很容易出错的,你怎么知道ChildModelDir.Length的值一定比取得的行数多?如果ChildModelDir.Length > ds.Tables[0].Rows.Count的话肯定要报错的
      

  3.   

    是不是这样?string _uninstallPath = GetPath();
    DataSet ds = Resolve();
    try 
       {
          string[] ChildModelDir = System.IO.Directory.GetDirectories(_uninstallPath);
          foreach(string directory in ChildModelDir)
          {
    foreach(DataRow dr in ds.Tables[0].Rows)
    {
          string ChildModelPath = _uninstallPath + "\\"+dr[1].ToString();
          if(directory == ChildModelPath )
          {
    return true;
          }
    }
          }
       }
       catch
      {}
      return false;
      

  4.   

    @viena(维也纳nn木人 [IQ=50,EQ<0]) 
    dr[1].ToString();
    从哪里取?
      

  5.   

    @skywolfma(蓝色愿望沙) 
    我知道问题
      

  6.   

    @skywolfma(蓝色愿望沙) 
    我知道有问题
      

  7.   

    viena(维也纳nn木人 [IQ=50,EQ<0]) ( )正解
    如果不习惯foreach 其实去掉你最后一个foreach就可以了
    string _uninstallPath = GetPath();
    DataSet ds = Resolve();
    try 
       {
          string[] ChildModelDir = System.IO.Directory.GetDirectories(_uninstallPath);
          for(int i=0;i<ChildModelDir.Length;i++)
          {
    for(int j=0;j<ds.Tables[0].Rows.Count;j++)
    {
       DataRow dr = ds.Tables[0].Rows[i];
     
          string ChildModelPath = _uninstallPath + "\\"+dr[1].ToString();
          if(directory == ChildModelPath )
          {
    return true;
          }
      
    }
          }
       }
       catch
      {}
      return false;
      

  8.   

    @hertcloud(·£DOTNET→C/C++£·) 
    如果那样的话
    directory 就没有定义了
      

  9.   

    string _uninstallPath = GetPath();
    DataSet ds = Resolve();
    try 
       {
          string[] ChildModelDir = System.IO.Directory.GetDirectories(_uninstallPath);
          for(int i=0;i<ChildModelDir.Length;i++)
          {
       DataRow dr = ds.Tables[0].Rows[i];
       foreach(string directory in ChildModelDir)
       {
          string ChildModelPath = _uninstallPath + "\\"+dr[1].ToString();
          if(directory == ChildModelPath )
          {
    return true;
          }
       }
           }
       }
       catch
      {}
      return false;
      

  10.   

    //dr[1].ToString();
    //从哪里取?什么从哪里取?dr是个DataRow啊,dr[1]取索引为1的列的值foreach(DataRow dr in ds.Tables[0].Rows)
    就是遍历行集,取得每一行给dr
    楼主,你最后的代码遍历两次ChildModelDir数组,而没有遍历DataTable
      

  11.   

    sorry,看错了
    第一重循环是遍历DataTable,但循环条件错了
    i<ChildModelDir.Length
    应该是
    i<ds.Tables[0].Rows.Count用foreach其实是一样的
      

  12.   

    @viena(维也纳nn木人 [IQ=50,EQ<0]) 
    搜索目标路径下是否有文件夹
    如果有文件夹的话
    判断 文件夹名 是否等于DataTable中某条数据如果有相等就返回true
      

  13.   

    如果你要进一步提高性能,可以先循环遍历DataTable一次,把所需的值放入一个数组,在双重循环的内部,只要遍历这个数组进行查找,效率比遍历DataTable要高~
      

  14.   

    @viena(维也纳nn木人 [IQ=50,EQ<0]) 
    我需要更简单的
    用这个代码的话 _uninstallPath 为空的话
    就会异常了!!!!
      

  15.   

    首先这个代码肯定是错的,DataRow dr = ds.Tables[0].Rows[i];如果代码是错的,那优化个头啊,越优化越错楼主先把代码写对了再说吧
      

  16.   

    其实我能理解楼主的意图,如果没错的话你只需要对 ChildModelDir 建立一个哈希表然后循环 ds.Tables[0]中的记录一遍搜索就ok了
      

  17.   

    DataRow dr = ds.Tables[0].Rows[i];
    为什么是错的?