Click here to Skip to main content
16,003,902 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I'm facing a problem with saving attachment content stream to a file, the problem is that the saved files contains leading bytes, specifically in my case three question marks ??? .
The problem seems simple but i'm not sure what is behind it , is it BOM (Byte Order Mark) written to the file or is it prefix length of the stream ?
I've tried several ways , but with all had the same results:
Here is my used methods:
First one:
C#
public void saveAttachment(System.Net.Mail.Attachment attachment)
        {
            StreamReader reader = new StreamReader(attachment.ContentStream);
            File.WriteAllText(attachment.Name, reader.ReadToEnd());
        }

Second one (similar to the first):
C#
public void saveAttachment(System.Net.Mail.Attachment attachment)
        {
            StreamWriter writer = new StreamWriter(attachment.Name);
            StreamReader reader = new StreamReader(attachment.ContentStream);
            writer.Write(reader.ReadToEnd());
            writer.Close();
        }


Third method based on the link :


C#
public void saveMailAttachment(System.Net.Mail.Attachment attachment)
{
    byte[] allBytes = new byte[attachment.ContentStream.Length];

    int bytesRead = attachment.ContentStream.Read(allBytes, 0, (int)attachment.ContentStream.Length);

    string destinationFile = attachment.Name;

    BinaryWriter writer = new BinaryWriter(new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
    writer.Write(allBytes);
    writer.Close();
}



I tried playing with the bytes array , but i don't think it's a very reliable way to solve the problem , any help would be very appreciated.
Thanks in advance.
Posted
Updated 9-Sep-14 2:39am
v2

1 solution

If you always have 3, and exactly 3, marker characters at the start of the file, there's a very easy way to implement this based on your second implementation:

C#
public void saveAttachment(System.Net.Mail.Attachment attachment)
{
    using(var writer = new StreamWriter(attachment.Name))
    {
        using(var reader = new StreamReader(attachment.ContentStream))
        {
            // Set the stream pointer
            reader.BaseStream.Seek(3, SeekOrigin.Begin);
            writer.Write(reader.Read());
            writer.Close();
        }
    }
}


Dispose logic included free of charge. :)
 
Share this answer
 
Comments
Hazem Albezreh 9-Sep-14 11:09am    
Hey Nathan,
Thanks for your respond, now i have more insights about the problem , I'm using the following code :

<pre lang="cs">public void saveAttachment(System.Net.Mail.Attachment attachment)
{
using (var fileStream = File.Create("C:\\"+attachment.Name))
{

attachment.ContentStream.CopyTo(fileStream);
}

}</pre>

I'm facing the problem with text files or xml files or custom extension textual files , some characters are converted to ? by the reader stream, and it's not always three.
I'm not sure if the problem related to encoding .
I'm still working on it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900