现在这似乎是一项相当简单的任务,但我还没有找到任何相关的回复。
我在文件夹中组织了大量文件,我需要将其导入数据库(不是文件本身作为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; }
DB Spec(ish)
SELECT [DocumentFileId]
,[MimeType]
,[FileName]
,[Active]
,[CreationDate]
,[CreationUser]
,[UpdateDate]
,[UpdateUser]
,[FilePath]
,[CategoryId]
,[SubCategoryId]
FROM [DocumentFile]
答案 0 :(得分:0)
如果您只需要db中的那几个字段,那么您不需要迭代所有文件夹。相反,您可以使用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());
...
}
答案 1 :(得分:0)
以下是基于答复的几乎完成的方法,如果有人将来也有类似的问题。
/// <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);
}
}
开启以优化类别检测...