In the OnPaint event in winform, AppDomain.CurrentDomain.BaseDirectory gets the following path
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE
Application.ExecutablePath gets C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe
file:///C:/Users/clu/AppData/Local/Microsoft/VisualStudio/15.0_74da2886/ProjectAssemblies/us6sa4js01/Accor.Windows.Forms.dll
https://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-a-net-console-application
System.Reflection.Assembly.GetExecutingAssembly()
. 1Location
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.
https://stackoverflow.com/a/841320/3782855
AppDomain.CurrentDomain.BaseDirectory
Don’t use this. The BaseDirectory can be set at runtime. It is not guaranteed to be correct (like the accepted answer is).
entryAssembly.GetSatelliteAssembly
System.IO.FileNotFoundException: Could not find file ‘file:///C:/Users/clu/AppData/Local/Microsoft/VisualStudio/15.0_74da2886/ProjectAssemblies/dgz79z6i01/en-US/Accor.Windows.Forms.resources.DLL’.
File name: ‘file:///C:/Users/clu/AppData/Local/Microsoft/VisualStudio/15.0_74da2886/ProjectAssemblies/dgz79z6i01/en-US/Accor.Windows.Forms.resources.DLL’
How do I get the path of the assembly the code is in?
Use codebase is the most accurate
I’ve defined the following property as we use this often in unit testing.
public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
The Assembly.Location
property sometimes gives you some funny results when using NUnit (where assemblies run from a temporary folder), so I prefer to use CodeBase
which gives you the path in URI format, then UriBuild.UnescapeDataString
removes the File://
at the beginning, and GetDirectoryName
changes it to the normal windows format.
Similar Posts:
- [Solved] Cannot run program “git.exe”: CreateProcess error=
- Installation tutorial of visual studio 2017 and visual Assist X
- Unity2018: How to Solve C# Project Open Error
- [Solved] NPM downloads the dependency of the front-end Vue admin template master template Error
- wpf Project import System.Windows.Forms Error [How to Solve]
- Unity5.4.x Update to Unity2019 Error: -System.MissingMethodException
- Running NETCORE 2.2 program error [How to Fix]
- Event ID 455 ESENT error in Windows 10 V1909
- After installing nodejs, running NPM – V will cause (module. JS: 339 throw err..) error. Solution
- [Solved] Could not load file or assembly ‘System.Web.Http Fuslogvw.exe (Assembly Binding Log Viewer)