Question

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();
 }
Was it helpful?

Solution

I solved the same problem today using ADODB.Stream instead of Scripting.FileSystemObject.

In a Silverlight 4 OOB App (even with elevated trust), you cannot access files in locations outside of 'MyDocuments' and a couple of other user related special folders. You have to use the workaround 'COM+ Automation'. But the Scripting.FileSystemObject, which works great for text files, cannot handle binary files. Fortunately you can also use ADODB.Stream there. And that handles binary files just fine. Here is my code, tested with Word Templates, .dotx files:

public static void WriteBinaryFile(string fileName, byte[] binary)
{
    const int adTypeBinary = 1;
    const int adSaveCreateOverWrite = 2;
    using (dynamic adoCom = AutomationFactory.CreateObject("ADODB.Stream"))
    {
        adoCom.Type = adTypeBinary;
        adoCom.Open();
        adoCom.Write(binary);
        adoCom.SaveToFile(fileName, adSaveCreateOverWrite);
    }
}

A file read can be done like this:

public static byte[] ReadBinaryFile(string fileName)
{
    const int adTypeBinary = 1;
    using (dynamic adoCom = AutomationFactory.CreateObject("ADODB.Stream"))
    {
        adoCom.Type = adTypeBinary;
        adoCom.Open();
        adoCom.LoadFromFile(fileName);
        return adoCom.Read();
    }
}

OTHER TIPS

Why not just:

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

or even

File.WriteAllBytes("file.xml", content);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top