Reputation: 1126
I set my program to launch at startup by the below code:
Registry.CurrentUser
.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)
.SetValue(Application.ProductName, Application.ExecutablePath);
After restarting Windows and login to my account (with admin access), the program runs successfully, but when it tries to connect to the sqlite file, it encounters the following exception:
"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
This problem disappears when I run the program manually
I tried to use a text file instead of a database, but a file created by a program run by Windows is different from a file created by a program run manually.
How can I solve this problem on windows 10?
Upvotes: 1
Views: 565
Reputation: 1126
When I restart and run in the startup case, the current directory is the systems directory.
So to fix this I need to use the full path.
Upvotes: 1