I am currently rewriting a few PowerShell modules in C#, primarily to learn the language and get a grip on C# development. So far it has been going really well, with some 20 functions rewritten and tested.
One I'm stuck on is compressing a GZip string. I've managed to use functions written by others to complete the task, but I was (and still am) unable to directly translate my PowerShell function.
Original PowerShell code:
$ms = New-Object System.IO.MemoryStream
$cs = New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Compress)
$sw = New-Object System.IO.StreamWriter($cs)
$sw.Write($StringToCompress)
$sw.Close();
[System.Convert]::ToBase64String($ms.ToArray()) | Out-File $FilePath -Force | Out-Null
$cs.Close();
$ms.Close();
C# equivalent (Ignore void, was testing in LINQPad):
public static void CompressString(string stringToCompress)
{
var memoryStream = new MemoryStream();
var compression = new GZipStream(memoryStream, CompressionMode.Compress, true);
var streamWriter = new StreamWriter(compression);
Console.WriteLine("String to compress: " + stringToCompress);
streamWriter.Write(stringToCompress);
Console.WriteLine("Base64 encoded compressed string: " +
Convert.ToBase64String(memoryStream.ToArray()));
}
The PowerShell code works perfectly, yet the C# returns nothing. Why is this, as I believe my 'translation' is correct?
Just a reminder, I have fixed my issue with help from another, but am now researching why the above did not work.

$sw.Close()causes the stream writer to flush its buffer, but in the C# version you never close it - so by the time you callmemoryStream.ToArray()there's no guarantee the writer has actually completed the write to the underlying stream