views:

521

answers:

1

This works:

using (StreamWriter stw = new StreamWriter(Server.MapPath("\\xml\\file.xml")))
{
    stw.Write(xmlEncStr);
}

This creates an empty file:

using (FileStream file = new FileStream(Server.MapPath("\\xml\\file.xml"), FileMode.CreateNew))
{
    using (StreamWriter sw = new StreamWriter(file))
    {
        sw.Write(xmlEncStr);
    }
}

I tried playing around with the FileStream constructor and tried flushing and I still get a zero byte file. The string I am writing is a simple base64 encoded ascii string with no special characters.

I know I can use the first example, but why won't the second work?

Update

This wasn't a Filestream/StreamWriter problem - it was a variable naming problem. I corrected the code above, so now both versions work. I originally had:

StreamWriter strw = new StreamWriter(file)
+2  A: 

You could shorten your code a bit:

File.WriteAllText(Server.MapPath("\\xml\\file.xml"), xmlEncStr);

Also the MapPath method accepts a relative or virtual path and converts it to the corresponding physical path on the server. \\xml\\file.xml is non of the above. It probably should be: ~/xml/file.xml.

Darin Dimitrov
Dont forget about the actual text to write
SwDevMan81
@SwDevMan81, thanks for the good remark. Updated my answer.
Darin Dimitrov