[英]Unable to open sqlite database file when program launch at windows startup
我通過以下代碼將我的程序設置為在啟動時啟動:
Registry.CurrentUser
.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)
.SetValue(Application.ProductName, Application.ExecutablePath);
重新啟動 Windows 並登錄我的帳戶(具有管理員訪問權限)后,程序運行成功,但是當它嘗試連接到 sqlite 文件時,遇到以下異常:
"ClassName": "System.Data.DataException", "Message": "An exception occurred while initializing the database. See the InnerException for details.", "Data": null, "InnerException": { "ClassName": "System.Data.Entity.Core.EntityException", "Message": "The underlying provider failed on Open.", "Data": null, "InnerException": { "errorCode": 14, "ClassName": "System.Data.SQLite.SQLiteException", "Message": "unable to open database file", "Data": null, "InnerException": null, "HelpURL": null, "StackTraceString": " at System.Data.SQLite.SQLite3.Open(String strFilename, String vfsName, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, Int32 maxPoolSize, Boolean usePool) at System.Data.SQLite.SQLiteConnection.Open() at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<>c.<Open>b__13_0(DbConnection t, DbConnectionInterceptionContext c) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext) at System.Data.Entity.Core.EntityClient.EntityConnection.<Open>b__55_0() at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute(Action operation) at System.Data.Entity.Core.EntityClient.EntityConnection.Open()", "RemoteStackTraceString": null, "RemoteStackIndex": 0, "ExceptionMethod": "8\nOpen\nSystem.Data.SQLite, Version=1.0.113.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139\nSystem.Data.SQLite.SQLite3\nVoid Open(System.String, System.String, System.Data.SQLite.SQLiteConnectionFlags, System.Data.SQLite.SQLiteOpenFlagsEnum, Int32, Boolean)", "HResult": -2147481601, "Source": "System.Data.SQLite", "WatsonBuckets": null }, "HelpURL": null, "StackTraceString": " at System.Data.Entity.Core.EntityClient.EntityConnection.Open() at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions) at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass174_0.<ExecuteStoreCommand>b__0() at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation) at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreCommand(TransactionalBehavior transactionalBehavior, String commandText, Object[] parameters) at System.Data.Entity.Internal.InternalContext.ExecuteSqlCommand(TransactionalBehavior transactionalBehavior, String sql, Object[] parameters) at System.Data.Entity.Database.ExecuteSqlCommand(TransactionalBehavior transactionalBehavior, String sql, Object[] parameters) at SQLite.CodeFirst.SqliteDatabaseCreator.Create(Database db, DbModel model) at SQLite.CodeFirst.SqliteInitializerBase`1.InitializeDatabase(TContext context) at SQLite.CodeFirst.SqliteCreateDatabaseIfNotExists`1.InitializeDatabase(TContext context) at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass66_0`1.<CreateInitializationAction>b__0() at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)", "RemoteStackTraceString": null, "RemoteStackIndex": 0, "ExceptionMethod": "8\nOpen\nEntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\nSystem.Data.Entity.Core.EntityClient.EntityConnection\nVoid Open()", "HResult": -2146233087, "Source": "EntityFramework", "WatsonBuckets": null }, "HelpURL": null, "StackTraceString": " at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() at System.Data.Entity.Internal.LazyInternalContext.<>c.<InitializeDatabase>b__58_0(InternalContext c) at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() at System.Data.Entity.Internal.InternalContext.Initialize() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source) at Watcher.MainApplication..ctor() at Watcher.Program.Main()", "RemoteStackTraceString": null, "RemoteStackIndex": 0, "ExceptionMethod": "8\nPerformInitializationAction\nEntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\nSystem.Data.Entity.Internal.InternalContext\nVoid PerformInitializationAction(System.Action)", "HResult": -2146233087, "Source": "EntityFramework", "WatsonBuckets": null
當我手動運行程序時,這個問題就消失了
我嘗試使用文本文件而不是數據庫,但是由 Windows 運行的程序創建的文件與手動運行的程序創建的文件不同。
如何在 windows 10 上解決此問題?
當我在啟動的情況下重新啟動並運行時,當前目錄是系統目錄。
所以要解決這個問題,我需要使用完整路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.