简体   繁体   中英

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. However, if I open it as Administrator (right clicking the icon in the start menu after searching its name) the application opens without closing but it does not show the information it is supposed to show, only shows the builtin data.

This aplpication data/information is read from a XML file located in a subfolder on the executable location. In the code I'm accesing this files through relative path:

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;

Also I'm using serilog for logging messages but in any of the both cases above, nothing is written in the log file. I'm referencing the log file path as a relative path:

private static string LogFile { get; set; } = @".\Logs\Log.log";

When I open the app directly with executable or through a shortcut created on the Desktop, the application works perfectly fine. Read and write data and also registering logs in files.

The application is published as 'self-contained' as described in Microsoft documentation

I don't know what is really causing this behaviour.

What happens right now is you reading files from the "working directory" ( Environment.CurrentDirectory ). When you run your app from the Start menu that working directory won't match your app location.

Instead retrieve your app's location directly and use it whenever you need to access any such files:

var appDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var fullPath = Path.Combine(appDir, @"Logs\Log.log"); // etc

Here appDir will be the full path of the directory where your *.exe file is located.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM