有人可以告诉我在遇到错误时会在这段代码中发生什么?理想情况下,它应该继续foreach语句,直到它到达最后一条记录,但我怀疑它在操作过程中停止,因为当我检查文件的数量时它被关闭了225.如果它实际上因为错误而停止,我该怎么做才能让它继续循环?
我正在为我们的软件创建一个新的上传管理器,需要清理旧文件。在使用一年半之后,大约有715个孤立文件大约相当于750 MB,因为原始开发人员在上传新文件时没有编写代码来正确覆盖旧文件。他们还将文件保存在一个目录中。我无法忍受,所以我将所有文件移动到一个结构 - Vessel Name - ServiceRequesetID - 为该服务上传的文件。我还给用户一个gridview来查看和删除他们在服务时不再需要的文件。
protected void Button1_Click(object sender,EventArgs e) {
GridViewRow[] rowArray = new GridViewRow[gv_Files.Rows.Count];
gv_Files.Rows.CopyTo(rowArray, 0);
int i = -1;
foreach(GridViewRow row in rowArray)
{
i++;
string _serviceRequestID = ((Label)gv_Files.Rows[row.RowIndex].FindControl("lbl_SRID")).Text;
string _vesselName = ((Label)gv_Files.Rows[row.RowIndex].FindControl("lbl_VesselID")).Text;
string _uploadDIR = Server.MapPath("uploadedFiles");
string _vesselDIR = Server.MapPath("uploadedFiles" + "\\" + _vesselName);
string _fileName = ((Label)gv_Files.Rows[row.RowIndex].FindControl("lbl_FileName")).Text;
DirectoryInfo dInfo = new DirectoryInfo(_uploadDIR);
DirectoryInfo dVessel = new DirectoryInfo(_vesselDIR);
DirectoryInfo dSRID = new DirectoryInfo(_serviceRequestID);
dInfo.CreateSubdirectory(_vesselName);
dVessel.CreateSubdirectory(_serviceRequestID);
string _originalFile = _uploadDIR + "\\" + _fileName;
string _fileFullPath = Path.Combine(Server.MapPath("uploadedFiles/" + _vesselName + "/" + _serviceRequestID + "/"), _fileName);
FileInfo NewFile = new FileInfo(_fileFullPath);
string _fileUploadPath = _vesselName + "/" + _serviceRequestID + "/" + _fileName;
string sourceFile = _originalFile;
FileInfo _source = new FileInfo(sourceFile);
string destinationFile = _fileFullPath;
try
{
{
File.Move(sourceFile, destinationFile);
movefiles.InsertNewUploadPath(Convert.ToDecimal(_serviceRequestID), 1, _fileUploadPath);
}
}
catch (Exception ex)
{
CreateLogFiles Err = new CreateLogFiles();
Err.ErrorLog(Server.MapPath("Logs/ErrorLog"), ex.Message);
}
}
_utility.MessageBox("Completed processing files.");
}
答案 0 :(得分:0)
只要在try catch子句中遇到错误,代码就会在foreach循环中继续执行。但是,如果错误发生在try catch之外,则该函数将退出并抛出错误。您的错误日志报告了多少个文件?