Question

Can someone tell me what is going to happen in this code when an error is encountered? Ideally it should continue the foreach statement until it gets to the last record, but I suspect it's stopping in the middle of the operation because when I check the number of files moved it's off by 225. If it is in fact stopping because of an error, what can I do to make it continue the loop?

I'm creating a new upload manager for our software and need to clean the old files up. There are about 715 orphaned files equaling around 750 MB after a year and a half of use because the original developers didn't write the code to correctly overwrite old files when a new one was uploaded. They also saved the files in a single directory. I can't stand that so I'm moving all of the files into a structure - Vessel Name - ServiceRequesetID - files uploaded for that service. I'm also giving the users a gridview to view and delete files they no longer need as they work the service.

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.");
}
Was it helpful?

Solution

As long as the error encountered occurs within the try catch clause, the code will continue executing within the foreach loop. However, if the error occurs outside of the try catch, the function will exit and throw an error. How many files does your error log report??

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top