从程序集里读取信息

本文介绍了一个基于C#的应用程序,该程序能够从指定的输入中生成并打印特定格式的电子标签。程序通过解析用户输入的位置信息,自动生成标签文本,并根据标签类型调用不同的模板进行打印。

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

本例主要是从内部程序集内读取一个文件,然后输入到指定目录

Code:
  1. class shelf   
  2.     {   
  3.         static void Main(string[] args)   
  4.         {   
  5.             string path = "c://PaperTag.xls";   
  6.             //判断该文件是否已经存在   
  7.             if (!File.Exists(path))   
  8.             {   
  9.                 Console.WriteLine("create new excel");   
  10.                 //获取shelf类当前加载的程序集   
  11.                 Assembly myAssembly = Assembly.GetAssembly(typeof(shelf));   
  12.                 string[] resFiles = myAssembly.GetManifestResourceNames();   
  13.                 string excelFileName = string.Empty;   
  14.                 //查找PaperTag.xls文件   
  15.                 for (int i = 0; i < resFiles.Length; i++)   
  16.                 {   
  17.                     if (resFiles[i].Contains("PaperTag.xls"))   
  18.                     {   
  19.                         excelFileName = resFiles[i];   
  20.                         break;   
  21.                     }   
  22.                 }   
  23.                 //将该文件写入到指定目录下   
  24.                 Stream tempStream = myAssembly.GetManifestResourceStream(excelFileName);   
  25.                 byte[] fileByte = new byte[tempStream.Length];   
  26.                 tempStream.Read(fileByte, 0, fileByte.Length);   
  27.                 File.WriteAllBytes("c://PaperTag.xls", fileByte);   
  28.             }   
  29.         }   
  30.     }  

 

Code:
  1. private void NewTagPrint_Load(object sender, EventArgs e)   
  2.         {   
  3.             if (File.Exists("TL-ModelPart.txt") && File.Exists("TL-ModelAll.txt"))   
  4.             {   
  5.                 blueText = File.ReadAllText("TL-ModelPart.txt");   
  6.                 otherText = File.ReadAllText("TL-ModelAll.txt");   
  7.             }   
  8.             else  
  9.             {   
  10.                 Assembly myAssembly = Assembly.GetAssembly(typeof(NewTagPrint));   
  11.                 string[] modelFiles = myAssembly.GetManifestResourceNames();   
  12.   
  13.                 string blueFileName = modelFiles.Single((element) => element.Contains("TL-ModelPart.txt"));   
  14.                 Stream blueStream = myAssembly.GetManifestResourceStream(blueFileName);   
  15.                 StreamReader blueReader = new StreamReader(blueStream);   
  16.                 blueText = blueReader.ReadToEnd();   
  17.                 blueReader.Close();   
  18.                 blueStream.Close();   
  19.   
  20.                 string otherFileName = modelFiles.Single((element) => element.Contains("TL-ModelAll.txt"));   
  21.                 Stream otherStream = myAssembly.GetManifestResourceStream(otherFileName);   
  22.                 StreamReader otherReader = new StreamReader(otherStream);   
  23.                 otherText = otherReader.ReadToEnd();   
  24.                 otherReader.Close();   
  25.                 otherStream.Close();   
  26.   
  27.             }   
  28.             if (!File.Exists("BlueHistory.txt"))   
  29.             {   
  30.                 FileStream stream = File.Create("BlueHistory.txt");   
  31.                 stream.Close();   
  32.             }   
  33.             if (!File.Exists("OtherHistory.txt"))   
  34.             {   
  35.                 FileStream stream = File.Create("OtherHistory.txt");   
  36.                 stream.Close();   
  37.             }   
  38.         }  

 

Code:
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using System.Windows.Forms;   
  9. using System.Text.RegularExpressions;   
  10. using System.IO;   
  11. using System.Reflection;   
  12. using System.Diagnostics;   
  13.   
  14. namespace RFIDTagPrint   
  15. {   
  16.     public partial class NewTagPrint : Form   
  17.     {   
  18.         private const string tagPrefix = "000059074EF65E93";   
  19.         string blueText;   
  20.         string otherText;   
  21.         string cruuentLocationString;   
  22.         public NewTagPrint()   
  23.         {   
  24.             InitializeComponent();              
  25.         }   
  26.   
  27.         private string formatString(string aa, int length)   
  28.         {   
  29.             string returnValue = aa;   
  30.             while (returnValue.Length < length)   
  31.             {   
  32.                 returnValue = "0" + returnValue;   
  33.             }   
  34.             return returnValue;   
  35.         }   
  36.   
  37.         private void BuildTagTextbutton_Click(object sender, EventArgs e)   
  38.         {   
  39.             BuildTagTextbutton.Enabled = false;   
  40.             string locationString = textBox1.Text;   
  41.             if (locationString != string.Empty)   
  42.             {   
  43.                 cruuentLocationString = string.Empty;   
  44.                 File.Delete("tagText.txt");   
  45.                 string[] locations = locationString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);   
  46.                 for (int i = 0; i < locations.Length; i++)   
  47.                 {   
  48.                     int startRack = -1;   
  49.                     int endRack = -1;   
  50.                     int startFloor = -1;   
  51.                     int endFloor = -1;   
  52.                     int startPosition = -1;   
  53.                     int endPosition = -1;   
  54.                     string tagPositionType = string.Empty;   
  55.   
  56.                     Regex regex = new Regex(@"^/d{1,2}[-][1-9][-]/d{1,2}[-][obOB]$");   
  57.                     Regex regex1 = new Regex(@"^/d{1,2}[-][1-9][-]/d{1,2}[-]/d{1,2}[-][obOB]$");   
  58.                     Regex regex2 = new Regex(@"^/d{1,2}[-][1-9][-][1-9][-]/d{1,2}[-]/d{1,2}[-][obOB]$");   
  59.                     Regex regex3 = new Regex(@"^/d{1,2}[-]/d{1,2}[-][1-9][-][1-9][-]/d{1,2}[-]/d{1,2}[-][obOB]$");   
  60.   
  61.                     if (regex.IsMatch(locations[i]))   
  62.                     {   
  63.                         string[] temp = locations[i].Split('-');   
  64.                         startRack = endRack = int.Parse(temp[0]);   
  65.                         startFloor = endFloor = int.Parse(temp[1]);   
  66.                         startPosition = endPosition = int.Parse(temp[2]);   
  67.                         tagPositionType = temp[3];   
  68.                     }   
  69.                     else if (regex1.IsMatch(locations[i]))   
  70.                     {   
  71.                         string[] temp = locations[i].Split('-');   
  72.                         startRack = endRack = int.Parse(temp[0]);   
  73.                         startFloor = endFloor = int.Parse(temp[1]);   
  74.                         startPosition = int.Parse(temp[2]);   
  75.                         endPosition = int.Parse(temp[3]);   
  76.                         tagPositionType = temp[4];   
  77.                     }   
  78.                     else if (regex2.IsMatch(locations[i]))   
  79.                     {   
  80.                         string[] temp = locations[i].Split('-');   
  81.                         startRack = endRack = int.Parse(temp[0]);   
  82.                         startFloor = int.Parse(temp[1]);   
  83.                         endFloor = int.Parse(temp[2]);   
  84.                         startPosition = int.Parse(temp[3]);   
  85.                         endPosition = int.Parse(temp[4]);   
  86.                         tagPositionType = temp[5];   
  87.                     }   
  88.                     else if (regex3.IsMatch(locations[i]))   
  89.                     {   
  90.                         string[] temp = locations[i].Split('-');   
  91.                         startRack = int.Parse(temp[0]);   
  92.                         endRack = int.Parse(temp[1]);   
  93.                         startFloor = int.Parse(temp[2]);   
  94.                         endFloor = int.Parse(temp[3]);   
  95.                         startPosition = int.Parse(temp[4]);   
  96.                         endPosition = int.Parse(temp[5]);   
  97.                         tagPositionType = temp[6];   
  98.                     }   
  99.                     else  
  100.                     {   
  101.                         return;   
  102.                     }   
  103.   
  104.   
  105.                     string historyPath = string.Empty;   
  106.                     string sampleText = string.Empty;   
  107.   
  108.                     if (tagPositionType.ToLower() == "b")   
  109.                     {   
  110.                         historyPath = "BlueHistory.txt";   
  111.                         sampleText = blueText;   
  112.                     }   
  113.                     else  
  114.                     {   
  115.                         historyPath = "OtherHistory.txt";   
  116.                         sampleText = otherText;   
  117.                     }   
  118.   
  119.                     string historyString = File.ReadAllText(historyPath);   
  120.                     StreamWriter writer = File.AppendText(historyPath);   
  121.                     StringBuilder builder = new StringBuilder();   
  122.                     for (int si = startRack; si < endRack + 1; si++)   
  123.                     {   
  124.                         for (int sj = startFloor; sj < endFloor + 1; sj++)   
  125.                         {   
  126.                             for (int sk = startPosition; sk < endPosition + 1; sk++)   
  127.                             {   
  128.                                 string positonString = si.ToString("00") + "-"  
  129.                                     + sj.ToString() + "-" + sk.ToString("00");   
  130.                                 string tagString = tagPrefix + formatString(Convert.ToString(si, 16), 3)   
  131.                                     + formatString(Convert.ToString(sj, 16), 2) + formatString(   
  132.                                     Convert.ToString(sk, 16), 3);   
  133.                                 writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "    " + positonString + "    " + tagString);   
  134.                                 builder.AppendLine(sampleText.Replace("@Position", positonString).Replace("@TagID", tagString));   
  135.                             }   
  136.                         }   
  137.                     }   
  138.                     File.AppendAllText("tagText.txt", builder.ToString());   
  139.                     writer.Flush();   
  140.                     writer.Close();   
  141.                     string fileName = "tagText" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".txt";   
  142.                     if (i == 0)   
  143.                     {   
  144.                         File.AppendAllText(fileName, locationString + "/r/n" + builder.ToString());   
  145.                     }   
  146.                     else  
  147.                     {   
  148.                         File.AppendAllText(fileName, builder.ToString());   
  149.                     }   
  150.                     cruuentLocationString = locationString.Replace("-o""").Replace("-O""").Replace("-b""").Replace("-B""").TrimEnd(',');   
  151.                     TagPrintbutton.Enabled = true;   
  152.                 }   
  153.                 BuildTagTextbutton.Enabled = true;   
  154.             }   
  155.         }   
  156.   
  157.         private void NewTagPrint_Load(object sender, EventArgs e)   
  158.         {   
  159.             if (File.Exists("TL-ModelPart.txt") && File.Exists("TL-ModelAll.txt"))   
  160.             {   
  161.                 blueText = File.ReadAllText("TL-ModelPart.txt");   
  162.                 otherText = File.ReadAllText("TL-ModelAll.txt");   
  163.             }   
  164.             else  
  165.             {   
  166.                 Assembly myAssembly = Assembly.GetAssembly(typeof(NewTagPrint));   
  167.                 string[] modelFiles = myAssembly.GetManifestResourceNames();   
  168.   
  169.                 string blueFileName = modelFiles.Single((element) => element.Contains("TL-ModelPart.txt"));   
  170.                 Stream blueStream = myAssembly.GetManifestResourceStream(blueFileName);   
  171.                 StreamReader blueReader = new StreamReader(blueStream);   
  172.                 blueText = blueReader.ReadToEnd();   
  173.                 blueReader.Close();   
  174.                 blueStream.Close();   
  175.   
  176.                 string otherFileName = modelFiles.Single((element) => element.Contains("TL-ModelAll.txt"));   
  177.                 Stream otherStream = myAssembly.GetManifestResourceStream(otherFileName);   
  178.                 StreamReader otherReader = new StreamReader(otherStream);   
  179.                 otherText = otherReader.ReadToEnd();   
  180.                 otherReader.Close();   
  181.                 otherStream.Close();   
  182.   
  183.             }   
  184.             if (!File.Exists("BlueHistory.txt"))   
  185.             {   
  186.                 FileStream stream = File.Create("BlueHistory.txt");   
  187.                 stream.Close();   
  188.             }   
  189.             if (!File.Exists("OtherHistory.txt"))   
  190.             {   
  191.                 FileStream stream = File.Create("OtherHistory.txt");   
  192.                 stream.Close();   
  193.             }   
  194.         }   
  195.   
  196.         private void textBox1_TextChanged(object sender, EventArgs e)   
  197.         {   
  198.             string regexString = @"^((/d{1,2}[-][1-9][-]/d{1,2}[-][obOB][,])|(/d{1,2}[-][1-9][-]/d{1,2}[-]/d{1,2}[-][obOB][,])|(/d{1,2}[-][1-9][-][1-9][-]/d{1,2}[-]/d{1,2}[-][obOB][,])|(/d{1,2}[-]/d{1,2}[-][1-9][-][1-9][-]/d{1,2}[-]/d{1,2}[-][obOB][,])){1,}$";   
  199.             Regex regex = new Regex(regexString);   
  200.             if (regex.IsMatch(textBox1.Text))   
  201.             {   
  202.                 textBox2.ForeColor = Color.Green;   
  203.                 textBox2.Text = "格式正确";   
  204.                 BuildTagTextbutton.Enabled = true;   
  205.             }   
  206.             else  
  207.             {   
  208.                 textBox2.Text = "格式不正确";   
  209.                 textBox2.ForeColor = Color.Red;   
  210.                 BuildTagTextbutton.Enabled = false;   
  211.             }   
  212.         }   
  213.   
  214.         private void NewTagPrint_Resize(object sender, EventArgs e)   
  215.         {   
  216.             textBox1.Height = this.Height - 136;   
  217.             textBox2.Location = new Point(0, textBox1.Height + 32);   
  218.             BuildTagTextbutton.Location = new Point(0, textBox1.Height + 65);   
  219.             TagPrintbutton.Location = new Point(TagPrintbutton.Location.X, BuildTagTextbutton.Location.Y);   
  220.         }   
  221.   
  222.         private void TagPrintbutton_Click(object sender, EventArgs e)   
  223.         {   
  224.             if (File.Exists("TagText.txt") && !string.IsNullOrEmpty(cruuentLocationString))   
  225.             {   
  226.                 if (DialogResult.OK == MessageBox.Show   
  227.                     (cruuentLocationString, "确认打印以下位置的电子标签?",   
  228.                     MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))   
  229.                 {   
  230.                     if (File.Exists("PrintTag.bat") && File.Exists("ftp.dat"))   
  231.                     {   
  232.                         Process myProcess = new Process();   
  233.                         myProcess.StartInfo = new ProcessStartInfo();   
  234.                         myProcess.StartInfo.CreateNoWindow = false;   
  235.                         myProcess.StartInfo.FileName = "cmd.exe";   
  236.                         myProcess.StartInfo.Arguments = "/c PrintTag.bat";   
  237.                         myProcess.Start();   
  238.                         myProcess.WaitForExit();   
  239.                         myProcess.Close();   
  240.                     }   
  241.                     else  
  242.                     {   
  243.                         MessageBox.Show("批处理命令丢失!");   
  244.                     }   
  245.                 }   
  246.             }   
  247.             else  
  248.             {   
  249.                 MessageBox.Show("请先生成标签文本!");   
  250.             }   
  251.         }   
  252.   
  253.         private void BuildTagTextbutton_MouseHover(object sender, EventArgs e)   
  254.         {   
  255.             ToolTip toolTip1 = new ToolTip();   
  256.             StringBuilder builder = new StringBuilder();   
  257.             builder.AppendLine("1-1-1表示第1货架第1层第1位置");   
  258.             builder.AppendLine("1-1-1-10表示第1货架第1层1到10位置");   
  259.             builder.AppendLine("1-1-9-1-10表示第1货架第1到9层第1到10位置");   
  260.             builder.AppendLine("1-10-1-9-1-10表示第1到10货架第1到9层第1到10位置");   
  261.             builder.AppendLine("位置后面加'-o'或'-O'表示打印到中间,位置后面加'-b'或'-B'表示反着打印到上面");   
  262.             toolTip1.ToolTipTitle = "位置信息解释";   
  263.             toolTip1.SetToolTip(BuildTagTextbutton, builder.ToString());    
  264.         }   
  265.     }   
  266. }   

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值