将文件夹作为类别 ASP.NET C# 的文件导入数据库

Importing files to database with folders as categories ASP.NET C#

现在这似乎是一个相当微不足道的任务,但我还没有找到任何相关的回复。

我在文件夹中组织了大量文件,我需要将其导入数据库(不是文件本身作为 varbinary,只是路径和相关文件数据),包括应该从中推断出的类别和子类别文件所在的文件夹和子文件夹:

\OBJECT ID\CATEGORY\SUBCATEGORY\FILE

我不想提前提供答案 - 我想知道是否有一种简单的方法可以做到这一点,或者我是否必须在扫描内部文件时迭代所有保留变量的文件夹说文件夹等等等等

一些代码:

public void ImportFileSystemToDatabase()
    {
        string fileImportFolder = ConfigurationManager.AppSettings["FileImportPath"];

        DirectoryInfo dirInfo = new DirectoryInfo(fileImportFolder);

        var folders = dirInfo.GetDirectories();
        var files = dirInfo.GetFiles();

        var fsItems = new List<FileAttachment>();

        foreach (var folder in folders)
            fsItems.Add(new FileAttachment(folder));

        foreach (var file in files)
            if (file.Extension != ".db")
                fsItems.Add(new FileAttachment(file));

    }

FileAttachment.cs:

public class FileAttachment
{
    public FileAttachment(FileInfo file)
    {
        this.Name = file.Name;
        this.FullName = file.FullName;
        this.Size = file.Length;
        this.CreationTime = file.CreationTime;
        this.LastAccessTime = file.LastAccessTime;
        this.LastWriteTime = file.LastWriteTime;
        this.IsFolder = false;
    }

    public FileAttachment(DirectoryInfo folder)
    {
        this.Name = folder.Name;
        this.FullName = folder.FullName;
        this.Size = null;
        this.CreationTime = folder.CreationTime;
        this.LastAccessTime = folder.LastAccessTime;
        this.LastWriteTime = folder.LastWriteTime;
        this.IsFolder = true;
    }

    public string Name { get; set; }
    public string FullName { get; set; }
    public long? Size { get; set; }
    public DateTime CreationTime { get; set; }
    public DateTime LastAccessTime { get; set; }
    public DateTime LastWriteTime { get; set; }
    public bool IsFolder { get; set; }
    public int CategoryId { get; set; }
    public int AdjudicatedRealEstateId { get; set; }

数据库规范(大概)

SELECT [DocumentFileId]
  ,[MimeType]
  ,[FileName]
  ,[Active]
  ,[CreationDate]
  ,[CreationUser]
  ,[UpdateDate]
  ,[UpdateUser]
  ,[FilePath]
  ,[CategoryId]
  ,[SubCategoryId]
  FROM [DocumentFile]

如果您只需要数据库中的那几个字段,那么您不需要遍历所有文件夹。相反,您可以使用 Directory.GetFiles() 获取所有子文件夹中特定文件的列表(例如,仅 *.docx 等)

var ff = Directory.GetFiles(fileImportFolder, "*", SearchOption.AllDirectories);
foreach (var f in ff)
{
    // f is a full path of the file
    Response.Write(f.ToString());

    // use FileInfo to get specific attributes   
    var i = new FileInfo(f);
    Response.Write("SubCategoryId=" + i.Directory.Name);
    Response.Write("CategoryId=" + i.Directory.Parent.Name);
    Response.Write("UpdateDate=" + i.LastWriteTime.ToString());
    ...
}

这里是根据给出的回复差不多完成的方法,以防以后有类似问题的人。

    /// <summary>
    /// Import files from filesystem to the database
    /// </summary>
    /// <remarks>This is a heavy one-time operation. If used on a job, it should run off-peak</remarks>
    public void ImportFileSystemToDatabase()
    {
        string fileImportFolder = ConfigurationManager.AppSettings["FileImportPath"];
        string fileStorage = ConfigurationManager.AppSettings["FileStoragePath"];

        var filesToImport = Directory.GetFiles(fileImportFolder, "*", SearchOption.AllDirectories);

        foreach (var filePath in filesToImport)
        {
            var file = new FileInfo(filePath);

            string[] treePath = filePath.Replace(fileImportFolder, "").Split('\');

            DocumentFile newDocument = new DocumentFile
            {
                MimeType = file.Extension,
                FileName = file.Name,
                Active = true,
                CreationDate = DateTime.Now,
                CreationUser = "IMPORT",
                FilePath = fileStorage + "\" + file.Name
            };

            //Lets contextualize the document to the object Id
            int adjReId = Convert.ToInt32(treePath[1]);
            //Context goes here

            if (treePath.Count() == 4)
            {
                //Lets set the Category
                newDocument.CategoryId = CreateDocumentCategory(treePath[2], (int)LookupTypes.DocumentCategories);
            }

            if (treePath.Count() > 4)
            {
                //Lets set the Category
                newDocument.CategoryId = CreateDocumentCategory(treePath[2], (int)LookupTypes.DocumentCategories);
                //Lets set the SubCategory
                newDocument.CategoryId = CreateDocumentCategory(treePath[3], (int)LookupTypes.DocumentSubCategories);
            }

            file.CopyTo(fileStorage + file.Name);

            InsertDocumentFile(newDocument);
        }
    }

现在开始优化类别检测...