MVC C# 在服务器上查找与名称 if 条件对应的文件

发布于 2024-10-11 09:18:55 字数 1872 浏览 1 评论 0原文

试图掌握已经传递给我的系统代码。它是用 asp.net C# MVC 编写的。

该系统的一个部分在服务器上的文件夹中查找文件,并将它们导入到数据库中。文件的详细信息保存在 Excel 文件中,该文件也位于服务器上的同一文件夹中。

目前在Excel工作表中,如果PDFName列下有一个值,系统将查找与该名称对应的文件。如果没有匹配的文件,系统将不会导入数据。

我需要做的是,它会更改它,以便如果有文件,它将导入该文件及其数据,但是如果没有文件,它将导入数据,以便稍后可以添加该文件日期。下面是查找相应文件的代码片段。

        if (!string.IsNullOrEmpty(fileRow.PDFName))
        {
            try
            {
                FileInfo matchingFile = files.First(df => (df.Extension.ToLower() == ".pdf" && df.Name.Replace(df.Extension, "").ToLower() == fileRow.PDFName));

                FTPFileEntry f = new FTPFileEntry()
                {
                    FileExtension = matchingFile.Extension,
                    FileName = matchingFile.Name,
                    Name = titleRow.PDFName,
                    Type = matchingFile.Extension.Trim('.')
                };

                fileRow.FileEntry.Add(f);
            }
            catch (InvalidOperationException)
            {

            }
        }

我只是有点不确定如果没有匹配的文件,如何在这种情况下进行构建,做其他事情。我在想:

if (!string.IsNullOrEmpty(fileRow.PDFName))
        { 


            FileInfo matchingFile = files.First(df => (df.Extension.ToLower() == ".pdf" && df.Name.Replace(df.Extension, "").ToLower() == fileRow.PDFName));


            if (matchingFile != null)
            {
                FTPFileEntry f = new FTPFileEntry()
                {
                    FileExtension = matchingFile.Extension,
                    FileName = matchingFile.Name,
                    Name = titleRow.PDFName,
                    Type = matchingFile.Extension.Trim('.')
                };

                fileRow.FileEntry.Add(f);

            }
            else
            {
                //Add data to database
            }
        }

我只是想知道我的方向是否正确?任何指示将不胜感激:)

trying to get to grips with the code of a system that has been passed on to me. It has been written in asp.net C# MVC.

One section of this system locates files in a folder on the server, and imports them to a database. The details for the files are kept in an excel file which is also in the same folder on the server.

Currently in the excel sheet, if there is a value under the column PDFName, the system will look for a file corresponding to that name. If there is no matching file, the system will not import the data.

What I need to do, it change it so that if there is a file, it will import the file with it's data, however if there is not a file, it will import the data so that just the file can be added at a later date. Below is the snippet from the code where it looks for the corresponding file.

        if (!string.IsNullOrEmpty(fileRow.PDFName))
        {
            try
            {
                FileInfo matchingFile = files.First(df => (df.Extension.ToLower() == ".pdf" && df.Name.Replace(df.Extension, "").ToLower() == fileRow.PDFName));

                FTPFileEntry f = new FTPFileEntry()
                {
                    FileExtension = matchingFile.Extension,
                    FileName = matchingFile.Name,
                    Name = titleRow.PDFName,
                    Type = matchingFile.Extension.Trim('.')
                };

                fileRow.FileEntry.Add(f);
            }
            catch (InvalidOperationException)
            {

            }
        }

I'm just a bit unsure as to how to go about building in the condition if there is no matching file, do something else. I was thinking something like:

if (!string.IsNullOrEmpty(fileRow.PDFName))
        { 


            FileInfo matchingFile = files.First(df => (df.Extension.ToLower() == ".pdf" && df.Name.Replace(df.Extension, "").ToLower() == fileRow.PDFName));


            if (matchingFile != null)
            {
                FTPFileEntry f = new FTPFileEntry()
                {
                    FileExtension = matchingFile.Extension,
                    FileName = matchingFile.Name,
                    Name = titleRow.PDFName,
                    Type = matchingFile.Extension.Trim('.')
                };

                fileRow.FileEntry.Add(f);

            }
            else
            {
                //Add data to database
            }
        }

I was just wondering if I'm going in the right direction with this? Any pointers would be appreciated :)

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

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

发布评论

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

评论(1

苍风燃霜 2024-10-18 09:18:55

对我来说看起来不错。
您还可以使用第一个代码片段并在 catch 部分将数据添加到数据库,但是捕获异常会降低性能(虽然不是很多,但仍然......)

It looks fine to me.
You could also use the first snippet of code and add data to the database in the catch section, but catching the exception would reduce the performances (not by much, but nonetheless...)

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