{"record":{"id":"fd5b412a7f62ce57","repo":"dotnet/wpf","slug":"argumentnullexception-destinationfile","errorCode":null,"errorMessage":"ArgumentNullException: destinationFile","messagePattern":"ArgumentNullException: destinationFile","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/MS/Internal/Tasks/TaskFileService.cs","lineNumber":287,"sourceCode":"            {\n                HostFileManager.Delete(fileName);\n            }\n            else\n            {\n                File.Delete(fileName);\n            }\n        }\n\n        //\n        // Save content stream for the given destination file\n        // UTF8 BOM should not be added to the contentArray.  This\n        // method adds BOM before writing file to disk.\n        //\n        public void WriteFile(byte[] contentArray, string destinationFile)\n        {\n            if (string.IsNullOrEmpty(destinationFile))\n            {\n                throw new ArgumentNullException(nameof(destinationFile));\n            }\n\n            if (contentArray == null)\n            {\n                throw new ArgumentNullException(nameof(contentArray));\n            }\n\n            UTF8Encoding utf8Encoding = new UTF8Encoding();\n            string contentStr = utf8Encoding.GetString(contentArray);\n\n            if (IsFileInHostManager(destinationFile))\n            {\n                // PutGeneratedFileContents adds BOM to the file when saving\n                // to memory or disk.\n                HostFileManager.PutGeneratedFileContents(destinationFile, contentStr);\n            }\n            else\n            {","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/MS/Internal/Tasks/TaskFileService.cs#L269-L305","documentation":"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.","triggerScenarios":"Calling WriteFile(byte[] contentArray, string destinationFile) with destinationFile == null or string.Empty.","commonSituations":"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.","solutions":["Ensure the destination path is computed and passed non-empty before calling WriteFile.","If the path comes from an MSBuild property/metadata, verify it is set in the project or .targets file.","Wrap the call with an explicit guard that produces a clearer error naming the unset property."],"exampleFix":"// before\nfileService.WriteFile(content, outputPath); // outputPath may be null\n// after\nif (string.IsNullOrEmpty(outputPath))\n    throw new ArgumentException(\"Output path must be provided\", nameof(outputPath));\nfileService.WriteFile(content, outputPath);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(destinationFile))\n    throw new ArgumentException(\"destinationFile must be a non-empty path\", nameof(destinationFile));","typeGuard":"bool HasPath(string p) => !string.IsNullOrEmpty(p);","tryCatchPattern":"try { fileService.WriteFile(content, path); }\ncatch (ArgumentNullException ex) { log.Error($\"Write target missing: {ex.ParamName}\"); throw; }","preventionTips":["Resolve output paths from MSBuild properties and assert non-empty at task start","Never pass through raw property values without checking","Unit-test tasks with missing-property configurations"],"tags":["null-argument","file-write","wpf-build-tasks"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}