I'm developing a Windows Forms app using SQLite as a local storage for program data and user settings. In fact, I'm using SQLiteCodeFirst to connect to my SQLite file with Entity Framework. Everything works fine when I start my program from Visual Studio/the folder, but I get a System.Data.DataException
when the program is set to fire from startup.
The exception messages given are:
- An exception occurred while initializing the database. See the InnerException for details.
- Inner exception: The underlying provider failed on Open.
- Inner inner exception: unable to open database file
I have no good clue why this exception throws only on Windows startup, but not when starting the program from the folder itself. The code I use to fire the program from startup is as follows:
using (var rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", RegistryKeyPermissionCheck.ReadWriteSubTree))
{
// set to init program on Windows startup
rk.SetValue(Program.AppName, Application.ExecutablePath.ToString());
// http://stackoverflow.com/questions/2151074/setting-registry-key-write-permissions-using-net
// Creating registry access rule for 'Everyone' NT account
SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
NTAccount account = sid.Translate(typeof(NTAccount)) as NTAccount;
RegistrySecurity rs = rk.GetAccessControl();
var rar = new RegistryAccessRule(
account.ToString(),
RegistryRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
rs.AddAccessRule(rar);
rk.SetAccessControl(rs);
}
If anyone has a good clue, I'd appreciate much. Been fumbling around with this problem for a while. Thank you!