2016-02-25 41 views
0

我有讀入內存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 

所以對於標籤的示例第五行需要是FO43如果用戶輸入的10的偏移值,需要爲23,如果他進入-10作爲偏移值,我在處理TEXTFILES一個小白在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++) 
{ 
+0

@Gusman我試過你的解決方案,但FO值仍然相同,但你正確理解我的要求。 – CodeMan

回答

0

好吧,你問題非常模糊,你的代碼與你提出的問題沒有任何關係,但這可以幫助你找到你所要求的替代品。

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); 
+0

我試過,但在FO值仍相同,請幫助:使用(TextReader的讀者=新的StreamReader(內存)){ INT 偏移= Convert.ToInt32(txtOffSet.Text); string inputs = reader.ReadToEnd(); Regex r = new Regex(「\\^FO([0-9] *)」,RegexOptions.Singleline); 串解析度= r.Replace(Convert.ToString(讀取器),(輸入)=> 「^ FO」 +(int.Parse(input.Groups [1]。價值)+偏移量)的ToString()); // for(int i = 0; 1 CodeMan