NickeManarin/ScreenToGif · warning · InvalidDataException

File is empty

Error message

File is empty

What it means

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.

Source

Thrown at ScreenToGif.Util/LocalizationHelper.cs:367

    public static void ImportStringResource(string path)
    {
        try
        {
            if (string.IsNullOrEmpty(path))
                throw new ArgumentException("Path is null");

            var destination = Path.Combine(Path.GetTempPath(), Path.GetFileName(path));

            if (File.Exists(destination))
                File.Delete(destination);

            File.WriteAllText(destination, File.ReadAllText(path).Replace("
", "\r"));

            using var fs = new FileStream(destination, FileMode.Open, FileAccess.Read, FileShare.Read);

            if (fs.Length == 0)
                throw new InvalidDataException("File is empty");

            //Reads the ResourceDictionary file
            var dictionary = (ResourceDictionary)XamlReader.Load(fs);
            dictionary.Source = new Uri(destination);

            //Add in newly loaded Resource Dictionary.
            Application.Current.Resources.MergedDictionaries.Add(dictionary);
        }
        catch (Exception ex)
        {
            LogWriter.Log(ex, "Import Resource");
            //Rethrowing, because it's more useful to catch later
            throw;
        }
    }

    public static List<ResourceDictionary> GetLocalizations()
    {

View on GitHub (pinned to a4d0a67c21)

Solutions

  1. Check the source file size before calling ImportStringResource and warn the user if it is empty.
  2. Verify the file is a valid XAML ResourceDictionary by attempting a quick parse before importing.
  3. Re-download or re-export the localization file from a known-good source.
  4. Catch InvalidDataException at the call site and show a user-friendly message.

Example fix

// before
if (fs.Length == 0)
    throw new InvalidDataException("File is empty");

// after: validate at entry, include file path in message
var fileInfo = new FileInfo(path);
if (fileInfo.Length == 0)
    throw new InvalidDataException($"File '{path}' is empty.");
Defensive patterns

Strategy: validation

Validate before calling

var fileInfo = new FileInfo(path);
if (!fileInfo.Exists || fileInfo.Length == 0)
{
    // Don't attempt import; warn the user
    return;
}

Type guard

static bool IsValidLocalizationFile(string path)
{
    return File.Exists(path) && new FileInfo(path).Length > 0;
}

Try / catch

try
{
    LocalizationHelper.ImportStringResource(path);
}
catch (InvalidDataException ex)
{
    Dialog.Ok("Localization", "Import failed", $"The file '{path}' is empty or invalid.");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of NickeManarin/ScreenToGif@a4d0a67c21 (2026-08-13). Data as JSON: /api/errors/c45445f7c52f8730. Report an issue: GitHub.