从文本文件中获取特定值不起作用
本文关键字:不起作用 获取 文本 文件 | 更新日期: 2023-09-27 18:36:11
我有一个 zpllabel.txt 文件,我读入内存,但我想从文本文件中获取所有 ^FO 值,并将用户输入的值添加到其中以偏移打印机。
^XA^MCY^PRD,D,D^MMT,N^XZ
^XA
^DFCASENEW^FS
^LH0,0
^FO33,45^A0N,90,98^FN01^FS^FX Company^FS
^FO879,28^A0N,263,214^FN02^FS^FX Product^FS
^FO30,159^A0N,90,58^FN03^FS^FX Description^FS
^FO42,321^A0N,65,68^FN04^FS^FX NumberIn^FS
^FO100,436^A0N,75,66^FN05^FS^FX SelectCode^FS
^BY6,,230^FO260,252^BCN,,N,N,N,N^FN06^FS^FX RotationProduct^FS
^FO660,503^A0N,32,98^FN07^FS^FX RotationProduct^FS
^BY4,3.0,204^FO60,518^B2N,,N,N,N^FN08^FS^FX UPCCode^FS
^FO170,732^A0N,24,47^FN09^FS^FX UPCCode^FS
^FO32,516^GB590,0,10^FS
^FO32,717^GB590,0,10^FS
^FO640,573^A0N,226,212^FN10^FS^FX Rotation^FS
^FO1362,546^A0N,90,66^FDBest Before^FS
^FO1235,628^A0N,134,140^FN11^FS^FX BestBefore^FS
^FO55,391^A0N,43,77^FDSEL^FS
^FO30,267^A0N,54,100^FDQTY^FS
^XZ
^XA^XFCASENEW
^FN01^FDCompany^FS
^FN02^FDProduct^FS
^FN03^FDDescription^FS
^FN04^FDNumberIn^FS
^FN05^FDSelectCode^FS
^FN06^FDRotationProduct^FS
^FN07^FD^FS
^FN08^FDUPCCode^FS
^FN09^FD^FS
^FN10^FDRotation^FS
^FN11^FDBestBefore^FS
^PQ1,0,1,Y
^XZ
^XA^ID*.*^XZ
所以例如,如果用户输入偏移值为 10,标签的第 5 行需要为 FO43,如果他输入 -10 作为偏移值,则需要为 23,我是处理 C# 文本文件的菜鸟,请帮助
我有这段代码来替换一对文本:
try
{
byte[] file = File.ReadAllBytes("C:''Users''something''Documents''Visual Studio 2013''Projects''zplTest''zplTest''zpllabel3.txt");
using (MemoryStream memory = new MemoryStream(file))
{
using (TextReader reader = new StreamReader(memory))
{
string input = reader.ReadToEnd();
// for (int i = 0; 1 < file.Length; i++)
{
using (MemoryStream writermemory = new MemoryStream())
using (StreamWriter writer = new StreamWriter(writermemory))
{
Dictionary<string, string> replacementcollections =
new Dictionary<string, string>();
replacementcollections.Add
("^FN01^FDCompany^FS", compname);
replacementcollections.Add
("^FN02^FDProduct^FS", stlabel23);
replacementcollections.Add
("^FN03^FDDescription^FS", stlabel24);
replacementcollections.Add
("^FN04^FDNumberIn^FS", stlabel25);
replacementcollections.Add
("^FN05^FDSelectCode^FS", stlabel26);
replacementcollections.Add
("^FN06^FDRotationProduct^FS", stlabel27);
replacementcollections.Add
("^FN07^FD^FS", stlabel28);
replacementcollections.Add
("^FN08^FDUPCCode^FS", stlabel29.TrimEnd());
replacementcollections.Add
("^FN09^FD^FS", stlabel30.TrimEnd());
replacementcollections.Add
("^FN10^FDRotation^FS", "^FN10^FD" + TxtRotcode.Text.Trim() + "^FS");
replacementcollections.Add
("^FN11^FDBestBefore^FS", "^FN11^FD" + TxtBestBefore.Text.Trim() + "^FS");
replacementcollections.Add
("^PQ1,0,1,Y", "PQ" + TxtQty.Text.Trim() + ",0,1,Y");
string output = input;
foreach (KeyValuePair<string, string>
replacement in replacementcollections)
{ output = output.Replace(replacement.Key, replacement.Value); }
Label256.Visible = true;
zplcode.Text = output;
zplcode.Visible = true;
writer.Close();
ZplPreview();
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace.ToString());
Console.WriteLine(ex.Message.ToString());
}
那么我想完成什么1) 将文本文件读入内存2)找到"FO"的所有值3) 用用户输入的内容添加或减去该值4)将其发送到文本框5)我稍后将该文本发送到斑马打印机
我试过了:
using
(TextReader reader = new StreamReader(memory))
{
int offset = Convert.ToInt32(txtOffSet.Text);
string inputs = reader.ReadToEnd();
Regex r = new Regex("''^FO([0-9]*)", RegexOptions.Singleline);
string res = r.Replace(Convert.ToString(reader), (input) => "^FO" + (int.Parse(input.Groups[1].Value) + offset).ToString());
// for (int i = 0; 1 < file.Length; i++)
{
好的,你的问题真的很模糊,你的代码与你问的问题无关,但这可以帮助你进行你要求的替换。
string fileContent = File.ReadAllText("path/to/your/file");
int offset = 10; //Store here your offset, positive to add it, negative to subtract it
Regex r = new Regex("''^FO([0-9]*)", RegexOptions.Singleline);
string res = r.Replace(fileContent, (input) => "^FO" + (int.Parse(input.Groups[1].Value) + offset).ToString());
//Now you have the content of the file with the FO replaced with it's new values
Debug.Write(res);