程序在 windows 启动时启动时无法打开 sqlite 数据库文件
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 上解决这个问题?
我重启的时候运行在启动情况下,当前目录是系统目录。
所以要解决这个问题,我需要使用完整路径。
我通过以下代码将我的程序设置为在启动时启动:
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 上解决这个问题?
我重启的时候运行在启动情况下,当前目录是系统目录。
所以要解决这个问题,我需要使用完整路径。