{"record":{"id":"2851f534f5272dba","repo":"dotnet/wpf","slug":"sr-filenamemustnotbenull","errorCode":null,"errorMessage":"SR.FileNameMustNotBeNull","messagePattern":"SR\\.FileNameMustNotBeNull","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/OpenFileDialog.cs","lineNumber":71,"sourceCode":"\n        /// <summary>\n        ///  Opens the file selected by the user with read-only permission,\n        ///  whether or not the Read Only checkbox is checked in the dialog.\n        /// </summary>\n        ///  The filename used to open the file is the first element of the\n        ///  FileNames array.\n        /// <exception cref=\"System.InvalidOperationException\">\n        /// Thrown if there are no filenames stored in the OpenFileDialog.\n        /// </exception>\n        public Stream OpenFile()\n        {\n            string filename = CriticalItemName;\n\n            // If we got an empty or null filename, throw an exception to\n            // tell the user we don't have any files to open.\n            if (string.IsNullOrEmpty(filename))\n            {\n                throw new InvalidOperationException(SR.FileNameMustNotBeNull);\n            }\n\n            return new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);\n        }\n\n        /// <summary>\n        ///  Opens the files selected by the user with read-only permission and\n        ///  returns an array of streams, one per file.\n        /// </summary>\n        /// <exception cref=\"System.InvalidOperationException\">\n        /// Thrown if there are no filenames stored in the OpenFileDialog\n        /// </exception>\n        public Stream[] OpenFiles()\n        {\n            // Cache ItemNames to avoid perf issues as per\n            // FxCop #CA1817\n            string[] cachedFileNames = CloneItemNames();\n","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/OpenFileDialog.cs#L53-L89","documentation":"OpenFileDialog.OpenFile opens a stream on the file the user selected (CriticalItemName). If the dialog was never successfully completed or returned no filename, the property is null/empty and an InvalidOperationException is thrown rather than attempting to open an invalid path.","triggerScenarios":"Calling OpenFile() without first calling ShowDialog and checking the result equals true; calling it after the user cancelled the dialog; deserializing/reusing an OpenFileDialog instance that has no cached filename.","commonSituations":"Assuming ShowDialog succeeded without checking the nullable bool return; calling OpenFile in code paths that run even when the user pressed Cancel; using a fresh OpenFileDialog that was never shown.","solutions":["Call ShowDialog() first and only call OpenFile() when it returned true","Check string.IsNullOrEmpty(dlg.FileName) before calling OpenFile as a defensive guard","If the dialog may be cancelled, handle the cancellation path instead of opening a stream","For multiple selections use FileNames/OpenFiles with the same checks"],"exampleFix":"// before\nvar dlg = new OpenFileDialog();\nusing var s = dlg.OpenFile();\n// after\nvar dlg = new OpenFileDialog();\nif (dlg.ShowDialog() == true)\n{\n    using var s = dlg.OpenFile();\n}","handlingStrategy":"validation","validationCode":"if (dlg.ShowDialog() != true || string.IsNullOrEmpty(dlg.FileName))\n    return; // user cancelled or nothing selected\nusing var s = dlg.OpenFile();","typeGuard":"bool HasSelection(OpenFileDialog d) => d.ShowDialog() == true && !string.IsNullOrEmpty(d.FileName);","tryCatchPattern":"try { using var s = dlg.OpenFile(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"null\"))\n{\n    // dialog not completed; treat as cancellation\n}","preventionTips":["Always check ShowDialog() == true before accessing FileName/OpenFile","Treat cancellation as a normal flow branch","Never call OpenFile on a dialog that was never shown"],"tags":["wpf","openfiledialog","null","filename"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}