使用 ASP.NET 和 C# 创建新的目录结构并移动文件

发布于 2024-09-26 12:48:10 字数 2359 浏览 2 评论 0原文

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

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

protected void 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.");
}

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.");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀里藏娇 2024-10-03 12:48:10

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

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??

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文