FileStream fs = new FileStream(sfd_cps.FileName, FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs, Encoding.UTF8);
                m_streamWriter.Flush();
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
                m_streamWriter.Write("这里只接收字符串");
                m_streamWriter.Flush();
                m_streamWriter.Close();//m_streamWriter.Write不接收byte[]型,又不想把byte[]转成字符串,有什么方法可以保存成文件?

解决方案 »

  1.   

    [C#]
    public override void Write(
       byte[] array,
       int offset,
       int count
    );
    用fs.Write(yourByts, offset, count);
      

  2.   

    using System;
    using System.IO;class BinaryRW
    {
        static void Main()
        {
            const int arrayLength = 1000;        // Create random data to write to the stream.
            byte[] dataArray = new byte[arrayLength];
            new Random().NextBytes(dataArray);        BinaryWriter binWriter = new BinaryWriter(new MemoryStream());        // Write the data to the stream.
            Console.WriteLine("Writing the data.");
            binWriter.Write(dataArray);        // Create the reader using the stream from the writer.
            BinaryReader binReader = 
                new BinaryReader(binWriter.BaseStream);        // Set Position to the beginning of the stream.
            binReader.BaseStream.Position = 0;        // Read and verify the data.
            byte[] verifyArray = binReader.ReadBytes(arrayLength);
            if(verifyArray.Length != arrayLength)
            {
                Console.WriteLine("Error writing the data.");
                return;
            }
            for(int i = 0; i < arrayLength; i++)
            {
                if(verifyArray[i] != dataArray[i])
                {
                    Console.WriteLine("Error writing the data.");
                    return;
                }
            }
            Console.WriteLine("The data was written and verified.");
        }
    }