{"record":{"id":"c45445f7c52f8730","repo":"NickeManarin/ScreenToGif","slug":"file-is-empty","errorCode":null,"errorMessage":"File is empty","messagePattern":"File is empty","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"warning","filePath":"ScreenToGif.Util/LocalizationHelper.cs","lineNumber":367,"sourceCode":"\n    public static void ImportStringResource(string path)\n    {\n        try\n        {\n            if (string.IsNullOrEmpty(path))\n                throw new ArgumentException(\"Path is null\");\n\n            var destination = Path.Combine(Path.GetTempPath(), Path.GetFileName(path));\n\n            if (File.Exists(destination))\n                File.Delete(destination);\n\n            File.WriteAllText(destination, File.ReadAllText(path).Replace(\"&#x0d;\", \"\\r\"));\n\n            using var fs = new FileStream(destination, FileMode.Open, FileAccess.Read, FileShare.Read);\n\n            if (fs.Length == 0)\n                throw new InvalidDataException(\"File is empty\");\n\n            //Reads the ResourceDictionary file\n            var dictionary = (ResourceDictionary)XamlReader.Load(fs);\n            dictionary.Source = new Uri(destination);\n\n            //Add in newly loaded Resource Dictionary.\n            Application.Current.Resources.MergedDictionaries.Add(dictionary);\n        }\n        catch (Exception ex)\n        {\n            LogWriter.Log(ex, \"Import Resource\");\n            //Rethrowing, because it's more useful to catch later\n            throw;\n        }\n    }\n\n    public static List<ResourceDictionary> GetLocalizations()\n    {","sourceCodeStart":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/NickeManarin/ScreenToGif/blob/a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd/ScreenToGif.Util/LocalizationHelper.cs#L349-L385","documentation":"Thrown by LocalizationHelper.ImportStringResource after copying a user-selected XAML localization file to temp and opening it with FileStream. If the copied file has zero bytes (fs.Length == 0), an InvalidDataException is thrown before attempting to parse it as XAML.","triggerScenarios":"The source file at 'path' exists but contains zero bytes. This happens when the file was created but never written to, or was truncated/corrupted during a previous save. File.ReadAllText succeeds on an empty file and File.WriteAllText produces an empty destination.","commonSituations":"User selected an empty or partially downloaded localization XAML file. A previous export operation failed mid-write, leaving a zero-byte file. File system issue (disk full, permissions) caused a zero-byte write. The source path points to a placeholder file.","solutions":["Check the source file size before calling ImportStringResource and warn the user if it is empty.","Verify the file is a valid XAML ResourceDictionary by attempting a quick parse before importing.","Re-download or re-export the localization file from a known-good source.","Catch InvalidDataException at the call site and show a user-friendly message."],"exampleFix":"// before\nif (fs.Length == 0)\n    throw new InvalidDataException(\"File is empty\");\n\n// after: validate at entry, include file path in message\nvar fileInfo = new FileInfo(path);\nif (fileInfo.Length == 0)\n    throw new InvalidDataException($\"File '{path}' is empty.\");","handlingStrategy":"validation","validationCode":"var fileInfo = new FileInfo(path);\nif (!fileInfo.Exists || fileInfo.Length == 0)\n{\n    // Don't attempt import; warn the user\n    return;\n}","typeGuard":"static bool IsValidLocalizationFile(string path)\n{\n    return File.Exists(path) && new FileInfo(path).Length > 0;\n}","tryCatchPattern":"try\n{\n    LocalizationHelper.ImportStringResource(path);\n}\ncatch (InvalidDataException ex)\n{\n    Dialog.Ok(\"Localization\", \"Import failed\", $\"The file '{path}' is empty or invalid.\");\n}","preventionTips":["Validate file existence and size before importing.","After exporting a localization file, verify it was written correctly before allowing import.","Check for disk space issues that could cause zero-byte writes."],"tags":["localization","xaml","file-io","validation","resource-dictionary"],"backgroundTag":null,"analyzedSha":"a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd","analyzedAt":"2026-08-13T11:12:06.147Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}