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 +@"/";
- 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);
- }
- }
- }
- }