1

I am creating a standalone database using LiteDB with C# code for storing every files metadata like filesize, absolute path ,createtime, extension etc. I am having problem in querying and iterating for the exact key value from the db file, please help out Thankyou.

This is my Insert query which is taking the absolute paths of every files from directories and passing the path as variable to my class for setting the file metadata values and returning the object with values to store in the LiteDB.

/INSERT QUERY/

public void Search(/* Drive paths */)

{

foreach(string file in Directory.GetFiles(path, "."))

{

try

{

using (var db = new LiteDatabase(/path of my DB file/))

{

var FileDBptr = db.GetCollection("FileDB");

var data = new FileInfoDB(file);

FileDBptr.Insert(data);

Console.WriteLine("SUCCESS INSERT");

}

}

catch (Exception e)

{

Console.WriteLine("COuld Not Create DB!!!!" + e);

}

} foreach (string subDir in Directory.GetDirectories(path))

{

try

{

Search(subDir);

}

catch

{

throw;

}

}

}

/* CLASS MODEL */

[Serializable]

public class FileInfoDB

{


    [BsonId]
    public ObjectId FileId { get; set; }
    public string FileName { get; set; }
    public string FilePath { get; set; }
    public string FileExtension { get; set; }
    public long FileSize { get; set; }
    public DateTime FileCreateTime { get; set; }
    public DateTime FileLastAccess { get; set; }
    public DateTime FileLastWrite { get; set; }

    public FileInfoDB(string path)
    {
        if (File.Exists(path))
        {
            try
            {
                FileInfo f = new FileInfo(path);
                FileId= ObjectId.NewObjectId();
                FileName = f.Name;
                FilePath = f.FullName;
                FileCreateTime = f.CreationTime;
                FileSize = f.Length;            //Size in bytes
                FileExtension = f.Extension;
                FileLastAccess = f.LastAccessTime;
                FileLastWrite = f.LastWriteTime;

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}

//QUERY CODE FOR RETRIEVAL

string fileName = "$ICN03Z0.txt";

try {

            using (var db = new LiteDatabase(_jsonpath))
            {   
                var FileDBptr = db.GetCollection<FileInfoDB>("FileDB");
                FileDBptr.EnsureIndex(x=>x.FileName);
                var data2 = FileDBptr.Find(Query.EQ("FileName", fileName));              
                int c = FileDBptr.Count();
                Console.WriteLine(c);                //Correct output
                if (data2 !=null)
                { 
                   foreach (var a in data2)         //Throwing an Exception 
                   {
                       Console.WriteLine(a.FileName);
                   }
                }                                          
            }

}

This is the data format which is stored in lite database file

{

"_id": {"$oid":"5c4ebee0f2e2d05814dcf865"},

"FileName": "$ICN03Z0.txt",

"FilePath": "C:\$Recycle.Bin\S-1-5-21-3439349906-2439027251-2956315770-1001\$ICN03Z0.txt",

"FileExtension":".txt",

"FileSize": {"$numberLong":"114"},

"FileCreateTime": {"$date":"2019-01-16T09:04:16.0810000Z"},

"FileLastAccess": {"$date":"2019-01-16T09:04:16.0810000Z"},

"FileLastWrite": {"$date":"2019-01-16T09:04:16.0810000Z"}

}

I expect to search the value according to filename first and then extract all the other key value pairs data of same file. I tried with different query using LINQ as mentioned in GitHub but always throwing exception. Also have verified the data stored inside Lite database using LiteDB shell its inserting as the format above, but retrieval is giving problem.

Thanks in Advance!

0 Answers0