Skip to content

stream.SetLength() not truncating file #1454

Open
@degosoft

Description

@degosoft

I'm trying to truncate a file on a SFTP server by opening a stream and then setting the length. The docs state that the stream will be truncated to the provided length. But the file remains unchanged.

If the specified value is less than the current length of the stream, the stream is truncated and - if the current position is greater than the new length - the current position is moved to the last byte of the stream.

var _client = new SftpClient("host", "user", "pass");
try
{
    _client.Connect();
    using (var stream = _client.OpenWrite("\\file2.txt"))
    {
        stream.SetLength(9);
        //stream.Flush(); //no change with or without
    }
}
catch (Exception ex)
{
    Log(ex.Message);
}

The stream is working when I append more data to the file or if I make changes, but it won't get smaller. Thus I can't remove text from the file.

How can I remove all bytes from a file past a certain length?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions