操作员'<';不能应用于类型为';方法组';和';字符串';在c#中
本文关键字:字符串 类型 不能 lt 应用于 操作员 方法 | 更新日期: 2023-09-27 17:58:33
我在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.";
}
()
之间有<
替换此:
string sFileExt1 = Path.GetExtension < (objFI[0].Name);
带有:
string sFileExt1 = Path.GetExtension(objFI[0].Name);