Is there a way to get the path to the assembly where the current code resides?I don't want to call the path of the assembly, just the path that contains the code.
Basically, my single meta-test needs to read some xml test files relative to the dll.I want this path to always resolve correctly, regardless of whether the test DLL is run from TestDriven.NET, MbUnit GUI, or another version.
Editor: People seem to have misunderstood what I'm asking.
My test library is located in
C: \\ projects \\ myapplication \\ daotests \\ bin \\ Debug \\ daotests.dll
I want to take this road:
C: \\ projects \\ myapplication \\ daotests \\ bin \\ Debug \\
When I was running from MbUnit Gui, so far, these three suggestions have disappointed me:
Environment.CurrentDirectory gives c:\Program Files\MbUnit
System.Reflection.Assembly.GetAssembly (typeof (DaoTests). Location gives C:\Documents and Settings \\george \Local Settings \\Temp \\.... \\DaoTests.dll
System.Reflection.Assembly.GetExecutingAssembly().Location is the same as the previous one.
#1st floor
This should work:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); Assembly asm = Assembly.GetCallingAssembly(); String path = Path.GetDirectoryName(new Uri(asm.EscapedCodeBase).LocalPath); string strLog4NetConfigPath = System.IO.Path.Combine(path, "log4net.config");
I'm using it to deploy the DLL file library and some configuration files (this is using log4net from the DLL file).
#2nd floor
That's what I came up with.Conduct unit tests (nunit and resharper test runners) between Web projects; I find this useful to me.
I've been looking for code to detect the internal version of the configuration, Debug/Release/CustomName.las, #if DEBUG.So if someone can improve it!
Edit and improve at any time.
Getting the application folder.Useful for the Web root directory, unittests is used to get the folder for the test files.
public static string AppPath { get { DirectoryInfo appPath = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); while (appPath.FullName.Contains(@"\bin\", StringComparison.CurrentCultureIgnoreCase) || appPath.FullName.EndsWith(@"\bin", StringComparison.CurrentCultureIgnoreCase)) { appPath = appPath.Parent; } return appPath.FullName; } }
Get the bin folder: Useful for executing assemblies using reflection.If the file is copied there because of building attributes.
public static string BinPath { get { string binPath = AppDomain.CurrentDomain.BaseDirectory; if (!binPath.Contains(@"\bin\", StringComparison.CurrentCultureIgnoreCase) && !binPath.EndsWith(@"\bin", StringComparison.CurrentCultureIgnoreCase)) { binPath = Path.Combine(binPath, "bin"); //-- Please improve this if there is a better way //-- Also note that apps like webapps do not have a debug or release folder. So we would just return bin. #if DEBUG if (Directory.Exists(Path.Combine(binPath, "Debug"))) binPath = Path.Combine(binPath, "Debug"); #else if (Directory.Exists(Path.Combine(binPath, "Release"))) binPath = Path.Combine(binPath, "Release"); #endif } return binPath; } }
#3rd floor
As far as I know, most of the other answers have some questions.
For those based on Disk (not Web-based), non-GACed assembly The correct way to do this is to use the CodeBase property of the assembly that is currently executing.
This will return a URL (file://).Don't mess up String Operation or UnescapeDataString , you can use the ocalPath property of Uri to convert with minimal hassle.
var codeBaseUrl = Assembly.GetExecutingAssembly().CodeBase; var filePathToCodeBase = new Uri(codeBaseUrl).LocalPath; var directoryPath = Path.GetDirectoryName(filePathToCodeBase);
#4th floor
Web application?
Server.MapPath("~/MyDir/MyFile.ext")
#5th floor
Same as John's answer, but with a slightly lengthy extension.
public static string GetDirectoryPath(this Assembly assembly) { string filePath = new Uri(assembly.CodeBase).LocalPath; return Path.GetDirectoryName(filePath); }
Now you can do the following:
var localDir = Assembly.GetExecutingAssembly().GetDirectoryPath();
Or, if you like:
var localDir = typeof(DaoTests).Assembly.GetDirectoryPath();