views:

83

answers:

2

So in my program I'm using COM Auotmation (AutomationFactory in Silverlight 4) to create a FileSystemObject, to which I write a string (theContent). theContent in this case is a small UTF-8 XML file, which I serialized using MemoryStream into the string.

The string is fine, but for some reason whenever I call the FileSystemObject's Write method I get the error "HRESULT 0x800A0005 (CTL_E_ILLEGALFUNCTIONCALL from google)." The strangest part is that if I pass another simple string, like "hello," it works with no problems.

Any ideas?

Alternatively, if there's a way to expose a file/text stream with FileSystemObject that I could serialize to directly, that would be good as well (I can't seem to find anything not in VB).

Thanks in advance!

string theContent = System.Text.Encoding.UTF8.GetString(content, 0, content.Length);
string hello = "hello";

 using (dynamic fsoCom = AutomationFactory.CreateObject("Scripting.FileSystemObject"))
 {
      dynamic file = fsoCom.CreateTextFile("file.xml", true);
      file.Write(theContent);
      file.Write(hello);
      file.Close();
 }
A: 

Why not just:

File.WriteAllText("file.xml", theContent, Encoding.UTF8);

or even

File.WriteAllBytes("file.xml", content);
Marc Gravell
Silverlight app doesn't have enough permission.
Chris J
A: 

FSO is rather primitive, it needs to be able to convert the string to the system code page. If your text contains any Unicode codepoint that doesn't have a corresponding character in the code page then it is liable to fail.

You can test this theory with:

string theContent = System.Text.Encoding.UTF8.GetString(content, 0, content.Length);
byte[] localized = System.Text.Encoding.Default.GetBytes(theContent)
string theContentLocal = System.Text.Encoding.Default.GetString(localized, 0, localized.Length);
bool problem = theContent != theContentLocal;
//...
file.Write(theContentLocal);

Look for question marks in theContentLocal for troublemakers.

Hans Passant
Silverlight CLR apparently doesn't support "Default" encoding. This sounds like the likely culprit, though--thanks.
Chris J