Search code examples
c#filestream

Attempted to seek before beginning of stream inside a using() statement


So I got the IOException: Attempted to Seek before the beginning of the stream. But when I looked into it the seek statement was inside of a using statement. I might be missunderstanding the using() because as far as I knew this initializes the in this case filestream before running the encased code.

private string saveLocation = string.Empty;
// This gets called inside the UI to visualize the save location
public string SaveLocation
    {
        get 
        {
            if (string.IsNullOrEmpty(saveLocation))
            {
                saveLocation = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\Pastes";
                Initializer();
            }
            return saveLocation; 
        }
        set { saveLocation = value; }
    }

And this is the function it calls

private void Initializer()
    {

        // Check if the set save location exists
        if (!Directory.Exists(saveLocation))
        {
            Debug.Log("Save location did not exist");
            try
            {
                Directory.CreateDirectory(saveLocation);
            }
            catch (Exception e)
            {
                Debug.Log("Failed to create Directory: " + e);
                return;
            }
        }

        // Get executing assembly
        if (string.IsNullOrEmpty(executingAssembly))
        {
            string codeBase = Assembly.GetExecutingAssembly().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            executingAssembly = Uri.UnescapeDataString(uri.Path);
        }

        // Get the last received list
        if (!string.IsNullOrEmpty(executingAssembly))
        {
            var parent = Directory.GetParent(executingAssembly);
            if (!File.Exists(parent + @"\ReceivedPastes.txt"))
            {
                // empty using to create file, so we don't have to clean up behind ourselfs.
                using (FileStream fs = new FileStream(parent + @"\ReceivedPastes.txt", FileMode.CreateNew)) { }
            }
            else
            {
                using (FileStream fs = new FileStream(parent + @"\ReceivedPastes.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    if (fs.Seek(-20000, SeekOrigin.End) >= 0)
                    {
                        fs.Position = fs.Seek(-20000, SeekOrigin.End);
                    }

                    using (StreamReader sr = new StreamReader(fs))
                    {
                        while (sr.ReadLine() != null)
                        {
                            storedPastes.Add(sr.ReadLine());
                        }
                    }
                }
            }

        }

        isInitialized = true;
    }

Solution

  • Are the commentors have posted: the file is less than 20000 bytes. It seems like you assume that Seek will stay at position 0 if the file is not large enough. It doesn't. It throws ArgumentException in that case.

    Another thing. Seek will move the position for you. No need to do both. Either use:

    fs.Seek(-20000, SeekOrigin.End);
    

    or set the position:

    fs.Position = fs.Length - 20000;
    

    So what you really wanted to write is:

    if (fs.Length > 20000)
        fs.Seek(-20000, SeekOrigin.End);