Aptana(我使用的版本是Aptana Studio, build: 1.2.1.020234)打開以前的檔案發現對中文支援不好,出現了亂碼。原因是檔案的編碼問題,如果把要打開的檔案儲存成utf-8編碼格式就不會了。當然,如果檔案比較多的手動去改會死人的,是以花了一點時間寫了個工具。源代碼如下:
- 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;
- using System.IO;
- using System.Collections;
- namespace fileToUTF_8
- {
- public partial class Form1 : Form
- {
- string savePath = "";
- ArrayList fileList = new ArrayList();
- ArrayList fileNameList = new ArrayList();
- public Form1()
- {
- InitializeComponent();
- }
- // File.WriteAllText(@"c:/test.html", File.ReadAllText(@"c:/index.html", Encoding.Default), Encoding.UTF8);
- private void button1_Click(object sender, EventArgs e)
- {
- //拷貝目錄
- if (txtFolerPath.Text != txtSavePath.Text)
- {
- CopyDirectory(txtFolerPath.Text , txtSavePath.Text);
- }
- savePath = txtSavePath.Text;
- string temp="";
- for (int i = 0; i < fileList.Count; i++)
- {
- string tempSavePath="";
- string tempSourcePath = "";
- tempSavePath =savePath [email protected]"/";
- tempSavePath += fileNameList[i].ToString();
- tempSourcePath = fileList[i].ToString();
- //tempSourcePath += fileNameList[i].ToString();
- temp=tempSourcePath ;
- tempSavePath = temp.Replace(txtFolerPath.Text, txtSavePath.Text);
- try
- {
- //該方法的第一個參數為儲存檔案的完全限定名路徑,該路徑必須存在,否則跳出DirectoryNotFoundException異常
- File.WriteAllText(tempSavePath, File.ReadAllText(tempSourcePath, Encoding.Default), Encoding.UTF8);
- }
- catch (DirectoryNotFoundException e1)
- {
- MessageBox.Show(e1.ToString());
- }
- finally
- {
- }
- }
- MessageBox.Show("操作完畢");
- }
- private void GetFolder(DirectoryInfo dInfo)
- {
- //顯示其中檔案
- GetFile(dInfo);
- //周遊檔案夾中的檔案夾
- foreach (DirectoryInfo dir in dInfo.GetDirectories())
- {
- //遞歸周遊該檔案夾
- GetFolder(dir);
- }
- }
- private void GetFile(DirectoryInfo dInfo)
- {
- try
- {
- //周遊檔案夾中的檔案
- foreach (FileInfo file in dInfo.GetFiles())
- {
- if (".html.htm.js".IndexOf(file.Extension, StringComparison.CurrentCultureIgnoreCase) >= 0)
- {
- fileList.Add(file.FullName);
- fileNameList.Add(file.Name);
- }
- }
- }
- catch (Exception gFexe)
- {
- MessageBox.Show(gFexe.ToString());
- return;
- }
- finally
- {
- }
- }
- private void btnOpenFolder_Click(object sender, EventArgs e)
- {
- if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
- {
- DirectoryInfo dInfo = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
- GetFolder(dInfo);
- txtFolerPath.Text = folderBrowserDialog1.SelectedPath;
- txtSavePath.Text = folderBrowserDialog1.SelectedPath;
- savePath = txtSavePath.Text;
- lblMsg.Text = "警告:當儲存路徑和源檔案路徑一樣時将覆寫源檔案!不推薦!";
- lblMsg.ForeColor = Color.Red;
- btnSavePath.Enabled = true;
- btnCastEncode.Enabled = true;
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- lblMsg.Text = "";
- }
- private void btnSavePath_Click(object sender, EventArgs e)
- {
- if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
- {
- txtSavePath.Text = folderBrowserDialog1.SelectedPath;
- savePath = txtSavePath.Text;
- if (txtSavePath.Text != txtFolerPath.Text)
- {
- lblMsg.Text = "";
- }
- else
- {
- lblMsg.Text = "警告:當儲存路徑和源檔案路徑一樣時将覆寫源檔案!不推薦!";
- }
- }
- }
- private void CopyDirectory(string srcDir, string tgtDir)
- {
- DirectoryInfo source = new DirectoryInfo(srcDir);
- DirectoryInfo target = new DirectoryInfo(tgtDir);
- if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase))
- {
- throw new Exception("父目錄不能拷貝到子目錄!");
- }
- if (!source.Exists)
- {
- return;
- }
- if (!target.Exists)
- {
- target.Create();
- }
- FileInfo[] files = source.GetFiles();
- for (int i = 0; i < files.Length; i++)
- {
- File.Copy(files[i].FullName, target.FullName + @"/" + files[i].Name, true);
- }
- DirectoryInfo[] dirs = source.GetDirectories();
- for (int j = 0; j < dirs.Length; j++)
- {
- CopyDirectory(dirs[j].FullName, target.FullName + @"/" + dirs[j].Name);
- }
- }
- }
- }