📄 frmmain.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ReplaceStrInFiles
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
/// <summary>
/// 获取需要替换字符的文件所在的文件夹路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnFolderDia_Click(object sender, EventArgs e)
{
FolderDia = new FolderBrowserDialog();
if (FolderDia.ShowDialog() == DialogResult.OK)
{
txtFolderName.Text = FolderDia.SelectedPath;
}
}
/// <summary>
///
/// </summary>
/// <param name="strFolderName">文件夹目录</param>
/// <param name="strOrigin">需替换的原始字符</param>
/// <param name="strTarget">替换后的字符</param>
void SearchReplaceInFile(string strFolderName, string strOrigin, string strTarget)
{
DirectoryInfo DirFolder = new DirectoryInfo(strFolderName);//根目录名称
FileInfo[] fis = DirFolder.GetFiles();//找到该目录下所有文件
///////////////////begin for/////////////////////
foreach (FileInfo fi in fis) //循环
{
StringBuilder sb = new StringBuilder();
StreamReader Readfile = new StreamReader(fi.FullName, Encoding.GetEncoding("GB2312"));
string strAll = "";
while ((strAll = Readfile.ReadLine()) != null)
{
int nPos = strAll.IndexOf(strOrigin);
if (nPos != -1)
{
strAll = strAll.Replace(strOrigin, strTarget); //替换
}
sb.Append(strAll + "\r\n");//将替换后的所有写入到StringBuilder中.
}//end while
Readfile.Close();
//如果是要替换的文件,将StringBuilder回写到文件中
StreamWriter Writefile = new StreamWriter(fi.FullName, false, Encoding.GetEncoding("GB18030"));
Writefile.Write(sb);
Writefile.Close();
}
///////////////////////////end for////////////////////////////////////
//递归调用
foreach (DirectoryInfo nextFolder in DirFolder.GetDirectories())
{
SearchReplaceInFile(nextFolder.FullName, strOrigin, strTarget);
}
}
/// <summary>
/// 直接将路径名去掉,只要纹理名
/// </summary>
/// <param name="strFolderName"></param>
void QuickReplaceInFile(string strFolderName)
{
DirectoryInfo DirFolder = new DirectoryInfo(strFolderName);//根目录名称
FileInfo[] fis = DirFolder.GetFiles();//找到该目录下所有文件
///////////////////begin for/////////////////////
foreach (FileInfo fi in fis) //循环
{
StringBuilder sb = new StringBuilder();
StreamReader Readfile = new StreamReader(fi.FullName, Encoding.GetEncoding("GB2312"));
string strAll = "";
while ((strAll = Readfile.ReadLine()) != null)
{
string strRe = "";
GetFileName(strAll,ref strRe);
if (strRe.Length>4)
{
strAll = strAll.Replace(strAll, strRe); //替换
}
sb.Append(strAll + "\r\n");//将替换后的所有写入到StringBuilder中.
}//end while
Readfile.Close();
//如果是要替换的文件,将StringBuilder回写到文件中
StreamWriter Writefile = new StreamWriter(fi.FullName, false, Encoding.GetEncoding("GB18030"));
Writefile.Write(sb);
Writefile.Close();
}
///////////////////////////end for////////////////////////////////////
//递归调用
foreach (DirectoryInfo nextFolder in DirFolder.GetDirectories())
{
QuickReplaceInFile(nextFolder.FullName);
}
}
/// <summary>
/// 开始替换
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnStartReplace_Click(object sender, EventArgs e)
{
try
{
string folderPath = txtFolderName.Text;
DirectoryInfo theFolder = new DirectoryInfo(folderPath);//目录名称
if (theFolder.Exists)
{
if (txtOriginStr.Text == "")
{
MessageBox.Show("请输入需要替换的字符!");
return;
}
SearchReplaceInFile(theFolder.FullName,txtOriginStr.Text,txtTargetStr.Text);
}
MessageBox.Show("替换成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//将纹理名提取出来
private void GetFileName(string strDir, ref string strResult)
{
int nIndex = strDir.LastIndexOf('\\');
int nPos = strDir.LastIndexOf('.');
if (nIndex>=0&& nPos>nIndex)
{
int nStart = nIndex + 1;
//strResult = strDir.Substring(nIndex, nPos);
strResult = strDir.Substring(nStart);
}
}
/// <summary>
/// 快速替换字符为文件名,不包括路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnQuickReplace_Click(object sender, EventArgs e)
{
try
{
string folderPath = txtFolderName.Text;
DirectoryInfo theFolder = new DirectoryInfo(folderPath);//目录名称
if (theFolder.Exists)
{
QuickReplaceInFile(theFolder.FullName);
}
MessageBox.Show("替换成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 修改坐标值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnModifyCoord_Click(object sender, EventArgs e)
{
try
{
string folderPath = txtFolderName.Text;
DirectoryInfo theFolder = new DirectoryInfo(folderPath);//目录名称
if (theFolder.Exists)
{
ModifyCoord(theFolder.FullName);
}
MessageBox.Show("修改成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void ModifyCoord(string strFolderName)
{
DirectoryInfo DirFolder = new DirectoryInfo(strFolderName);//根目录名称
FileInfo[] fis = DirFolder.GetFiles();//找到该目录下所有文件
///////////////////begin for/////////////////////
foreach (FileInfo fi in fis) //循环
{
StringBuilder sb = new StringBuilder();
StreamReader Readfile = new StreamReader(fi.FullName, Encoding.GetEncoding("GB2312"));
int nLineNum = 0;//记录行数
int nAddNum = 0;
int nAddNum2 = 0;
string strAll = "";
while ((strAll = Readfile.ReadLine()) != null)
{
nLineNum++;
//string strOrigin = "Frame Line";
//int nPos = strAll.IndexOf(strOrigin);
//if (nPos != -1)
//{
// nAddNum = nLineNum + 5;
//}
//if (nLineNum==nAddNum)
//{
// string strRe = "0.000000,0.000000,0.000000,1.000000;;";
// strAll = strAll.Replace(strAll, strRe); //替换
//}
////sb.Append(strAll + "\r\n");//将替换后的所有写入到StringBuilder中.
//strOrigin = "Frame Group";
//nPos = strAll.IndexOf(strOrigin);
//if (nPos != -1)
//{
// nAddNum2= nLineNum + 5;
//}
//if (nLineNum == nAddNum2)
//{
// string strRe = "0.000000,0.000000,0.000000,1.000000;;";
// strAll = strAll.Replace(strAll, strRe); //替换
//}
//strOrigin = "Frame Plane";
//nPos = strAll.IndexOf(strOrigin);
//if (nPos != -1)
//{
// nAddNum2 = nLineNum + 5;
//}
//if (nLineNum == nAddNum2)
//{
// string strRe = "0.000000,0.000000,0.000000,1.000000;;";
// strAll = strAll.Replace(strAll, strRe); //替换
//}
////////////////////////////一次性全部替换//////////////////////
string strOrigin = "0.000000,0.000000,1.000000,0.000000,";
int nPos = strAll.IndexOf(strOrigin);
if (nPos != -1)
{
nAddNum = nLineNum + 1;
}
if (nLineNum==nAddNum)
{
string strRe = "0.000000,0.000000,0.000000,1.000000;;";
strAll = strAll.Replace(strAll, strRe); //替换
}
sb.Append(strAll + "\r\n");//将替换后的所有写入到StringBuilder中.
}//end while
Readfile.Close();
//如果是要替换的文件,将StringBuilder回写到文件中
StreamWriter Writefile = new StreamWriter(fi.FullName, false, Encoding.GetEncoding("GB18030"));
Writefile.Write(sb);
Writefile.Close();
}
///////////////////////////end for////////////////////////////////////
//递归调用
foreach (DirectoryInfo nextFolder in DirFolder.GetDirectories())
{
ModifyCoord(nextFolder.FullName);
}
}
private void FrmMain_Load(object sender, EventArgs e)
{
this.txtOriginStr.Enabled = false;
this.txtTargetStr.Enabled = false;
this.BtnQuickReplace.Enabled = false;
this.BtnStartReplace.Enabled = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -