我需要制作一个转换工具,首先说我是菜鸟,下面的代码都是查资料编写出来的。
打开文件的模块已经写好了,
“开始转换”的那个模块最难,我还没开始,在开始之前我想先测验能否正确读取csv里的数据,结果是虽然能正确读取我想要的行,但可惜我用showbox测试输出的是乱码,现在的问题是如何让showbox输出正确的文字?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace POP3通讯簿自动制作
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //选取由Exchange管理器导出的csv文件路径
        private void button1_Click(object sender, EventArgs e)
        {
            string resultFile = "";
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "All files (*.*)|*.*|csv files (*.csv)|*.csv";
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                resultFile = openFileDialog1.FileName;
            select_file_path.Text = resultFile;
        }
        //选取转换后的文件的保存路径
        private void button2_Click(object sender, EventArgs e)
        {
            string resultPath = "";
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            //folderBrowserDialog1.RootFolder = "";
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                resultPath = folderBrowserDialog1.SelectedPath;
            save_file_path.Text = resultPath;
        }
        //开始转换
        private void btn_create_Click(object sender, EventArgs e)
        {
            string[] lines = System.IO.File.ReadAllLines(select_file_path.Text);
            int formatingline=2;
            everylinedatadispose(lines,formatingline);
        }
        static void everylinedatadispose(string[] line, int formatline)
        {
            MessageBox.Show(line[formatline]);
        }
    }
}

解决方案 »

  1.   


    比如有个字符串:
    存时进行System.Web.HttpUtility.UrlEncode(strAbc, System.Text.Encoding.UTF8);取时:System.Web.HttpUtility.UrlDecode(strAbc, System.Text.Encoding.UTF8);
      

  2.   

    估计是编码格式问题,建议你使用带编码的重载:
    public static string[] ReadAllLines (
    string path,
    Encoding encoding
    )
      

  3.   

    其实如果仅仅是showbox输出的是乱码而不影响程序处理后的数据的话,也是无大碍的。但是我现在不确定影不影响。如果说确实有影响的话,那也就是说从一开始就不应该用string来读取csv每一行的数据了?
      

  4.   

    该方法还有一个重载:File.ReadAllLines(path, Encoding.UTF8);尝试一下其他编码
      

  5.   

    如果你读的文件中含有中文,就
    System.IO.File.ReadAllLines(path, System.Text.Encoding.GetEncoding("GB2312"));
    非accii的,最好写上编码方式。
      

  6.   

    string[] lines = System.IO.File.ReadAllLines(select_file_path.Text,Encoding.Default);经测试,添加了Encoding.Default参数后输出正常了!Encoding.utf8,Encoding.unicode,Encoding.ASCII均不行!提供给各位参考。感谢5楼及各位的解答!
      

  7.   

    做编码格式处理.如考虑到有中文可以使用GB2312,或UTF-8 或是Default