积极答复者
C# 批量改写记事本文件问题 小白搞了两天没有搞定 求助 代码怎么才能运行??

问题
-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace txt文件批量改写拷贝
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//存储文件的信息全路径
List<string> listPath = new List<string>();
double BSX;
double BSY;
double JCXA;
int XA_H;
double JCYA;
int YA_H;
double JCXE;
int XE_H;
double JCYE;
int YE_H;
int JCT;
private void b_PiLiangKaoBei_mpr_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"Z:\";
ofd.Filter = "Mpr文件|*.txt|所有文件|*.*";
ofd.Title = "请选择要批量转换的txt文件";
ofd.Multiselect=true;
ofd.ShowDialog();
//获得选择的文件全路径
string[] path = ofd.FileNames;
for (int i =0; i<path.Length;i++ )
{
//listPath.Add(path[i]);
//listBox1.Items.Add(Path.GetFileName(path[i]));
string[] mprstr = File.ReadAllLines(path[i],Encoding.Default );
for (int impr = 0; impr < mprstr.LongLength; impr++)
{
if (mprstr[impr].ToString().Contains("_BSX"))
{
string[] strNew = mprstr[impr].Split(new char[] { '=' });
string[] strNew2 = mprstr[impr+1].Split(new char[] { '=' });
//MessageBox.Show((strNew[1]));
BSX = int.Parse(strNew[1]);
BSY = int.Parse(strNew2[1]);
break;
}
if (mprstr[impr].ToString().Contains("<109"))
{
for (int mi = impr+1; mi < mprstr.LongLength; mi++)
{
if (mprstr[mi].ToString().Contains("XA="))
{
string[] strNew3 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
JCXA = int.Parse(strNew3[1]);
XA_H = mi;
continue;
}
if (mprstr[mi].ToString().Contains("YA="))
{
string[] strNew4 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
JCYA = int.Parse(strNew4[1]);
YA_H = mi;
continue;
}
if (mprstr[mi].ToString().Contains("XE="))
{
string[] strNew5 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
JCXE = int.Parse(strNew5[1]);
XE_H = mi;
continue;
}
if (mprstr[mi].ToString().Contains("YE="))
{
string[] strNew6 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
JCYE = int.Parse(strNew6[1]);
YE_H = mi;
continue;
}
if (mprstr[mi].ToString().Contains("T_="))
{
string[] strNew7 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
JCT = int.Parse(strNew7[1]);
break;
}
}
if (JCT == 40)
{
if (JCXA > 0 && JCXA < 4)
{
mprstr[XA_H] = "XA='4'";
}
else if (BSX - JCXA > 0 && BSX - JCXA < 4)
{
mprstr[XA_H] = "XA='" + (BSX - 4).ToString() + "'";
}
else
{
}
if (JCXE > 0 && JCXE < 4)
{
mprstr[XE_H] = "XE='4'";
}
else if (BSX - JCXE > 0 && BSX - JCXE < 4)
{
mprstr[XE_H] = "XE='" + (BSX - 4).ToString() + "'";
}
else
{
}
}
else if (JCT == 41)
{
if (JCYA > 0 && JCYA < 4)
{
mprstr[YA_H] = "YA='4'";
}
else if (BSY - JCYA > 0 && BSY - JCYA < 4)
{
mprstr[YA_H] = "YA='" + (BSY - 4).ToString() + "'";
}
else
{
}
if (JCYE > 0 && JCYE < 4)
{
mprstr[YE_H] = "YE='4'";
}
else if (BSY - JCYE > 0 && BSY - JCYE < 4)
{
mprstr[YE_H] = "YE='" + (BSX - 4).ToString() + "'";
}
else
{
}
}
else
{
}
BSX=0;
BSY=0;
JCXA=0;
XA_H=0;
JCYA=0;
YA_H=0;
JCXE=0;
XE_H=0;
JCYE=0;
YE_H=0;
JCT=0;
}
}
string a = textBox1.Text;
File.WriteAllLines(@"D:\NC\" + Path.GetFileName(path[i]), mprstr);
MessageBox.Show("OK");
}
}
}
}记事本文件如下:
[H
VERSION="4.0 Alpha"
OP="4"
FM="1"
GP="0"
UP="0"
DW="0"
FW="600"
_BSX=480.00
_BSY=414.00
_BSZ=18.00
[001
SIDE1="1"
KM=""
FNX_EXP="0"
KM=""
FNY_EXP="0"
KM=""
AX_EXP="0"
KM=""
AY_EXP="0"
KM=""
l="480"
KM=""
b="414"
KM=""
d="18"
KM=""
ShowNonM="_nonmirror"
KM=""
ShowM="_mirror"
KM=""
ModusMir="0"
KM=""
bohrver="1"
KM=""
bohrhor="1"
KM=""
bohruni="1"
KM=""
bohren="1"
KM=""
fraesen="1"
KM=""
fsaegen="1"
KM=""
saegen="1"
KM=""
nuten="1"
KM=""
ktasche="1"
KM=""
rtasche="1"
KM=""
abblas="1"
KM=""
leimen="1"
KM=""
kappen="1"
KM=""
bfraesen="1"
KM=""
ziehkl="1"
KM=""
Platz1_8="_mirror AND _ymirror OR _nonmirror AND _nonymirror"
KM=""
Platz4_5="_mirror AND _nonymirror OR _ymirror AND _nonmirror"
KM=""
<100 \Werkstck\
LA="l"
BR="b"
DI="d"
FNX="FNX_EXP"
FNY="FNY_EXP"
RNX="0"
RNY="0"
RNZ="0"
AX="AX_EXP"
AY="AY_EXP"
<101 \Kommentar\
KM="MPR_PP VER: : 11,0,2,25 "
KM="Ordername : Y810001_1_3 "
KM="Name : 底板 "
KM="Date : 15.10.18 "
KM="Material : GTB_18MM "
KM="Surftop : "
KM="Surfbot: "
KM="Material with grain !! "
<103 \BohrHoriz\
??="bohrhor=1 "
BM="YP"
XA="80"
YA="0"
ZA="d/2"
DU="8"
TI="32"
AN="1"
AB="32"
<103 \BohrHoriz\
??="bohrhor=1 "
BM="YP"
XA="112"
YA="0"
ZA="d/2"
DU="8"
TI="32"
AN="1"
AB="32"
<103 \BohrHoriz\
??="bohrhor=1 "
BM="YP"
XA="240"
YA="0"
ZA="d/2"
DU="8"
TI="32"
AN="1"
AB="32"
<103 \BohrHoriz\
??="bohrhor=1 "
BM="YP"
XA="368"
YA="0"
ZA="d/2"
DU="8"
TI="32"
AN="1"
AB="32"
<103 \BohrHoriz\
??="bohrhor=1 "
BM="YP"
XA="400"
YA="0"
ZA="d/2"
DU="8"
TI="32"
AN="1"
AB="32"
<103 \BohrHoriz\
??="bohrhor=1 "
BM="YM"
XA="80"
YA="414"
ZA="d/2"
DU="8"
TI="32"
AN="1"
AB="32"
<103 \BohrHoriz\
??="bohrhor=1 "
BM="YM"
XA="112"
YA="414"
ZA="d/2"
DU="8"
TI="32"
AN="1"
AB="32"
<103 \BohrHoriz\
??="bohrhor=1 "
BM="YM"
XA="240"
YA="414"
ZA="d/2"
DU="8"
TI="32"
AN="1"
AB="32"
<103 \BohrHoriz\
??="bohrhor=1 "
BM="YM"
XA="368"
YA="414"
ZA="d/2"
DU="8"
TI="32"
AN="1"
AB="32"
<103 \BohrHoriz\
??="bohrhor=1 "
BM="YM"
XA="400"
YA="414"
ZA="d/2"
DU="8"
TI="32"
AN="1"
AB="32"
<109 \Nuten\
??="saegen=1 "
OP="1"
XA="22"
YA="-30"
XE="22"
YE="444"
T_="41"
NB="6"
TI="6"
TV="0"
MV="GL"
EM="MOD1"
MN="GL"
XY="90.000000"
RK="NoWRK"
F_="10"
!
就是想编写一个程序实现批量判断记事本中的特定信息的值是否在范围内,如果在就改一下值的大小,改完后把新
文件并保存到新路径下,不改变文件顺序
- 已编辑 willi2019 2019年1月27日 12:48
答案
-
你好,
由于BSX, BSY, 都是double, 我建议你在转换的时候也转成double类型的,把下面的这两行代码修改一下就可以了(看一下加粗的代码)。
public partial class Form1 : Form { public Form1() { InitializeComponent(); } //存储文件的信息全路径 List<string> listPath = new List<string>(); double BSX; double BSY; double JCXA; int XA_H; double JCYA; int YA_H; double JCXE; int XE_H; double JCYE; int YE_H; int JCT; private void b_PiLiangKaoBei_mpr_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = @"Z:\"; ofd.Filter = "Mpr文件|*.txt|所有文件|*.*"; ofd.Title = "请选择要批量转换的txt文件"; ofd.Multiselect=true; ofd.ShowDialog(); //获得选择的文件全路径 string[] path = ofd.FileNames; for (int i =0; i<path.Length;i++ ) { //listPath.Add(path[i]); //listBox1.Items.Add(Path.GetFileName(path[i])); string[] mprstr = File.ReadAllLines(path[i],Encoding.Default ); for (int impr = 0; impr < mprstr.LongLength; impr++) { if (mprstr[impr].ToString().Contains("_BSX")) { string[] strNew = mprstr[impr].Split(new char[] { '=' }); string[] strNew2 = mprstr[impr+1].Split(new char[] { '=' }); //MessageBox.Show((strNew[1])); // BSX = int.Parse(strNew[1]); //BSY = int.Parse(strNew2[1]); //使用下面的这两行代码转换成double类型的。 double.TryParse(strNew[1], out BSX); double.TryParse(strNew2[1],out BSY); break; } if (mprstr[impr].ToString().Contains("<109")) { for (int mi = impr+1; mi < mprstr.LongLength; mi++) { if (mprstr[mi].ToString().Contains("XA=")) { string[] strNew3 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); JCXA = int.Parse(strNew3[1]); XA_H = mi; continue; } if (mprstr[mi].ToString().Contains("YA=")) { string[] strNew4 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); JCYA = int.Parse(strNew4[1]); YA_H = mi; continue; } if (mprstr[mi].ToString().Contains("XE=")) { string[] strNew5 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); JCXE = int.Parse(strNew5[1]); XE_H = mi; continue; } if (mprstr[mi].ToString().Contains("YE=")) { string[] strNew6 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); JCYE = int.Parse(strNew6[1]); YE_H = mi; continue; } if (mprstr[mi].ToString().Contains("T_=")) { string[] strNew7 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); JCT = int.Parse(strNew7[1]); break; } } if (JCT == 40) { if (JCXA > 0 && JCXA < 4) { mprstr[XA_H] = "XA='4'"; } else if (BSX - JCXA > 0 && BSX - JCXA < 4) { mprstr[XA_H] = "XA='" + (BSX - 4).ToString() + "'"; } else { } if (JCXE > 0 && JCXE < 4) { mprstr[XE_H] = "XE='4'"; } else if (BSX - JCXE > 0 && BSX - JCXE < 4) { mprstr[XE_H] = "XE='" + (BSX - 4).ToString() + "'"; } else { } } else if (JCT == 41) { if (JCYA > 0 && JCYA < 4) { mprstr[YA_H] = "YA='4'"; } else if (BSY - JCYA > 0 && BSY - JCYA < 4) { mprstr[YA_H] = "YA='" + (BSY - 4).ToString() + "'"; } else { } if (JCYE > 0 && JCYE < 4) { mprstr[YE_H] = "YE='4'"; } else if (BSY - JCYE > 0 && BSY - JCYE < 4) { mprstr[YE_H] = "YE='" + (BSX - 4).ToString() + "'"; } else { } } else { } BSX=0; BSY=0; JCXA=0; XA_H=0; JCYA=0; YA_H=0; JCXE=0; XE_H=0; JCYE=0; YE_H=0; JCT=0; } } string a = textBox1.Text; File.WriteAllLines(@"D:\NC\" + Path.GetFileName(path[i]), mprstr); MessageBox.Show("OK"); } } }
Best regards,
Zhanglong
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已建议为答案 Zhanglong WuMicrosoft contingent staff, Moderator 2019年2月5日 7:05
- 已标记为答案 willi2019 2019年2月10日 3:13
全部回复
-
你好,
由于BSX, BSY, 都是double, 我建议你在转换的时候也转成double类型的,把下面的这两行代码修改一下就可以了(看一下加粗的代码)。
public partial class Form1 : Form { public Form1() { InitializeComponent(); } //存储文件的信息全路径 List<string> listPath = new List<string>(); double BSX; double BSY; double JCXA; int XA_H; double JCYA; int YA_H; double JCXE; int XE_H; double JCYE; int YE_H; int JCT; private void b_PiLiangKaoBei_mpr_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = @"Z:\"; ofd.Filter = "Mpr文件|*.txt|所有文件|*.*"; ofd.Title = "请选择要批量转换的txt文件"; ofd.Multiselect=true; ofd.ShowDialog(); //获得选择的文件全路径 string[] path = ofd.FileNames; for (int i =0; i<path.Length;i++ ) { //listPath.Add(path[i]); //listBox1.Items.Add(Path.GetFileName(path[i])); string[] mprstr = File.ReadAllLines(path[i],Encoding.Default ); for (int impr = 0; impr < mprstr.LongLength; impr++) { if (mprstr[impr].ToString().Contains("_BSX")) { string[] strNew = mprstr[impr].Split(new char[] { '=' }); string[] strNew2 = mprstr[impr+1].Split(new char[] { '=' }); //MessageBox.Show((strNew[1])); // BSX = int.Parse(strNew[1]); //BSY = int.Parse(strNew2[1]); //使用下面的这两行代码转换成double类型的。 double.TryParse(strNew[1], out BSX); double.TryParse(strNew2[1],out BSY); break; } if (mprstr[impr].ToString().Contains("<109")) { for (int mi = impr+1; mi < mprstr.LongLength; mi++) { if (mprstr[mi].ToString().Contains("XA=")) { string[] strNew3 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); JCXA = int.Parse(strNew3[1]); XA_H = mi; continue; } if (mprstr[mi].ToString().Contains("YA=")) { string[] strNew4 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); JCYA = int.Parse(strNew4[1]); YA_H = mi; continue; } if (mprstr[mi].ToString().Contains("XE=")) { string[] strNew5 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); JCXE = int.Parse(strNew5[1]); XE_H = mi; continue; } if (mprstr[mi].ToString().Contains("YE=")) { string[] strNew6 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); JCYE = int.Parse(strNew6[1]); YE_H = mi; continue; } if (mprstr[mi].ToString().Contains("T_=")) { string[] strNew7 = mprstr[mi].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); JCT = int.Parse(strNew7[1]); break; } } if (JCT == 40) { if (JCXA > 0 && JCXA < 4) { mprstr[XA_H] = "XA='4'"; } else if (BSX - JCXA > 0 && BSX - JCXA < 4) { mprstr[XA_H] = "XA='" + (BSX - 4).ToString() + "'"; } else { } if (JCXE > 0 && JCXE < 4) { mprstr[XE_H] = "XE='4'"; } else if (BSX - JCXE > 0 && BSX - JCXE < 4) { mprstr[XE_H] = "XE='" + (BSX - 4).ToString() + "'"; } else { } } else if (JCT == 41) { if (JCYA > 0 && JCYA < 4) { mprstr[YA_H] = "YA='4'"; } else if (BSY - JCYA > 0 && BSY - JCYA < 4) { mprstr[YA_H] = "YA='" + (BSY - 4).ToString() + "'"; } else { } if (JCYE > 0 && JCYE < 4) { mprstr[YE_H] = "YE='4'"; } else if (BSY - JCYE > 0 && BSY - JCYE < 4) { mprstr[YE_H] = "YE='" + (BSX - 4).ToString() + "'"; } else { } } else { } BSX=0; BSY=0; JCXA=0; XA_H=0; JCYA=0; YA_H=0; JCXE=0; XE_H=0; JCYE=0; YE_H=0; JCT=0; } } string a = textBox1.Text; File.WriteAllLines(@"D:\NC\" + Path.GetFileName(path[i]), mprstr); MessageBox.Show("OK"); } } }
Best regards,
Zhanglong
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已建议为答案 Zhanglong WuMicrosoft contingent staff, Moderator 2019年2月5日 7:05
- 已标记为答案 willi2019 2019年2月10日 3:13