现在的位置: 首页 > 综合 > 正文

Aptana解决中文乱码工具

2013年04月01日 ⁄ 综合 ⁄ 共 5514字 ⁄ 字号 评论关闭

Aptana(我使用的版本是Aptana Studio, build: 1.2.1.020234)打开以前的文件发现对中文支持不好,出现了乱码。原因是文件的编码问题,如果把要打开的文件保存成utf-8编码格式就不会了。当然,如果文件比较多的手动去改会死人的,所以花了一点时间写了个工具。源代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Collections;
  11. namespace fileToUTF_8
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         string savePath = "";
  16.         ArrayList fileList = new ArrayList();
  17.         ArrayList fileNameList = new ArrayList();
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.         // File.WriteAllText(@"c:/test.html", File.ReadAllText(@"c:/index.html", Encoding.Default), Encoding.UTF8);
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             //拷贝目录
  26.             if (txtFolerPath.Text != txtSavePath.Text)
  27.             {
  28.                 CopyDirectory(txtFolerPath.Text , txtSavePath.Text);
  29.             }
  30.             savePath = txtSavePath.Text;
  31.            string temp="";
  32.             for (int i = 0; i < fileList.Count; i++)
  33.             {
  34.                 string tempSavePath="";
  35.                 string tempSourcePath = "";
  36.                 tempSavePath =savePath +@"/";
  37.                 tempSavePath += fileNameList[i].ToString();
  38.                 tempSourcePath = fileList[i].ToString();
  39.                 //tempSourcePath += fileNameList[i].ToString();
  40.                 temp=tempSourcePath ;
  41.                 tempSavePath = temp.Replace(txtFolerPath.Text, txtSavePath.Text);
  42.                 
  43.                 try
  44.                 {
  45.                     //该方法的第一个参数为保存文件的完全限定名路径,该路径必须存在,否则跳出DirectoryNotFoundException异常
  46.                     File.WriteAllText(tempSavePath, File.ReadAllText(tempSourcePath, Encoding.Default), Encoding.UTF8);
  47.                 }
  48.                 catch (DirectoryNotFoundException e1)
  49.                 {
  50.                     MessageBox.Show(e1.ToString());
  51.                 }
  52.                 finally
  53.                 { 
  54.                     
  55.                 }
  56.             }
  57.             MessageBox.Show("操作完毕");
  58.             
  59.         }
  60.         /******************************************************************************************************************/
  61.         private void GetFolder(DirectoryInfo dInfo)
  62.         {
  63.             //显示其中文件
  64.             GetFile(dInfo);
  65.             //遍历文件夹中的文件夹
  66.             foreach (DirectoryInfo dir in dInfo.GetDirectories())
  67.             {
  68.                 //递归遍历该文件夹
  69.                 GetFolder(dir);
  70.             }
  71.         }
  72.         /*-----*/
  73.         private void GetFile(DirectoryInfo dInfo)
  74.         {
  75.             try
  76.             {
  77.                 //遍历文件夹中的文件
  78.                 foreach (FileInfo file in dInfo.GetFiles())
  79.                 {
  80.                     if (".html.htm.js".IndexOf(file.Extension, StringComparison.CurrentCultureIgnoreCase) >= 0)
  81.                     {
  82.                         fileList.Add(file.FullName);
  83.                         fileNameList.Add(file.Name);
  84.                     }
  85.                 }
  86.             }
  87.             catch (Exception gFexe)
  88.             {
  89.                 MessageBox.Show(gFexe.ToString());
  90.                 return;
  91.             }
  92.             finally
  93.             {
  94.             }
  95.         }
  96.         private void btnOpenFolder_Click(object sender, EventArgs e)
  97.         {
  98.             if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  99.             {
  100.                 DirectoryInfo dInfo = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
  101.                 GetFolder(dInfo);
  102.                 txtFolerPath.Text = folderBrowserDialog1.SelectedPath;
  103.                 txtSavePath.Text = folderBrowserDialog1.SelectedPath;
  104.                 savePath = txtSavePath.Text;
  105.                 lblMsg.Text = "警告:当保存路径和源文件路径一样时将覆盖源文件!不推荐!";
  106.                 lblMsg.ForeColor = Color.Red;
  107.                 btnSavePath.Enabled = true;
  108.                 btnCastEncode.Enabled = true;
  109.             }
  110.         }
  111.         private void Form1_Load(object sender, EventArgs e)
  112.         {
  113.             lblMsg.Text = "";
  114.         }
  115.         private void btnSavePath_Click(object sender, EventArgs e)
  116.         {
  117.             if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  118.             {
  119.                 txtSavePath.Text = folderBrowserDialog1.SelectedPath;
  120.                 savePath = txtSavePath.Text;
  121.                 if (txtSavePath.Text != txtFolerPath.Text)
  122.                 {
  123.                     lblMsg.Text = "";
  124.                 }
  125.                 else
  126.                 {
  127.                     lblMsg.Text = "警告:当保存路径和源文件路径一样时将覆盖源文件!不推荐!";
  128.                 }
  129.             }
  130.         }
  131.       
  132.         private void CopyDirectory(string srcDir, string tgtDir)
  133.         {
  134.             DirectoryInfo source = new DirectoryInfo(srcDir);
  135.             DirectoryInfo target = new DirectoryInfo(tgtDir);
  136.             if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase))
  137.             {
  138.                 throw new Exception("父目录不能拷贝到子目录!");
  139.             }
  140.             if (!source.Exists)
  141.             {
  142.                 return;
  143.             }
  144.             if (!target.Exists)
  145.             {
  146.                 target.Create();
  147.             }
  148.             FileInfo[] files = source.GetFiles();
  149.             for (int i = 0; i < files.Length; i++)
  150.             {
  151.                 File.Copy(files[i].FullName, target.FullName + @"/" + files[i].Name, true);
  152.             }
  153.             DirectoryInfo[] dirs = source.GetDirectories();
  154.             for (int j = 0; j < dirs.Length; j++)
  155.             {
  156.                 CopyDirectory(dirs[j].FullName, target.FullName + @"/" + dirs[j].Name);
  157.             }
  158.         }
  159.     }
  160. }

 

抱歉!评论已关闭.