473,289 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

problem with memorystream and streamwriter

Hi,

Could someone explain me why the following code doesn't work? The
memorystream always remains with length 0.

MemoryStream input = new MemoryStream();

StreamWriter swriter = new StreamWriter(input);

swriter.Write("test");

swriter.Close();


Oct 22 '06 #1
4 8386
nm, found it :)
Must user Flush() instead of Close() and keep the streamwriter open.

"Heron" <no****@nospam.comschreef in bericht
news:uH*************@TK2MSFTNGP05.phx.gbl...
Hi,

Could someone explain me why the following code doesn't work? The
memorystream always remains with length 0.

MemoryStream input = new MemoryStream();

StreamWriter swriter = new StreamWriter(input);

swriter.Write("test");

swriter.Close();


Oct 22 '06 #2
Heron <no****@nospam.comwrote:
Could someone explain me why the following code doesn't work? The
memorystream always remains with length 0.

MemoryStream input = new MemoryStream();
StreamWriter swriter = new StreamWriter(input);
swriter.Write("test");
swriter.Close();
Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here's a program which works:

using System;
using System.IO;

class Test
{
static void Main()
{
MemoryStream input = new MemoryStream();
StreamWriter swriter = new StreamWriter(input);
swriter.Write("test");
swriter.Close();
Console.WriteLine (input.ToArray().Length);
}
}

Note that you can't access the Length property after the MemoryStream
is closed, which the StreamWriter will do when it's closed.

If you want to access the Length property while the StreamWriter is
still open, you should call Flush() first:

using System;
using System.IO;

class Test
{
static void Main()
{
MemoryStream input = new MemoryStream();
StreamWriter swriter = new StreamWriter(input);
swriter.Write("test");
swriter.Flush();
Console.WriteLine (input.Length);
swriter.Close();
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 22 '06 #3
Hi Jon,

Thanks for replying. Below is a complete program as requested.
My problem was in the encrypt method which is solved now but i'm still
having a problem with the decrypt method, the sreader.ReadToEnd(); method
throws me a "Padding is invalid and cannot be removed." error.

using System;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Text;
namespace testconsole
{
class Program
{
static void Main(string[] args)
{
byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16 };
byte[] iv = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17 };
MyRijndael rn = new MyRijndael(key);
string enc = rn.Encrypt("abcdefghijklmnopqrstuvwxyz", iv);
string dec = rn.Decrypt(enc, iv);
}

public class MyRijndael
{
private byte[] _key;
public MyRijndael(byte[] key)
{
this._key = key;
}
public string Encrypt(string input, byte[] iv)
{
MemoryStream output = new MemoryStream();
Rijndael rn = Rijndael.Create();
CryptoStream scrypt = new CryptoStream(output,
rn.CreateEncryptor(this._key, iv), CryptoStreamMode.Write);
scrypt.FlushFinalBlock();
StreamWriter swriter = new StreamWriter(scrypt);
swriter.Write(input);
swriter.Flush();
byte[] buffer = output.ToArray();
swriter.Close();
scrypt.Close();
output.Close();
return Convert.ToBase64String(buffer);
}

public string Decrypt(string input64, byte[] iv)
{
MemoryStream input = new
MemoryStream(Convert.FromBase64String(input64));
Rijndael rn = Rijndael.Create();
CryptoStream scrypt = new CryptoStream(input,
rn.CreateDecryptor(this._key, iv), CryptoStreamMode.Read);
StreamReader sreader = new StreamReader(scrypt);
string res = sreader.ReadToEnd(); //<-- error
"Padding is invalid and cannot be removed."
sreader.Close();
scrypt.Close();
input.Close();
return res;
}
}
}
}

"Jon Skeet [C# MVP]" <sk***@pobox.comschreef in bericht
news:MP************************@msnews.microsoft.c om...
Heron <no****@nospam.comwrote:
>Could someone explain me why the following code doesn't work? The
memorystream always remains with length 0.

MemoryStream input = new MemoryStream();
StreamWriter swriter = new StreamWriter(input);
swriter.Write("test");
swriter.Close();

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here's a program which works:

using System;
using System.IO;

class Test
{
static void Main()
{
MemoryStream input = new MemoryStream();
StreamWriter swriter = new StreamWriter(input);
swriter.Write("test");
swriter.Close();
Console.WriteLine (input.ToArray().Length);
}
}

Note that you can't access the Length property after the MemoryStream
is closed, which the StreamWriter will do when it's closed.

If you want to access the Length property while the StreamWriter is
still open, you should call Flush() first:

using System;
using System.IO;

class Test
{
static void Main()
{
MemoryStream input = new MemoryStream();
StreamWriter swriter = new StreamWriter(input);
swriter.Write("test");
swriter.Flush();
Console.WriteLine (input.Length);
swriter.Close();
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Oct 22 '06 #4
Heron <no****@nospam.comwrote:
Thanks for replying. Below is a complete program as requested.
My problem was in the encrypt method which is solved now but i'm still
having a problem with the decrypt method, the sreader.ReadToEnd(); method
throws me a "Padding is invalid and cannot be removed." error.
You're calling FlushFinalBlock before you've actually written any data.
Call it after calling Flush on the StreamWriter instead.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: c duden | last post by:
I am attempting to encrypt some text and be able to decrypt it at a later time. I have two methods to do this: public static Byte EncryptText(string textToEncrypt, string encryptionHash) {...
1
by: Saper\(ek\) | last post by:
I need to encrypt some data in my program, so I've created 2 functions to encrypt and decrypt data. I've created a simple program to test it, and... it crashes. It wors ok on XP, but on win98 it...
7
by: Donovan | last post by:
I can't believe this is causing me as much difficulty as it is, but I have an Infragistics UltraTreeview control that I want to persist whatever the user has in the tree. It has a method SaveAsXml...
2
by: Reshma Prabhu | last post by:
hello, I am trying to do an xsl tranformation from an XML file into another xml file. I want the output file to be in MemoryStream so that my dataset can direclty read xml using...
1
by: R.L. | last post by:
See the code below, var 'content ' is suppose to be "Hello!", not "". Who knows why? Thanks ---------------------------------------- string text = "hello!"; MemoryStream stream = new...
2
by: almurph | last post by:
Hi everyone, Can you help me please? I am having a problem with the encryption/decryption of words with the Irish fada in them. The Irish fada is like this: αινσϊ/ΑΙΝΣΪ. It's kind of like the...
9
by: philip | last post by:
If I execute that : Dim Temp as string = "This is a text" Dim sw As StreamWriter Dim fullFileName as string = "c:\text.txt" sw = New StreamWriter(fullFilename) sw.Write(temp) sw.Close() ...
2
by: Mikus Sleiners | last post by:
I'm trying to write new stream from string and i can't figure out why my memory stream instance is null after i have writen to it with stream writer. Here is an example. MemoryStream stream =...
1
by: MSwanston | last post by:
Hi I need some help with saving retreiving data from the cache, and how best to structure my code. FYI am working in VS2005/under .NET2 Framework. Ok, we have a series of reports that get run via...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.