天天看點

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. }

繼續閱讀