[英]WPF application crashes when it's opened from windows start menu
When a try to open a WPF application developed in.Net Core 3.1 with c# through the Window start menu by searching its name, the application opens, it shows the main window but then closes after a few seconds. 但是,如果我以管理員身份打開它(在搜索其名稱后右鍵單擊開始菜單中的圖標),應用程序會打開而不關閉,但它不會顯示它應該顯示的信息,只顯示內置數據。
該應用數據/信息是從位於可執行位置子文件夾中的 XML 文件中讀取的。 在代碼中,我通過相對路徑訪問這些文件:
internal const string PERSISTENT_FOLDER = @".\data\";
internal const string PERSISTENT_DATA_FILE = "data.xml";
internal const string PERSISTENT_DATA_FILE_PATH = PERSISTENT_FOLDER + PERSISTENT_DATA_FILE;
此外,我使用 serilog 來記錄消息,但在上述兩種情況下,日志文件中都沒有寫入任何內容。 我將日志文件路徑引用為相對路徑:
private static string LogFile { get; set; } = @".\Logs\Log.log";
當我直接使用可執行文件或通過在桌面上創建的快捷方式打開應用程序時,應用程序運行良好。 讀取和寫入數據以及在文件中注冊日志。
如Microsoft 文檔中所述,該應用程序作為“獨立”發布
我不知道是什么真正導致了這種行為。
現在發生的是您從“工作目錄”( Environment.CurrentDirectory
)讀取文件。 當您從“開始”菜單運行您的應用程序時,該工作目錄將與您的應用程序位置不匹配。
而是直接檢索您的應用程序的位置,並在您需要訪問任何此類文件時使用它:
var appDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var fullPath = Path.Combine(appDir, @"Logs\Log.log"); // etc
這里appDir
將是您的*.exe
文件所在目錄的完整路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.