How can I convert Assembly.CodeBase into a filesystem path in C?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Converting Assembly.CodeBase to a Filesystem Path in C#
When working with .NET assemblies, you sometimes need to determine where an assembly file physically lives on disk. The Assembly.CodeBase property provides this information, but it returns a URI string (like file:///C:/Projects/MyApp/bin/MyLibrary.dll) rather than a standard filesystem path. This article explains how to properly convert that URI into a usable path and when you should consider alternatives.
Note that Assembly.CodeBase was deprecated in .NET 5 and later. If you are targeting modern .NET, the Assembly.Location property is the preferred approach. However, many codebases still target .NET Framework or need to support older runtimes where CodeBase remains relevant.
Why CodeBase Returns a URI
The CodeBase property is part of System.Reflection and was designed to represent the original location from which an assembly was loaded. Because assemblies can be loaded from network shares, HTTP endpoints, or local paths, the property uses a URI format to accommodate all these sources. A local file path becomes file:///C:/path/to/assembly.dll, while a network path might look like file://server/share/assembly.dll.
The problem is that most file system APIs expect standard paths like C:\path\to\assembly.dll, not URI-formatted strings. Simply stripping the file:/// prefix with string manipulation is fragile and breaks on UNC paths, encoded characters, and cross-platform scenarios.
The Correct Conversion Using the Uri Class
The safest way to convert CodeBase to a filesystem path is through the System.Uri class, which handles percent-encoding, UNC paths, and platform differences:
The Uri.LocalPath property converts the URI to a proper OS-specific filesystem path. It handles percent-encoded characters (like %20 for spaces) and produces the correct format for the current platform.
Handling Edge Cases
Paths with Spaces
File paths containing spaces are percent-encoded in URIs. For example, C:\My Projects\App\lib.dll becomes file:///C:/My%20Projects/App/lib.dll. The Uri.LocalPath property decodes these automatically:
UNC Network Paths
Network paths require special attention. A UNC path like \\server\share\lib.dll is represented as file://server/share/lib.dll in URI form. The Uri class handles this correctly:
Special Characters in Paths
Paths containing characters like # or ? can cause issues because these characters have special meaning in URIs. If your assembly path includes these characters, you may need additional handling:
CodeBase vs Location: When to Use Which
The Assembly.Location property always returns a filesystem path (or an empty string for dynamic assemblies). It is simpler and more reliable for most use cases:
Use CodeBase only when you specifically need the original load URL and are targeting .NET Framework. Here is a comparison:
In shadow-copy scenarios (common in ASP.NET Framework), Location points to the shadow copy directory while CodeBase points to the original file. This distinction matters when you need to find configuration files or resources that sit alongside the original assembly.
Common Pitfalls
- String manipulation instead of Uri parsing. Removing
file:///manually withReplaceorSubstringbreaks on UNC paths, encoded characters, and Linux paths. Always use theUriclass. - Ignoring the deprecation warning. In .NET 5+,
CodeBasethrowsPlatformNotSupportedExceptionfor bundled (single-file) applications. Guard your code with a try-catch or check the runtime version before accessing it. - Assuming CodeBase is never null. Dynamically generated assemblies and in-memory assemblies may return null for
CodeBase. Always check for null before conversion. - Cross-platform path separators. On Linux and macOS, paths use forward slashes. The
Uri.LocalPathproperty handles this, but hardcoding backslashes in subsequent path operations will fail on non-Windows platforms. - Fragment identifiers in URIs. If a path contains
#, theUriclass interprets everything after it as a fragment. UseUri.UnescapeDataString(uri.AbsolutePath)as a fallback for these edge cases.
Summary
Converting Assembly.CodeBase to a filesystem path requires the System.Uri class to properly handle percent-encoding, UNC paths, and platform differences. The pattern is straightforward: construct a Uri from the CodeBase string and read its LocalPath property. For modern .NET (5+), prefer Assembly.Location since CodeBase is deprecated and unsupported in single-file deployments. When you do need CodeBase, always parse it through Uri rather than using string manipulation, and handle edge cases like null values and special characters in paths.

