天天看点

Aptana解决中文乱码工具

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 [email protected]"/";
  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.                 try
  43.                 {
  44.                     //该方法的第一个参数为保存文件的完全限定名路径,该路径必须存在,否则跳出DirectoryNotFoundException异常
  45.                     File.WriteAllText(tempSavePath, File.ReadAllText(tempSourcePath, Encoding.Default), Encoding.UTF8);
  46.                 }
  47.                 catch (DirectoryNotFoundException e1)
  48.                 {
  49.                     MessageBox.Show(e1.ToString());
  50.                 }
  51.                 finally
  52.                 { 
  53.                 }
  54.             }
  55.             MessageBox.Show("操作完毕");
  56.         }
  57.         private void GetFolder(DirectoryInfo dInfo)
  58.         {
  59.             //显示其中文件
  60.             GetFile(dInfo);
  61.             //遍历文件夹中的文件夹
  62.             foreach (DirectoryInfo dir in dInfo.GetDirectories())
  63.             {
  64.                 //递归遍历该文件夹
  65.                 GetFolder(dir);
  66.             }
  67.         }
  68.         private void GetFile(DirectoryInfo dInfo)
  69.         {
  70.             try
  71.             {
  72.                 //遍历文件夹中的文件
  73.                 foreach (FileInfo file in dInfo.GetFiles())
  74.                 {
  75.                     if (".html.htm.js".IndexOf(file.Extension, StringComparison.CurrentCultureIgnoreCase) >= 0)
  76.                     {
  77.                         fileList.Add(file.FullName);
  78.                         fileNameList.Add(file.Name);
  79.                     }
  80.                 }
  81.             }
  82.             catch (Exception gFexe)
  83.             {
  84.                 MessageBox.Show(gFexe.ToString());
  85.                 return;
  86.             }
  87.             finally
  88.             {
  89.             }
  90.         }
  91.         private void btnOpenFolder_Click(object sender, EventArgs e)
  92.         {
  93.             if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  94.             {
  95.                 DirectoryInfo dInfo = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
  96.                 GetFolder(dInfo);
  97.                 txtFolerPath.Text = folderBrowserDialog1.SelectedPath;
  98.                 txtSavePath.Text = folderBrowserDialog1.SelectedPath;
  99.                 savePath = txtSavePath.Text;
  100.                 lblMsg.Text = "警告:当保存路径和源文件路径一样时将覆盖源文件!不推荐!";
  101.                 lblMsg.ForeColor = Color.Red;
  102.                 btnSavePath.Enabled = true;
  103.                 btnCastEncode.Enabled = true;
  104.             }
  105.         }
  106.         private void Form1_Load(object sender, EventArgs e)
  107.         {
  108.             lblMsg.Text = "";
  109.         }
  110.         private void btnSavePath_Click(object sender, EventArgs e)
  111.         {
  112.             if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  113.             {
  114.                 txtSavePath.Text = folderBrowserDialog1.SelectedPath;
  115.                 savePath = txtSavePath.Text;
  116.                 if (txtSavePath.Text != txtFolerPath.Text)
  117.                 {
  118.                     lblMsg.Text = "";
  119.                 }
  120.                 else
  121.                 {
  122.                     lblMsg.Text = "警告:当保存路径和源文件路径一样时将覆盖源文件!不推荐!";
  123.                 }
  124.             }
  125.         }
  126.         private void CopyDirectory(string srcDir, string tgtDir)
  127.         {
  128.             DirectoryInfo source = new DirectoryInfo(srcDir);
  129.             DirectoryInfo target = new DirectoryInfo(tgtDir);
  130.             if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase))
  131.             {
  132.                 throw new Exception("父目录不能拷贝到子目录!");
  133.             }
  134.             if (!source.Exists)
  135.             {
  136.                 return;
  137.             }
  138.             if (!target.Exists)
  139.             {
  140.                 target.Create();
  141.             }
  142.             FileInfo[] files = source.GetFiles();
  143.             for (int i = 0; i < files.Length; i++)
  144.             {
  145.                 File.Copy(files[i].FullName, target.FullName + @"/" + files[i].Name, true);
  146.             }
  147.             DirectoryInfo[] dirs = source.GetDirectories();
  148.             for (int j = 0; j < dirs.Length; j++)
  149.             {
  150.                 CopyDirectory(dirs[j].FullName, target.FullName + @"/" + dirs[j].Name);
  151.             }
  152.         }
  153.     }
  154. }

继续阅读