What's the best way to get the directory from which an assembly is executing

前端 未结 7 1414
说谎
说谎 2020-12-08 21:40

For my apps, I store some configuration file in xml along with the assembly(exe), and something other temporary files for proccessing purpose.

I found some quirk w

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 22:17

    Try the implementation below. The CodeBase property is the most reliable way to get the location, and then the rest of the code converts it into standard Windows format (instead of a URI).

    static public string AssemblyLoadDirectory
    {
        get
        {
            string codeBase = Assembly.GetCallingAssembly().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);
            return Path.GetDirectoryName(path);
        }
    }
    

提交回复
热议问题