How can I get the application's path in a .NET console application?

后端 未结 27 2120
甜味超标
甜味超标 2020-11-21 11:11

How do I find the application\'s path in a console application?

In Windows Forms, I can use Application.StartupPath to find the current path, but this d

27条回答
  •  佛祖请我去吃肉
    2020-11-21 11:51

    I didn't see anyone convert the LocalPath provided by .Net Core reflection into a usable System.IO path so here's my version.

    public static string GetApplicationRoot()
    {
       var exePath = new Uri(System.Reflection.
       Assembly.GetExecutingAssembly().CodeBase).LocalPath;
    
       return new FileInfo(exePath).DirectoryName;
           
    }
    

    This will return the full C:\\xxx\\xxx formatted path to where your code is.

提交回复
热议问题