最后加一句return null;
因为你的foreach语句里的内容不一定就被执行

解决方案 »

  1.   

    估计是你这里有问题:StreamWriter srMyfile = new StreamWriter(fsMyfile,System.Text.Encoding.Default); 
    //System.Text.Encoding.Default 保证中文显示正确
      

  2.   

    return GetPath(Gid,f.FullName);Gid是什么?
      

  3.   


    return GetPath(Gid,f.FullName);//对每个子目录递归调用
    解释一下
      

  4.   

    最后加一个 return ;看看
      

  5.   

    对不起,上面的一句return GetPath(Gid,f.FullName);//对每个子目录递归调用
    改为      return GetPath(name,f.FullName);//对每个子目录递归调用请各位大虾再仔细看看为什么编译时报告:
         编译时错误:
               “GetPath(string, string)” : 并非所有的代码路径都返回值
      

  6.   

    public static string GetPath(string name,string initpath)
    {       
    。。
    return "";
    }
      

  7.   

    public static string GetPath(string name,string initpath)
    {        //根据name,在初始路径initpath下搜索是否存在
    //以naem命名的文件夹,若有,则返回其绝对路径,若无
    //则返回空串,使用递归算法
    //调用时传入所要搜索的文件夹名及所要搜索的初始路径
    if(name=="") return"";
             if(initpath=="") return "";
    DirectoryInfo dir=new DirectoryInfo(initpath);
             if(!dir.Exits) return "";
             
    DirectoryInfo[] subdirs=dir.GetDirectories(initpath);

             if(subdirs.Length==0) return"";//不存在,返回空串
    foreach(DirectoryInfo f in subdirs)
    {
        if(f.Name==name)
                   return f.FullName;//返回所要的文件夹的绝对路径
        else
          return GetPath(Gid,f.FullName);//对每个子目录递归调用
    }return "没有找到";
    }
      

  8.   

    string aaa;
    foreach(DirectoryInfo f in subdirs)
    {
        if(f.Name==name)
                   aaa= f.FullName;//返回所要的文件夹的绝对路径
        else
          aaa= GetPath(Gid,f.FullName);//对每个子目录递归调用
    }
    return aaa;
      

  9.   

    string aaa;
    ...if(...) aaa="";
    foreach(DirectoryInfo f in subdirs)
    {
        if(f.Name==name)
                   aaa= f.FullName;//返回所要的文件夹的绝对路径
        else
          aaa= GetPath(Gid,f.FullName);//对每个子目录递归调用
    }
    return aaa;
      

  10.   

    问题搞定,经过优化后得到一个比较不错的结果
    public static string GetPath(string name,string initpath)
    {        /*根据name,在初始路径initpath下搜索是否存在
    *以naem命名的文件夹,若有,则返回其绝对路径,若无
    *则返回空串,使用递归算法
    *调用时传入所要搜索的文件夹名及所要搜索的初始路径*/
    if(name==""||initpath=="") return"";
    DirectoryInfo dir=new DirectoryInfo(initpath);
             if(!dir.Exits) return "";
             
    DirectoryInfo[] subdirs=dir.GetDirectories();//原来的函数在这里有错 foreach(DirectoryInfo f in subdirs)
    {
        if(f.Name==name)
                   return f.FullName;//返回所要的文件夹的绝对路径
        else
          return GetPath(name,f.FullName);//对每个子目录递归调用
    }
             return "";
    }