Default Encoding

Hi,

I am at the point in my appication where I am downloading an email attachment from an exchange server. I can save the attachment, but cannot open it.

Does this have something to do with the default encoding type I am using StreamWritier to write to the file, here's the constructor:


StreamWriter (String) Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the default encoding and buffer size.

Supported by the .NET Compact Framework.


Any help is much appreciated !
Thanks


Answer this question

Default Encoding

  • jjre

    arch_angel81,
    I think your problem is that you are trying to decode the bits of the zip file using UTF-8. This is quite likely to mangle your file, so that you won't be able to open it as a zip.

    Try saving the bytes you get from the Convert.FromBase64String directly, and you should be ok.

    HTH
    --mc


  • Amritha

    OK,

    Now I am using this function to decode the email attachments from base64 to the default encoding:

    public string base64Decode(string data)
    {
    try
    {
    System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
    System.Text.Decoder utf8Decode = encoder.GetDecoder();

    byte[] todecode_byte = Convert.FromBase64String(data);
    int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
    char[] decoded_char = new char[charCount];
    utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
    string result = new String(decoded_char);
    return result;
    }
    catch(Exception e)
    {
    throw new Exception("Error in base64Decode" + e.Message);
    }
    }
    This works with files that are not zipped. With archived files I lose the data (i.e. when I double- click on the archive it opens up fine, but there is not file inside).

  • Luis_Ga

    Could you be more specific about the problems you're having opening the file Do you mean you can't open it by double-clicking on it, or a program you've written isn't able to read from the file Are you getting an error message or an exception, and if so could you post it

    The encoding is one possible issue, but you'll need to provide more infomation about what you're trying to do with the file.

    -Tom Meschter
    Software Dev, Visual C# IDE



  • Russell81

    Yes I cannot open the file by double-clicking on it (zip file). Error message:
    - The archive is either in unknown format or damaged!

    With text file attachments (.txt), the text comes out in the wrong format.

    Thanks,

  • Jorijn--

    back up a bit!

    how are you getting the attachment and saving it



  • Bruce Bukovics

    If you are using mapi/cdo you should use the Attachment object's WriteToFile( filepath ) method......

  • DKB

    Yes thankyou,

    It works, it was the UTF-8 encoding that was messing things up!!!

  • systech

    I'm using an IMAP component.

  • Default Encoding