2

したがって、私のプログラムでは、COM Automation (Silverlight 4 の AutomationFactory) を使用して FileSystemObject を作成し、そこに文字列 (theContent) を書き込みます。この場合の theContent は小さな UTF-8 XML ファイルであり、これを MemoryStream を使用して文字列にシリアライズしました。

文字列は問題ありませんが、何らかの理由で FileSystemObject の Write メソッドを呼び出すたびに、「HRESULT 0x800A0005 (CTL_E_ILLEGALFUNCTIONCALL from google)」というエラーが表示されます。最も奇妙な点は、"hello" のような別の単純な文字列を渡すと、問題なく動作することです。

何か案は?

または、直接シリアル化できる FileSystemObject を使用してファイル/テキスト ストリームを公開する方法があれば、それも良いでしょう (VB 以外には何も見つからないようです)。

前もって感謝します!

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

2 に答える 2

4

今日、Scripting.FileSystemObject の代わりに ADODB.Stream を使用して同じ問題を解決しました。

Silverlight 4 OOB アプリでは (昇格された信頼があっても)、'MyDocuments' およびその他のいくつかのユーザー関連の特別なフォルダー以外の場所にあるファイルにアクセスできません。回避策「COM+ オートメーション」を使用する必要があります。しかし、テキスト ファイルに最適な Scripting.FileSystemObject は、バイナリ ファイルを処理できません。幸いなことに、そこで ADODB.Stream を使用することもできます。そして、それはバイナリファイルをうまく処理します。Word テンプレート、.dotx ファイルでテストされた私のコードは次のとおりです。

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

ファイルの読み取りは次のように実行できます。

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();
    }
}
于 2011-02-26T16:12:02.523 に答える
0

それだけではない理由:

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

あるいは

File.WriteAllBytes("file.xml", content);
于 2010-08-11T19:29:45.343 に答える