class FileControl
{
private string path = "";
private DirectoryInfo dirInfo = null;
private FileInfo[] fileInfo = null;
private string msg = ""; public string Msg
{
get { return msg; }
set { msg = value; }
} public string Path {
get { return path; }
set { path = value; }
} public FileControl()
{
}
public FileControl(string path)
{
this.dirInfo = new DirectoryInfo(path);
} public void getDirInfo(string path)
{
this.dirInfo = new DirectoryInfo(path);
}
private void getFileInfoArr()
{
if (this.dirInfo != null)
{
this.fileInfo = this.dirInfo.GetFiles("*.txt", SearchOption.AllDirectories);
}
else
{
this.msg = "无可转换的txt文件";
}
} public void Transform()
{ try
{
this.getFileInfoArr();
foreach (FileInfo fi in this.fileInfo)
{
FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read);
StreamReader streamReader = new StreamReader(fs);
string newFileName = fi.FullName.Split('.')[fi.FullName.Split('.').Length - 2] + "_.txt";
File.WriteAllText(newFileName, streamReader.ReadToEnd(), Encoding.UTF8);
}
this.msg = "转换完美完成!";
}
catch (Exception ex)
{
this.msg = "转换发生异常!异常原因:"+ex.Message;
}
} }这是我写的一个转换的类 Transform就是转换的方法
{
private string path = "";
private DirectoryInfo dirInfo = null;
private FileInfo[] fileInfo = null;
private string msg = ""; public string Msg
{
get { return msg; }
set { msg = value; }
} public string Path {
get { return path; }
set { path = value; }
} public FileControl()
{
}
public FileControl(string path)
{
this.dirInfo = new DirectoryInfo(path);
} public void getDirInfo(string path)
{
this.dirInfo = new DirectoryInfo(path);
}
private void getFileInfoArr()
{
if (this.dirInfo != null)
{
this.fileInfo = this.dirInfo.GetFiles("*.txt", SearchOption.AllDirectories);
}
else
{
this.msg = "无可转换的txt文件";
}
} public void Transform()
{ try
{
this.getFileInfoArr();
foreach (FileInfo fi in this.fileInfo)
{
FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read);
StreamReader streamReader = new StreamReader(fs);
string newFileName = fi.FullName.Split('.')[fi.FullName.Split('.').Length - 2] + "_.txt";
File.WriteAllText(newFileName, streamReader.ReadToEnd(), Encoding.UTF8);
}
this.msg = "转换完美完成!";
}
catch (Exception ex)
{
this.msg = "转换发生异常!异常原因:"+ex.Message;
}
} }这是我写的一个转换的类 Transform就是转换的方法
{
//把原来的转换为byte数组,而且是utf_8编码的
Byte[] change = System.Text.Encoding.UTF8.GetBytes(UtfString.ToCharArray());
//用convet直接转换
Byte[]changde= Encoding.Convert(System.Text.Encoding.UTF8, System.Text.Encoding.ASCII, change);
return change.ToString(); }