C#:导出到文本文件时,项目符号混乱

时间:2011-12-02 20:35:49

标签: c# file

我有一个问题 - 当我export我的数据发送到文本时,我的文本文件没有正确创建 - 子弹被搞砸了。 我曾尝试使用实际的子弹或字节子弹;不起作用!

你能告诉我我做错了吗?

public void createFile()
{
    String Body;
    String TAB = "\t";
    String RETURN = "\r\n";

    Byte[] buffer = new byte[] { (byte)149 };
    string bullet = Encoding.GetEncoding(1252).GetString(buffer);

    Body = TAB + bullet + TAB + "TEXT1" + RETURN;
    Body = Body + "•" + TAB + "TEXT2" + RETURN;
    System.IO.StreamWriter file = new System.IO.StreamWriter(@"c:\fname.txt");
    file.WriteLine(Body);
    file.Close();
}

4 个答案:

答案 0 :(得分:2)

我刚试过这个,它在文本文件中正确添加了一个项目符号

    String correctString = "\u2022"; 
    System.IO.StreamWriter ansiWriter = new StreamWriter(@"c:\projects\file.txt", false, Encoding.GetEncoding(1250)); 
    ansiWriter.WriteLine(correctString); 
    ansiWriter.Close();             

答案 1 :(得分:1)

第2行是修复。

Body = TAB + bullet + TAB + "TEXT1" + RETURN;
Body += TAB + "•" + TAB + "TEXT2" + RETURN;
System.IO.StreamWriter file = new System.IO.StreamWriter(@"c:\fname.txt");
file.WriteLine(Body);
file.Close();

答案 2 :(得分:1)

 System.Text.Encoding Encoder = System.Text.ASCIIEncoding.Default;
 Byte[] buffer = new byte[]{(byte)149};
 string bullet = Encoding.GetEncoding(1252).GetString(buffer);
//why are you adding the "." bullet back with BODY = BODY + "." ?

Create a StringBuilder and use the Append Method()

StringBuilder messageBuilder = new StringBuilder(); //add a capacity if you know the size you want like this for example StringBuilder messageBuilder = new StringBuilder(200); 
messageBuilder.Append("\t\u2022\t"); 
messageBuilder.Append("Text1\r\n");

follow the pattern with what you need.
Thanks

答案 3 :(得分:0)

好的,我需要指定不同的编码:

        String correctString ="•";
        System.IO.StreamWriter ansiWriter = new StreamWriter(@"c:\file.txt", false, Encoding.GetEncoding(1250));
        ansiWriter.WriteLine(correctString);
        ansiWriter.Close();