0

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!

matt
  • 2,857
  • 7
  • 33
  • 58
  • Been a while since I used SQLite, but IIRC it's pretty picky on running architecture (x86 vs x64). I don't recall this error message in particular (I never used an EF provider with it), but maybe that's something you can give a quick check to? (also, just to confirm, you have verified the path is correct to the db file during runtime?) – jleach Jul 12 '16 at 04:17
  • @jdl134679 I've tried building with x86 but it throws the same exception when fired on startup... I think the database file path should be correct (my connection string in app.config is "Data Source=.\data.sqlite"), it gives no exception when I double-click on it – matt Jul 12 '16 at 06:41

0 Answers0