0

我需要将 one.xaml 文件的内容复制到一个字节 clob 中。这是我的代码。看起来我没有访问此文件的内容。谁能告诉我为什么。我是 C# API 的新手,但我是一名程序员。选择 4000 是因为最大字符串大小限制,以防万一有人想知道。我可能有关于 zies 等的错误。但主要是我想将 thje xaml 文件的内容放入 clob 中。谢谢。

           string LoadedFileName = @"C:\temp2\one.xaml";//Fd.FileName;
             byte[] clobByteTotal ;
             FileStream stream = new FileStream(LoadedFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
             if (stream.Length % 2 >= 1)
             {
                  clobByteTotal = new byte[stream.Length + 1];
             }
             else clobByteTotal = new byte[stream.Length];
             for (int i = 0; i <= stream.Length/4000; i++)
             {   
                 int x = (stream.Length / 4000 == 0) ? (int)stream.Length : 4000;
                 stream.Read(stringSizeClob, i*4000, x);
                 String tempString1 = stringSizeClob.ToString();
                 byte[] clobByteSection = Encoding.Unicode.GetBytes(stringSizeClob.ToString());
                 Buffer.BlockCopy(clobByteSection, 0, clobByteTotal, i * clobByteSection.Length, clobByteSection.Length);
             }
4

2 回答 2

2

如果您只需要将文本文件的内容读入字节数组,就可以这样做

string xamlText = File.ReadAlltext(LoadedFileName );
byte[] xamlBytes = Encoding.Unicode.GetBytes(xamlText); //if this is a Unicode and not UTF8

//write byte data somewhere

这个更短的选项自然适用于不太大的文件。

于 2012-09-14T20:45:01.477 回答
1

有什么理由不使用File.ReadAllBytes吗?

byte[] xamlBytes = File.ReadAllBytes(path);
于 2012-09-14T20:51:26.857 回答