运算符'<'不能应用于类型'方法组'和'字符串'的操作数在c#

问题描述:

我有这个代码在c#中上传的问题。运算符'<'不能应用于类型'方法组'和'字符串'的操作数在c#

我需要防止上传多个重复的名称文件。

的错误是:

操作“<”不能应用于类型 和“串”

什么问题“方法组”的操作数?

我的代码如下,提前谢谢。

DirectoryInfo objDir = 
    new DirectoryInfo(Server.MapPath("\\images\\")); 

string sFileName = Path.GetFileName(hpf.FileName); 
string sFileExt = Path.GetExtension(hpf.FileName); 

FileInfo[] objFI = 
    objDir.GetFiles(sFileName.Replace(sFileExt, "") + ".*"); 

if (objFI.Length > 0) 
{ 
    foreach (FileInfo file in objFI) 
    { 
     string sFileName1 = objFI[0].Name; 
     string sFileExt1 = Path.GetExtension < (objFI[0].Name); // LINE ERROR 

     if (sFileName1.Replace(sFileExt1, "") == 
       sFileName.Replace(sFileExt, "")) 
     { 
      iFailedCnt += 1;  // NOT ALLOWING DUPLICATE. 
      break; 
     } 
    } 
} 

编辑#1

protected void Upload_Files(object sender, EventArgs e) 
    { 
     if (fileUpload.HasFile)  // CHECK IF ANY FILE HAS BEEN SELECTED. 
     { 
      int iUploadedCnt = 0; 
      int iFailedCnt = 0; 
      HttpFileCollection hfc = Request.Files; 
      lblFileList.Text = "Select <b>" + hfc.Count + "</b> file(s)"; 

      if (hfc.Count <= 10) // 10 FILES RESTRICTION. 
      { 
       for (int i = 0; i <= hfc.Count - 1; i++) 
       { 
        HttpPostedFile hpf = hfc[i]; 
        if (hpf.ContentLength > 0) 
        { 
         if (!File.Exists(Server.MapPath("\\images\\") + 
          Path.GetFileName(hpf.FileName))) 
         { 
          DirectoryInfo objDir = 
           new DirectoryInfo(Server.MapPath("\\images\\")); 

          string sFileName = Path.GetFileName(hpf.FileName); 
          string sFileExt = Path.GetExtension(hpf.FileName); 

          // CHECK FOR DUPLICATE FILES. 
          FileInfo[] objFI = 
           objDir.GetFiles(sFileName.Replace(sFileExt, "") + ".*"); 

          if (objFI.Length > 0) 
          { 
           // CHECK IF FILE WITH THE SAME NAME EXISTS 
            (IGNORING THE EXTENTIONS). 
           foreach (FileInfo file in objFI) 
           { 
            string sFileName1 = objFI[0].Name; 
            string sFileExt1 = Path.GetExtension(objFI[0].Name); 

            if (sFileName1.Replace(sFileExt1, "") == 
              sFileName.Replace(sFileExt, "")) 
            { 
             iFailedCnt += 1;  // NOT ALLOWING DUPLICATE. 
             break; 
            } 
           } 
          } 
          else 
          { 
           // SAVE THE FILE IN A FOLDER. 
           hpf.SaveAs(Server.MapPath("\\images\\") + 
            Path.GetFileName(hpf.FileName)); 
           iUploadedCnt += 1; 
          } 
         } 
        } 
       } 
       lblUploadStatus.Text = "<b>" + iUploadedCnt + "</b> file(s) Uploaded."; 
       lblFailedStatus.Text = "<b>" + iFailedCnt + 
        "</b> duplicate file(s) could not be uploaded."; 
      } 
      else lblUploadStatus.Text = "Max. 10 files allowed."; 
     } 
     else lblUploadStatus.Text = "No files selected."; 
    } 
+1

Path.GetExtension

+0

你不需要此验证的方式'if(objFI.Length> 0)' – Disappointed

+1

这只是一个错字,在参数之前有一个 Steve

获取扩展是一个功能,您将它和它的()

替换此之间有<

string sFileExt1 = Path.GetExtension < (objFI[0].Name); 

带:

string sFileExt1 = Path.GetExtension(objFI[0].Name); 
+0

感谢您的回复,现在我没有错误,但在案件重复的文件中,文件未上传,但未上传的文件总数始终为0.请在我的第一个问题中参阅**编辑#1 **。 –

+2

@AntonioMailtraq这是另一个问题。 [变色龙的问题在SO上不鼓励](http://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions)。请参阅链接中的第4点。 – spender

+0

对于那个调试它,看看你的逻辑无法工作。同时查看史蒂夫的评论以使代码更清晰。 –