-1

那是我的代码

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

我想在开始时将 2 个字节放入文本文件。

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

问题是当我用十六进制打开时,我看不到这 2 个字节。但我在文本文件本身上看到了这些 http://img713.imageshack.us/img713/1143/zaqj.png:\

任何解决方案请帮忙。它假设看起来像这样http://img266.imageshack.us/img266/1105/y40i.png

4

1 回答 1

0

试试这样:

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

在参数中没有使用字节数组写入的实现:http: //msdn.microsoft.com/en-us/library/system.io.streamwriter.write.aspx

所以它使用 Object 参数并调用他的 ToString() 方法

于 2013-10-16T11:26:37.283 回答