Best way to get application folder path

前端 未结 10 2194
無奈伤痛
無奈伤痛 2020-11-22 02:03

I see that there are some ways to get the application folder path:

  1. Application.StartupPath
  2. System.IO.Path.GetDirectoryName( System.
10条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 02:35

    In my experience, the best way is a combination of these.

    1. System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase Will give you the bin folder
    2. Directory.GetCurrentDirectory() Works fine on .Net Core but not .Net and will give you the root directory of the project
    3. System.AppContext.BaseDirectory and AppDomain.CurrentDomain.BaseDirectory Works fine in .Net but not .Net core and will give you the root directory of the project

    In a class library that is supposed to target.Net and .Net core I check which framework is hosting the library and pick one or the other.

提交回复
热议问题