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

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

    You have two options for finding the directory of the application, which you choose will depend on your purpose.

    // to get the location the assembly is executing from
    //(not necessarily where the it normally resides on disk)
    // in the case of the using shadow copies, for instance in NUnit tests, 
    // this will be in a temp directory.
    string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
    
    //To get the location the assembly normally resides on disk or the install directory
    string path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
    
    //once you have the path you get the directory with:
    var directory = System.IO.Path.GetDirectoryName(path);
    

提交回复
热议问题