1

当遇到错误时,有人能告诉我这段代码会发生什么吗?理想情况下,它应该继续执行 foreach 语句,直到它到达最后一条记录,但我怀疑它在操作中间停止,因为当我检查移动的文件数时,它关闭了 225。如果它实际上是因为错误而停止,我该怎么做才能让它继续循环?

我正在为我们的软件创建一个新的上传管理器,并且需要清理旧文件。使用一年半后,大约有 715 个孤立文件相当于大约 750 MB,因为原始开发人员在上传新文件时没有编写代码来正确覆盖旧文件。他们还将文件保存在一个目录中。我无法忍受,所以我将所有文件移动到一个结构中 - 船只名称 - ServiceRequesetID - 为该服务上传的文件。我还为用户提供了一个 gridview 来查看和删除他们在使用服务时不再需要的文件。

受保护的无效Button1_Click(对象发送者,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.");
}
4

1 回答 1

0

只要遇到的错误发生在 try catch 子句中,代码就会在 foreach 循环中继续执行。但是,如果错误发生在 try catch 之外,函数将退出并抛出错误。您的错误日志报告了多少个文件?

于 2010-10-04T19:39:58.540 回答