Пример #1
0
        private void btnPreview_Click(object sender, EventArgs e)
        {
            if (!PathExist(txtConversionPath.Text))
            {
                return;
            }

            string str = string.Empty;

            str = cmbConversionFrom.Text;
            Encoding srcEncoding = Encoding.GetEncoding(int.Parse(
                                                            str.Substring(
                                                                str.LastIndexOf(':') + 1,
                                                                str.Length - str.LastIndexOf(':') - 2)));

            str = cmbConversionTo.Text;
            Encoding dstEncoding = Encoding.GetEncoding(int.Parse(
                                                            str.Substring(
                                                                str.LastIndexOf(':') + 1,
                                                                str.Length - str.LastIndexOf(':') - 2)));

            string content = string.Empty;

            char[] buff = new char[300];

            StreamReader sr = new StreamReader(txtConversionPath.Text, srcEncoding);

            sr.Read(buff, 0, 300);
            sr.Close();

            byte[] srcContent = srcEncoding.GetBytes(buff);
            content = srcEncoding.GetString(srcContent);

            if (chkPreviewSrc.Checked)
            {
                frmPreview preview = new frmPreview(srcEncoding.EncodingName + " [CP:" + srcEncoding.CodePage + "]", content);
                preview.Show();
            }

            if (chkPreviewDst.Checked)
            {
                if (srcEncoding.CodePage == 936 && dstEncoding.CodePage == 950)
                {
                    content = ConvertString.Gb2312SToBig5(content);
                }
                else if (srcEncoding.CodePage == 950 && dstEncoding.CodePage == 936)
                {
                    content = ConvertString.Big5ToGb2312S(content);
                }
                else
                {
                    byte[] dstContent = Encoding.Convert(srcEncoding, dstEncoding, srcContent);
                    content = dstEncoding.GetString(dstContent);
                }

                frmPreview preview = new frmPreview(dstEncoding.EncodingName + " [CP:" + dstEncoding.CodePage + "]", content);
                preview.Show();
            }
        }
Пример #2
0
        private bool ConvertFile(string fileName, Encoding srcEncoding, Encoding dstEncoding, bool newfile)
        {
            string dstFileName = fileName;

            if (newfile)
            {
                FileInfo fi = new FileInfo(fileName);
                if (fi.Extension.Equals(string.Empty))
                {
                    dstFileName = fileName + "_cec";
                }
                else
                {
                    dstFileName = fileName.Substring(0, fileName.LastIndexOf('.')) + "_cec.txt";
                }
            }

            StreamReader sr         = new StreamReader(fileName, srcEncoding);
            string       strContent = sr.ReadToEnd();

            sr.Close();

            if (srcEncoding.CodePage == 936 && dstEncoding.CodePage == 950)
            {
                strContent = ConvertString.Gb2312SToBig5(strContent);
            }
            else if (srcEncoding.CodePage == 950 && dstEncoding.CodePage == 936)
            {
                strContent = ConvertString.Big5ToGb2312S(strContent);
            }

            StreamWriter sw = new StreamWriter(dstFileName, false, dstEncoding);

            sw.Write(strContent);
            sw.Close();
            return(true);
        }