a1204773 a1204773 - 10 days ago 6
C# Question

Get application directory using C# Console Application?

I found something on google but it not working on C# Console Application

What I found:

string appPath = Path.GetDirectoryName(Application.ExecutablePath);


How I can get application directory using c# Console Application?

Answer

If you still want to use Application.ExecutablePath in console application you need to:

  1. Add a reference to System.Windows.Forms namespace
  2. Add System.Windows.Forms to your usings section

    using System;
    using System.IO;
    using System.Windows.Forms;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string appDirectory = Path.GetDirectoryName(Application.ExecutablePath);
                Console.WriteLine(appDirectory);
            }
        }
    }
    

Also you can use Directory.GetCurrentDirectory() instead of Path.GetDirectoryName(Application.ExecutablePath) and thus you won't need a reference to System.Windows.Forms.

If you'd like not to include neither System.IO nor System.Windows.Forms namespaces then you should follow Reimeus's answer.

Comments