将OBJ文件材质上的文理索引后缀从dds,tif改为tga

本文介绍了一个使用C#实现的应用程序,该程序可以批量搜索指定目录及其子目录下的所有.mtl文件,并对这些文件的内容进行统一替换操作。通过简单的用户界面,用户能够选择文件夹路径并指定替换模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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




}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值