Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the Application object from a console app?

I took some code from my C# Windows form app which uses the Application object to get the start up path and tried to use it in a console app. When I did this, the compiler displayed the following error msg - "The name 'Applicaiton' does not exist in the current context. Did some research and discovered that the Application object is in System.Windows.Forms namespace. So, I added a reference to this with no apparent effect. Then, tried adding using System.Windows.Forms to the top of the file and got a new error message - "The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)." Can anyone shed some light on how to access the Application object from a console app?

like image 219
Bob Bryan Avatar asked Jan 20 '23 12:01

Bob Bryan


2 Answers

System.Reflection.Assembly.GetExecutingAssembly().Location

Also, of directory needed, wrap the call in System.IO.Path.GetDirectory()

like image 163
Denis Biondic Avatar answered Jan 22 '23 01:01

Denis Biondic


Application is not available for Console Applications, it's for windows forms. You can use

Assembly.GetExecutingAssembly().CodeBase
like image 36
Waleed Avatar answered Jan 22 '23 01:01

Waleed