Aviv
Aviv

Reputation: 466

how to launch Console Application(!) on startup? C#

I've tried to create a Console Application program, and cause the application to run on startup. I found some solutions, but they uses external dll files (Of the operation system, in my case - Windows) or they refers to Windows Forms / WPF. I've realized that the startup code for Windows Forms application is pretty different than from a Console Application's startup code.. Can Someone help me? I really confused now..

Here's the code I found: (on this url: How to run a C# application at Windows startup?)

RegistryKey rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("MyAPP", Application.ExecutablePath.ToString());

How can I do this action for a console application, with code that will be similar to this one, and without using external dlls?

Thanks..

Upvotes: 2

Views: 5387

Answers (2)

D Stanley
D Stanley

Reputation: 152616

Application.ExecutablePath is only used by Windows apps. To get the executing assembly of any app, use Assembly.GetExecutingAssembly().Location:

RegistryKey rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("MyAPP", Assembly.GetExecutingAssembly().Location);

Upvotes: 4

Michael Gunter
Michael Gunter

Reputation: 12811

You've already got the right solution. The Registry classes in the .NET framework don't require WinForms or WPF references, so you're free to use them in a console application without littering your project with unnecessary references.

Upvotes: 2

Related Questions