簡體   English   中英

SQlite 在 windows 啟動時從 c# 中的錯誤文件夾加載數據庫

[英]SQlite loads database from the wrong folder in c# on windows startup

當應用程序在 windows 啟動時啟動時出現此錯誤,但是當我在 windows 運行時打開它時應用程序運行完美。

SQLite error (14): cannot open file at line 47640 of [b0c4230c89]
Exception thrown: 'System.Data.SQLite.SQLiteException' in System.Data.SQLite.dll
SQLite error (14): os_win.c:47639: (2) winOpen(C:\WINDOWS\System32\qubanga.db) - The system cannot find the file specified

這是程序用於檢索數據庫的 sqlite 數據庫連接。

 try
            {
               SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=database.db; Version = 3; New = True; Compress = True; ");
                sqlite_conn.Open();



                SQLiteCommand sqlite_cmd;
                
                string createTable = "Insert into " + table + "(" + columns + ") Values("+values+");";

                sqlite_cmd = sqlite_conn.CreateCommand();
                sqlite_cmd.CommandText = createTable;
                var query = sqlite_cmd.ExecuteNonQuery();
              
                if (query == 1)
                    result = true;

            }

為程序設置啟動

 public  static void  SetStartup()
        {
            RegistryKey rk = Registry.CurrentUser.OpenSubKey
                ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

           
                rk.SetValue("AppName", Application.ExecutablePath);
           

        }

如果您的應用程序與 System32 文件夾(我希望它是)位於不同的文件夾中,您的代碼會將注冊表項設置為該文件夾路徑。 運行 regedit 並導航到該鍵進行檢查。 因此,在啟動時 Windows 將查找 db 的 Application 文件夾,而不是 System32 文件夾。 嘗試將其設置為 System32 文件夾。

rk.SetValue("AppName", Environment.GetFolderPath(Environment.SpecialFolder.System) + "qubanga.db" );

因此,在使用上面提供的解決方案解決問題后,我意識到 Windows 啟動的程序是從C:\WINDOWS\System32\而不是安裝位置執行的。 我通過更改應用程序的工作目錄解決了這些問題。我從@Caius 得到了這個想法

public static void SetWorkingDirectory()
        {
            string excePath = AppDomain.CurrentDomain.BaseDirectory;

            Directory.SetCurrentDirectory(excePath);
                //Console.WriteLine(Directory.GetCurrentDirectory());



        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM