Chris J
Chris J

Reputation: 3848

Scripting.FileSystemObject Write method fails

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();
 }

Upvotes: 2

Views: 6670

Answers (3)

MaliEbbae
MaliEbbae

Reputation: 375

For Unicode text that passed into CreateTextFile() method the parameter of Unicode should change to True, otherwise you will get CTL_E_ILLEGALFUNCTIONCALL. This solved my problem.

enter image description here

Upvotes: 0

kaiz.net
kaiz.net

Reputation: 1994

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();
    }
}

Upvotes: 4

Marc Gravell
Marc Gravell

Reputation: 1064114

Why not just:

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

or even

File.WriteAllBytes("file.xml", content);

Upvotes: 0

Related Questions