2

I have a WPF-Application that I would like to start automatically if I start my Computer.

I have a window where a user can configure some settings for the application, one of the possible configuration options is a checkbox, that allows the user to dis- or enable the application to automatically start on the System-Startup.

This is how I set or delete the value in the Registry, depending on the users choice in the Checkbox:

try
        {
            var currentAssembly = Assembly.GetExecutingAssembly();
            var rkApp = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);

            if (settingsViewModel.AutostartEnabled)
            {
                rkApp.SetValue(currentAssembly.GetName().Name, currentAssembly.Location);
            }
            else
            {
                rkApp.DeleteValue(currentAssembly.GetName().Name, false);
            }
        }
        catch (Exception exception)
        {
        }

My Problem is, that even though the Application gets registered and can also be seen in the Autostart-Section within the Task-Manager, that I get the following error every time I restart my computer to check if the Appliction is started:

"You are attempting to open a file of type Application extension (.dll)"

So what am I doing wrong? Is there any way to avoid this error or to fix it? I already tried adding an application manifest file to my project to always start my Application as an Administrator. But that didn't seem to work either.

I'd appreciate any help.

FFranz
  • 98
  • 1
  • 10
  • What's the value of currentAssembly.Location? – Marc Sep 23 '20 at 14:04
  • currentAssembly.Location has the following value: "C:\\Users\\Username\\source\\repos\\AppName\\AppName\\AppNameCore\\bin\\Debug\\netcoreapp3.1\\AppName.dll" – FFranz Sep 23 '20 at 14:09

1 Answers1

2

Try to use System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName instead of currentAssembly.Location.

This should give you the path of the running executable. Assembly.GetEntryAssembly does not.

mm8
  • 163,881
  • 10
  • 57
  • 88