-1

thats my code

static void Write(DataTable dt, string outputFilePath)
        {
        int[] maxLengths = new int[dt.Columns.Count];

        for (int i = 0; i < dt.Columns.Count; i++)
        {
            maxLengths[i] = dt.Columns[i].ColumnName.Length;

            foreach (DataRow row in dt.Rows)
            {
                if (!row.IsNull(i))
                {
                    int length = row[i].ToString().Length;

                    if (length > maxLengths[i])
                    {
                        maxLengths[i] = length;
                    }
                }
            }
        }

        using (StreamWriter sw = new StreamWriter(outputFilePath, false))
        {   
            sw.Write(new byte[]{0xff,0xfe});
            foreach (DataRow row in dt.Rows)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (!row.IsNull(i))
                    {
                        sw.Write(row[i].ToString() + "  ");
                    }
                }

                sw.WriteLine();
            }
        }
    }

i want to put 2 bytes to the text file at the beginning.

sw.Write(new byte[]{0xff,0xfe}); 

the problem is when i open with hex i don't see those 2 bytes. but i see on the text file itself these http://img713.imageshack.us/img713/1143/zaqj.png :\

any solution please help. its suppose to look like this http://img266.imageshack.us/img266/1105/y40i.png

Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
user2799605
  • 187
  • 1
  • 2
  • 6
  • Why do you want to write an UTF-16 BOM and then output the content as UTF-8? My guess is that the file should be UTF-16 and you should just use `new StreamWriter(..., Encoding.Unicode)` instead. – adrianm Oct 16 '13 at 13:08

1 Answers1

0

try like this :

sw.Write(new char[]{(char)0xff , (char)0xfe});

there is no implementaion of Write with array of byte in params : http://msdn.microsoft.com/en-us/library/system.io.streamwriter.write.aspx

so it use Object parameter and call his ToString() method

Pascalz
  • 2,348
  • 1
  • 24
  • 25