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

后端 未结 27 1415
甜味超标
甜味超标 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 12:00

    Following line will give you an application path:

    var applicationPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)
    

    Above solution is working properly in the following situations:

    • simple app
    • in another domain where Assembly.GetEntryAssembly() would return null
    • DLL is loaded from Embedded resources as a byte array and loaded to AppDomain as Assembly.Load(byteArrayOfEmbeddedDll)
    • with Mono's mkbundle bundles (no other methods work)

提交回复
热议问题