简体   繁体   English

LiteDB - 未经授权的访问异常

[英]LiteDB - Unauthorized Access Exception

I am trying to make LiteDB work - only to be halted by this exception when trying to add a new entry.我正在尝试使 LiteDB 工作 - 只是在尝试添加新条目时被此异常停止。

System.UnauthorizedAccessException: Unauthorized Access to Path 'F:\ProjectName\bin\Debug\Data\2021'. System.UnauthorizedAccessException:未经授权访问路径“F:\ProjectName\bin\Debug\Data\2021”。

    public bool AddEntry(TplMainEntry Template)
    {
        // Find a new index for the new entry.
        LiteCollection<TplMainEntry> MainDBCollection = Entries.GetCollection<TplMainEntry>("MainEntries");

        MainDBCollection .EnsureIndex(x => x.iID); // EXCEPTION THROWN HERE!!!!

        var vField = MainDBCollection .FindOne(Query.All("iID", 1));
        // If no index is found, then Database must be new. Use index 1.
        // Else, get max index from the collection - then +1 it.
        if (vField == null)
        {
            Template.iID = 1;
            Template.iIndex = 1;
        }
        else
        {
            int iMaxID = int.Parse(MainDBCollection .Max().ToString());
            Template.iID = iMaxID + 1;
            Template.iIndex = Template.iID;
        }
        Entries.Mapper.Entity<TplMainEntry>().Id(x => x.iID);

        // Insert new entry, then update collection.
        Collection.Insert(Template);
        return Collection.Update(Template);
    }

LiteDB is initialized by Form constructor. LiteDB 由 Form 构造函数初始化。

        sDBPath = sAppPath + "\\Data\\" + DateTime.Now.Year.ToString() + "\\";
        if (!Directory.Exists(sDBPath)) Directory.CreateDirectory(sDBPath);
        MainDatabase = new LiteDBClass(sDBPath);
        public LiteDBClass(string sPath)
        {
            try
            {
                Entries = new LiteDatabase(sPath);
                Collection = Entries.GetCollection<TplMainEntry>("MainEntries");
            }
            catch (DirectoryNotFoundException cExc)
            {
                // Bla
            }
            catch (LiteException cExc)
            {
                // Bla bla
            }
            catch (Exception cExc)
            {
                //Bla bla bla
            }
        }

I am already running VS with admin priviliges and I have set the requestedExecutionLevel to Admin on the app manifest.我已经在使用管理员权限运行 VS,并且在应用程序清单上将 requestedExecutionLevel 设置为 Admin。 What am I missing????我错过了什么????

 public LiteDBClass(string sPath)
        {
            try
            {
                // Needs a full path + file name.
                Entries = new LiteDatabase(sPath + "\\" + sDatabaseName);
                Collection = Entries.GetCollection<TplMainEntry>("MainEntries");
            }
            catch (DirectoryNotFoundException cExc)
            {
                // Bla
            }
            catch (LiteException cExc)
            {
                // Bla bla
            }
            catch (Exception cExc)
            {
                //Bla bla bla
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM