我有這個代碼在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.";
}
Path.GetExtension <(objFI [0] .Name)應該做什麼?就我而言,這只是一個錯字... –
你不需要此驗證的方式'if(objFI.Length> 0)' – Disappointed
這只是一個錯字,在參數之前有一個<錯誤。但是我想說_Path.GetFileNameWithoutExtension_可以簡化你的代碼。 – Steve