How to get the path of the current assembly of C#?
In many cases, it is necessary to determine the path that contains the assembly that is executed. This is particularly important when using the need to access the DLL resource.
Solution:
To retrieve the path of the current assembly, you can use two alternative methods: <.> 1. Use
:
This method returns the complete path of the assembly, including file name. However, in some cases, it may provide unexpected results, especially when using the test framework such as Nunit. Assembly.Location
<.> 2. Use
<code class="language-csharp">string path = System.Reflection.Assembly.GetExecutingAssembly().Location;</code>
The attribute contains the URI format path of the assembly. To convert it to a conventional Windows path, please execute the following steps:
Assembly.CodeBase
Additional description:
<code class="language-csharp">string codeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;</code>
CodeBase
It is worth noting that the
<code class="language-csharp">UriBuilder uri = new UriBuilder(codeBase); string path = Uri.UnescapeDataString(uri.Path);</code>
When running testing from Mbunit GUI, attributes may not accurately reflect the path of the assembly.
The above is the detailed content of How Can I Get the Current Assembly's Path in C#?. For more information, please follow other related articles on the PHP Chinese website!