dotnet/wpf · error · ArgumentNullException

ArgumentNullException: destinationFile

Error message

ArgumentNullException: destinationFile

What it means

TaskFileService.WriteFile saves generated file content (as a byte array) to disk or to the VS host in-memory file manager, adding a UTF-8 BOM. The library throws ArgumentNullException when the destinationFile parameter is null or empty because there is no meaningful path to write to.

Solutions

  1. Ensure the destination path is computed and passed non-empty before calling WriteFile.
  2. If the path comes from an MSBuild property/metadata, verify it is set in the project or .targets file.
  3. Wrap the call with an explicit guard that produces a clearer error naming the unset property.

Example fix

// before
fileService.WriteFile(content, outputPath); // outputPath may be null
// after
if (string.IsNullOrEmpty(outputPath))
    throw new ArgumentException("Output path must be provided", nameof(outputPath));
fileService.WriteFile(content, outputPath);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(destinationFile))
    throw new ArgumentException("destinationFile must be a non-empty path", nameof(destinationFile));

Type guard

bool HasPath(string p) => !string.IsNullOrEmpty(p);

Try / catch

try { fileService.WriteFile(content, path); }
catch (ArgumentNullException ex) { log.Error($"Write target missing: {ex.ParamName}"); throw; }

Prevention

When it happens

Trigger: Calling WriteFile(byte[] contentArray, string destinationFile) with destinationFile == null or string.Empty.

Common situations: Custom build tasks or tooling that invoke PresentationBuildTasks' file service directly and compute the output path from MSBuild properties that were not set, yielding an empty path.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/fd5b412a7f62ce57. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/MS/Internal/Tasks/TaskFileService.cs:287

            {
                HostFileManager.Delete(fileName);
            }
            else
            {
                File.Delete(fileName);
            }
        }

        //
        // Save content stream for the given destination file
        // UTF8 BOM should not be added to the contentArray.  This
        // method adds BOM before writing file to disk.
        //
        public void WriteFile(byte[] contentArray, string destinationFile)
        {
            if (string.IsNullOrEmpty(destinationFile))
            {
                throw new ArgumentNullException(nameof(destinationFile));
            }

            if (contentArray == null)
            {
                throw new ArgumentNullException(nameof(contentArray));
            }

            UTF8Encoding utf8Encoding = new UTF8Encoding();
            string contentStr = utf8Encoding.GetString(contentArray);

            if (IsFileInHostManager(destinationFile))
            {
                // PutGeneratedFileContents adds BOM to the file when saving
                // to memory or disk.
                HostFileManager.PutGeneratedFileContents(destinationFile, contentStr);
            }
            else
            {

View on GitHub (pinned to 81131a70a4)