using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace Dds2Tga
{
public partial class Form : System.Windows.Forms.Form
{
List<string> _folderList = new List<string>();
public Form()
{
InitializeComponent();
}
private void Btn_OK(object sender, EventArgs e)
{
if (txt_path.Text == "")
{
return;
}
listBox1.Items.Clear();
progressBar.Value = 0;
FindFile(txt_path.Text);
listBox1.Items.Add("---------------- Count : " + listBox1.Items.Count + " ----------------");
int pbv = 100 / listBox1.Items.Count;
for (int i = 0; i < listBox1.Items.Count - 1; i++)
{
ModifyMtlFile(listBox1.Items[i].ToString());
//-----------------------------------------
ModifyMtlFile(listBox1.Items[i].ToString());
//-----------------------------------------
progressBar.Value += pbv;
}
progressBar.Value = 100;
MessageBox.Show("你是最棒的!");
}
private void Btn_filePath_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择文件路径";
DialogResult result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string folderPath = dialog.SelectedPath.Trim();
txt_path.Text = folderPath;
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("-------------------------------------");
}
/// <summary>
/// 获取所有文件.
/// </summary>
/// <param name="dirPath"></param>
public void FindFile(string dirPath) //参数dirPath为指定的目录
{
//在指定目录及子目录下查找文件,在listBox1中列出子目录及文件
DirectoryInfo Dir = new DirectoryInfo(dirPath);
try
{
foreach (FileInfo f in Dir.GetFiles("*.mtl")) //查找文件
{
listBox1.Items.Add(Dir + "\\" + f.ToString()); //listBox1中填加文件名
}
foreach (DirectoryInfo d in Dir.GetDirectories())//查找子目录
{
FindFile(Dir + "\\" + d.ToString());
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private void ModifyMtlFile(string path)
{
string loadTex = Read(path);
if (loadTex == "") return;
string str = ReplaceString(loadTex, tb_pattern.Text, tb_replace.Text);
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Write);
StreamWriter sr = new StreamWriter(fs);
sr.Write(str);
sr.Close();
fs.Close();
}
public string Read(string path)
{
try
{
return File.ReadAllText(path);
}
catch (IOException e)
{
MessageBox.Show(e.Message);
}
return "";
}
public string ReplaceString(string strInput, string strPattern, string strReplace)
{
string str = "";
try
{
str = System.Text.RegularExpressions.Regex.Replace(strInput, strPattern, strReplace);
}
catch (IOException e)
{
MessageBox.Show(e.Message);
}
return str;
}
}
}