Share via

GetDirectoryName('')" cannot be evaluated. The path is not of a legal form.

Pieter Claassens 45 Reputation points
2026-01-07T07:56:12.91+00:00

Good day, trying to compile my Windows Forms app - getting the error in the subject field...

Thx

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


2 answers

Sort by: Most helpful
  1. Varsha Dundigalla(INFOSYS LIMITED) 4,785 Reputation points Microsoft External Staff
    2026-01-07T11:49:21.8333333+00:00

    Thank you for reaching out.

    This happens because the project is accidentally targeting a Windows SDK version that ends with “.1” (for example 10.0.19041.1). In .NET 10, those “.1” Windows target versions route the build to a CsWinRT 3.x (preview / not ready) path. When that preview path isn’t available (or the targeting pack can’t be resolved), the build task ends up with an empty WinRT runtime reference path. Then MSBuild tries to compute a folder using GetDirectoryName(''), and .NET throws “The path is not of a legal form” because an empty string is not a valid path. That’s why you see MSB4184 pointing into Microsoft.NET.Windows.targets under C:\Program Files\dotnet\sdk\...\ (it’s the SDK targets file reporting the failure, not your code).

    The “Permission denied / Save file as under C:\Program Files\dotnet…” pop‑up is a side effect of the same situation: C:\Program Files\dotnet\... is a protected install location, and builds should not need to write there. So the right fix is not editing anything under the SDK folder, but correcting the project’s Windows target so the build resolves WinRT/CsWinRT paths normally.

    Steps

    • In Project Properties (or in the project settings UI), change the Windows SDK / Target OS version from a value ending in .1 to the matching value ending in .0.
      • Example: change 10.0.19041.110.0.19041.0
      • If you set the target in the project file, make sure the TargetFramework Windows version also ends in .0 (not .1).
      • After switching to a “.0” Windows target version, rebuild. The WinRT path resolves again, so GetDirectoryName(...) no longer receives an empty string and the build succeeds.

    References

    Please let us know if you require any further assistance, we’re happy to help. If you found this information useful, kindly mark this as "Accept Answer". So that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    3 people found this answer helpful.

  2. Q&A Assist
    2026-01-07T07:56:21.15+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The error message you are encountering, "GetDirectoryName('') cannot be evaluated. The path is not of a legal form," indicates that you are trying to use the GetDirectoryName method with an empty string as the path. According to the documentation, the GetDirectoryName method requires a valid path string. If the path is empty, it will not be able to return a directory name and will throw an exception.

    To resolve this issue, ensure that you are passing a valid file or directory path to the GetDirectoryName method. If you need to handle cases where the path might be empty or null, you can add a check before calling the method to avoid this error.

    For example:

    string? filePath = ""; // or null
    if (!string.IsNullOrEmpty(filePath)) {
        string? directoryName = Path.GetDirectoryName(filePath);
        // Proceed with using directoryName
    } else {
        // Handle the case where filePath is empty or null
    }
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.