Home > Backend Development > C++ > How Can I Get the Current Assembly's Path in C#?

How Can I Get the Current Assembly's Path in C#?

Mary-Kate Olsen
Release: 2025-01-31 22:56:11
Original
692 people have browsed it

How Can I Get the Current Assembly's Path in C#?

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>
Copy after login

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>
Copy after login

CodeBase It is worth noting that the

attribute has been abandoned in .NET Core/.NET 5.
<code class="language-csharp">UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);</code>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template