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

后端 未结 27 1400
甜味超标
甜味超标 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:56

    System.Reflection.Assembly.GetExecutingAssembly().Location1

    Combine that with System.IO.Path.GetDirectoryName if all you want is the directory.

    1As per Mr.Mindor's comment:
    System.Reflection.Assembly.GetExecutingAssembly().Location returns where the executing assembly is currently located, which may or may not be where the assembly is located when not executing. In the case of shadow copying assemblies, you will get a path in a temp directory. System.Reflection.Assembly.GetExecutingAssembly().CodeBase will return the 'permanent' path of the assembly.

提交回复
热议问题