一个程序,向服务器传图片,图片名字为1,2,3,4……,要求每次上传后图片名字自动加1,
string[] dirs = Directory.GetFiles(HttpContext.Current.Server.MapPath("/") + "/picture/Statue of Liberty/");
                int max=0;
//求出名字最大的图片的名字
                for(int j=0;j<dirs.Length;j++)
                {   
                if (Convert.ToInt32(dirs[j]) > max)
                {
                    max = Convert.ToInt32(dirs[j]);
                }
                }
//新图片的路径
paths = HttpContext.Current.Server.MapPath("/") + "/picture/Statue of Liberty" + "/" + (max+1).ToString();
各位大侠,我哪里错了?

解决方案 »

  1.   

    看看 dirs 里的值是什么`
      Convert.ToInt32  转换的时候 如果dirs里的值 如果是 1.gif 这样的`
    肯定会出错了
      

  2.   

    max = Convert.ToInt32(dirs[j]); //不确定的说,考虑一下文件名会是x:\yyyy\zzz\1. 形式,小心无法转为整形
      

  3.   

    问题应该出在这个地方,你跟踪看看Convert.ToInt32(dirs[j])
      

  4.   

    string的
    substring
    lastindexof
    indexof
    结合使用
      

  5.   

    Split如果dirs[0] = "1.gif";
    string[] temp = dirs[0].Split('-');则: temp[0] = "1";
    temp[1] = "gif";
      

  6.   

    //示例化文件上传的类
    HttpPostedFile uplode=file.PostedFile;
    //得到上传文件名
    string filePath=uplode.FileName;
    //得到文件后缀名
    string exName=System.IO.Path.GetExtension(filePath);