sba
Guest
|
Posted:
Mon Aug 08, 2005 4:59 pm Post subject:
ADODB stream writes a unicode header |
|
|
Hi there
I'm opening a stream to an ADODB record of a mail and then I write the
stream into a file. The file starts with a unicode header (FF FE), but
then goes ahead in plain ASCII.
This seems not right to me. Is this a bug or a feature? How can I make
it write the whole mail in ASCII? I could of course just cut off the
first two bytes but this might get me into trouble on other
nationalised deployments.
Regards,
Stephan Bachofen
public class WebStoreEventSinkHandler
{
public class AsyncEvents : ServicedComponent,
Exoledb.IExStoreAsyncEvents
{
public void OnDelete(Exoledb.IExStoreEventInfo pEventInfo, string
bstrURLItem, int lFlags)
{
// Implement OnDelete code here.
}
public void OnSave(Exoledb.IExStoreEventInfo pEventInfo, string
bstrURLItem, int lFlags)
{
try
{
ADODB.Connection oCn = new ADODB.Connection();
try
{
oCn.Provider = "exoledb.datasource";
oCn.Open(bstrURLItem, "", "", -1);
ADODB.Record record= new ADODB.RecordClass();
record.Open(bstrURLItem, oCn,
ADODB.ConnectModeEnum.adModeReadWrite,
ADODB.RecordCreateOptionsEnum.adFailIfNotExists,
ADODB.RecordOpenOptionsEnum.adOpenSource, "", "");
ADODB.Stream stream= new ADODB.StreamClass();
stream.Open(record, ConnectModeEnum.adModeReadWrite,
StreamOpenOptionsEnum.adOpenStreamFromRecord, "", "");
String filename= "D:\\Temp\\" + "ExchangeMail" +
DateTime.Now.Ticks + ".eml";
stream.SaveToFile(filename,
ADODB.SaveOptionsEnum.adSaveCreateNotExist);
}
finally
{
try
{
oCn.Close();
}
catch (Exception e)
{
// Nothing I could do.
}
}
}
catch (Exception e)
{
WriteToLog("Executing WebStoreEventSinkHandler::OnSave ->
failed. Caught exception: " + e.Message + ". Stacktrace:" +
e.ToString());
throw e;
}
}
}
|
|
sba
Guest
|
Posted:
Wed Aug 10, 2005 11:06 pm Post subject:
Re: ADODB stream writes a unicode header |
|
|
I've solved the riddle. The solution is to set the type as binary on
the stream and read with read(...).
Just a note to the developer of the ADODB.Stream: Thanks for the very
usefull exception text: "Operation is not allowed in this context." I
did get that text for all exceptions calling various methods on that
stream with various configurations. You saved time and wasted my time.
The code is here for future reference:
ADODB.Record record= new ADODB.RecordClass();
record.Open(bstrURLItem, oCn,
ADODB.ConnectModeEnum.adModeReadWrite,
ADODB.RecordCreateOptionsEnum.adFailIfNotExists,
ADODB.RecordOpenOptionsEnum.adOpenSource,
"", "");
ADODB.Stream stream= new ADODB.StreamClass();
stream.Type= ADODB.StreamTypeEnum.adTypeBinary;
stream.Open(record, ConnectModeEnum.adModeReadWrite,
StreamOpenOptionsEnum.adOpenStreamFromRecord, "", "");
byte[] rawMsg= (byte[])stream.Read(stream.Size);
writeToFile(filename, rawMsg);
void writeToFile(String filename, String content)
{
// create a writer and open the file
TextWriter tw = null;
try
{
tw= new StreamWriter(filename, false,
System.Text.Encoding.ASCII);
tw.WriteLine(content);
}
finally
{
if (null != tw)
{
tw.Close();
}
}
}
void writeToFile(String filename, byte[] content)
{
writeToFile(filename, ByteArrayToStr(content));
} |
|