本例主要是从内部程序集内读取一个文件,然后输入到指定目录
- class shelf
- {
- static void Main(string[] args)
- {
- string path = "c://PaperTag.xls";
- //判断该文件是否已经存在
- if (!File.Exists(path))
- {
- Console.WriteLine("create new excel");
- //获取shelf类当前加载的程序集
- Assembly myAssembly = Assembly.GetAssembly(typeof(shelf));
- string[] resFiles = myAssembly.GetManifestResourceNames();
- string excelFileName = string.Empty;
- //查找PaperTag.xls文件
- for (int i = 0; i < resFiles.Length; i++)
- {
- if (resFiles[i].Contains("PaperTag.xls"))
- {
- excelFileName = resFiles[i];
- break;
- }
- }
- //将该文件写入到指定目录下
- Stream tempStream = myAssembly.GetManifestResourceStream(excelFileName);
- byte[] fileByte = new byte[tempStream.Length];
- tempStream.Read(fileByte, 0, fileByte.Length);
- File.WriteAllBytes("c://PaperTag.xls", fileByte);
- }
- }
- }
- private void NewTagPrint_Load(object sender, EventArgs e)
- {
- if (File.Exists("TL-ModelPart.txt") && File.Exists("TL-ModelAll.txt"))
- {
- blueText = File.ReadAllText("TL-ModelPart.txt");
- otherText = File.ReadAllText("TL-ModelAll.txt");
- }
- else
- {
- Assembly myAssembly = Assembly.GetAssembly(typeof(NewTagPrint));
- string[] modelFiles = myAssembly.GetManifestResourceNames();
- string blueFileName = modelFiles.Single((element) => element.Contains("TL-ModelPart.txt"));
- Stream blueStream = myAssembly.GetManifestResourceStream(blueFileName);
- StreamReader blueReader = new StreamReader(blueStream);
- blueText = blueReader.ReadToEnd();
- blueReader.Close();
- blueStream.Close();
- string otherFileName = modelFiles.Single((element) => element.Contains("TL-ModelAll.txt"));
- Stream otherStream = myAssembly.GetManifestResourceStream(otherFileName);
- StreamReader otherReader = new StreamReader(otherStream);
- otherText = otherReader.ReadToEnd();
- otherReader.Close();
- otherStream.Close();
- }
- if (!File.Exists("BlueHistory.txt"))
- {
- FileStream stream = File.Create("BlueHistory.txt");
- stream.Close();
- }
- if (!File.Exists("OtherHistory.txt"))
- {
- FileStream stream = File.Create("OtherHistory.txt");
- stream.Close();
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Text.RegularExpressions;
- using System.IO;
- using System.Reflection;
- using System.Diagnostics;
- namespace RFIDTagPrint
- {
- public partial class NewTagPrint : Form
- {
- private const string tagPrefix = "000059074EF65E93";
- string blueText;
- string otherText;
- string cruuentLocationString;
- public NewTagPrint()
- {
- InitializeComponent();
- }
- private string formatString(string aa, int length)
- {
- string returnValue = aa;
- while (returnValue.Length < length)
- {
- returnValue = "0" + returnValue;
- }
- return returnValue;
- }
- private void BuildTagTextbutton_Click(object sender, EventArgs e)
- {
- BuildTagTextbutton.Enabled = false;
- string locationString = textBox1.Text;
- if (locationString != string.Empty)
- {
- cruuentLocationString = string.Empty;
- File.Delete("tagText.txt");
- string[] locations = locationString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
- for (int i = 0; i < locations.Length; i++)
- {
- int startRack = -1;
- int endRack = -1;
- int startFloor = -1;
- int endFloor = -1;
- int startPosition = -1;
- int endPosition = -1;
- string tagPositionType = string.Empty;
- Regex regex = new Regex(@"^/d{1,2}[-][1-9][-]/d{1,2}[-][obOB]$");
- Regex regex1 = new Regex(@"^/d{1,2}[-][1-9][-]/d{1,2}[-]/d{1,2}[-][obOB]$");
- Regex regex2 = new Regex(@"^/d{1,2}[-][1-9][-][1-9][-]/d{1,2}[-]/d{1,2}[-][obOB]$");
- Regex regex3 = new Regex(@"^/d{1,2}[-]/d{1,2}[-][1-9][-][1-9][-]/d{1,2}[-]/d{1,2}[-][obOB]$");
- if (regex.IsMatch(locations[i]))
- {
- string[] temp = locations[i].Split('-');
- startRack = endRack = int.Parse(temp[0]);
- startFloor = endFloor = int.Parse(temp[1]);
- startPosition = endPosition = int.Parse(temp[2]);
- tagPositionType = temp[3];
- }
- else if (regex1.IsMatch(locations[i]))
- {
- string[] temp = locations[i].Split('-');
- startRack = endRack = int.Parse(temp[0]);
- startFloor = endFloor = int.Parse(temp[1]);
- startPosition = int.Parse(temp[2]);
- endPosition = int.Parse(temp[3]);
- tagPositionType = temp[4];
- }
- else if (regex2.IsMatch(locations[i]))
- {
- string[] temp = locations[i].Split('-');
- startRack = endRack = int.Parse(temp[0]);
- startFloor = int.Parse(temp[1]);
- endFloor = int.Parse(temp[2]);
- startPosition = int.Parse(temp[3]);
- endPosition = int.Parse(temp[4]);
- tagPositionType = temp[5];
- }
- else if (regex3.IsMatch(locations[i]))
- {
- string[] temp = locations[i].Split('-');
- startRack = int.Parse(temp[0]);
- endRack = int.Parse(temp[1]);
- startFloor = int.Parse(temp[2]);
- endFloor = int.Parse(temp[3]);
- startPosition = int.Parse(temp[4]);
- endPosition = int.Parse(temp[5]);
- tagPositionType = temp[6];
- }
- else
- {
- return;
- }
- string historyPath = string.Empty;
- string sampleText = string.Empty;
- if (tagPositionType.ToLower() == "b")
- {
- historyPath = "BlueHistory.txt";
- sampleText = blueText;
- }
- else
- {
- historyPath = "OtherHistory.txt";
- sampleText = otherText;
- }
- string historyString = File.ReadAllText(historyPath);
- StreamWriter writer = File.AppendText(historyPath);
- StringBuilder builder = new StringBuilder();
- for (int si = startRack; si < endRack + 1; si++)
- {
- for (int sj = startFloor; sj < endFloor + 1; sj++)
- {
- for (int sk = startPosition; sk < endPosition + 1; sk++)
- {
- string positonString = si.ToString("00") + "-"
- + sj.ToString() + "-" + sk.ToString("00");
- string tagString = tagPrefix + formatString(Convert.ToString(si, 16), 3)
- + formatString(Convert.ToString(sj, 16), 2) + formatString(
- Convert.ToString(sk, 16), 3);
- writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + positonString + " " + tagString);
- builder.AppendLine(sampleText.Replace("@Position", positonString).Replace("@TagID", tagString));
- }
- }
- }
- File.AppendAllText("tagText.txt", builder.ToString());
- writer.Flush();
- writer.Close();
- string fileName = "tagText" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".txt";
- if (i == 0)
- {
- File.AppendAllText(fileName, locationString + "/r/n" + builder.ToString());
- }
- else
- {
- File.AppendAllText(fileName, builder.ToString());
- }
- cruuentLocationString = locationString.Replace("-o", "").Replace("-O", "").Replace("-b", "").Replace("-B", "").TrimEnd(',');
- TagPrintbutton.Enabled = true;
- }
- BuildTagTextbutton.Enabled = true;
- }
- }
- private void NewTagPrint_Load(object sender, EventArgs e)
- {
- if (File.Exists("TL-ModelPart.txt") && File.Exists("TL-ModelAll.txt"))
- {
- blueText = File.ReadAllText("TL-ModelPart.txt");
- otherText = File.ReadAllText("TL-ModelAll.txt");
- }
- else
- {
- Assembly myAssembly = Assembly.GetAssembly(typeof(NewTagPrint));
- string[] modelFiles = myAssembly.GetManifestResourceNames();
- string blueFileName = modelFiles.Single((element) => element.Contains("TL-ModelPart.txt"));
- Stream blueStream = myAssembly.GetManifestResourceStream(blueFileName);
- StreamReader blueReader = new StreamReader(blueStream);
- blueText = blueReader.ReadToEnd();
- blueReader.Close();
- blueStream.Close();
- string otherFileName = modelFiles.Single((element) => element.Contains("TL-ModelAll.txt"));
- Stream otherStream = myAssembly.GetManifestResourceStream(otherFileName);
- StreamReader otherReader = new StreamReader(otherStream);
- otherText = otherReader.ReadToEnd();
- otherReader.Close();
- otherStream.Close();
- }
- if (!File.Exists("BlueHistory.txt"))
- {
- FileStream stream = File.Create("BlueHistory.txt");
- stream.Close();
- }
- if (!File.Exists("OtherHistory.txt"))
- {
- FileStream stream = File.Create("OtherHistory.txt");
- stream.Close();
- }
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- 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,}$";
- Regex regex = new Regex(regexString);
- if (regex.IsMatch(textBox1.Text))
- {
- textBox2.ForeColor = Color.Green;
- textBox2.Text = "格式正确";
- BuildTagTextbutton.Enabled = true;
- }
- else
- {
- textBox2.Text = "格式不正确";
- textBox2.ForeColor = Color.Red;
- BuildTagTextbutton.Enabled = false;
- }
- }
- private void NewTagPrint_Resize(object sender, EventArgs e)
- {
- textBox1.Height = this.Height - 136;
- textBox2.Location = new Point(0, textBox1.Height + 32);
- BuildTagTextbutton.Location = new Point(0, textBox1.Height + 65);
- TagPrintbutton.Location = new Point(TagPrintbutton.Location.X, BuildTagTextbutton.Location.Y);
- }
- private void TagPrintbutton_Click(object sender, EventArgs e)
- {
- if (File.Exists("TagText.txt") && !string.IsNullOrEmpty(cruuentLocationString))
- {
- if (DialogResult.OK == MessageBox.Show
- (cruuentLocationString, "确认打印以下位置的电子标签?",
- MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
- {
- if (File.Exists("PrintTag.bat") && File.Exists("ftp.dat"))
- {
- Process myProcess = new Process();
- myProcess.StartInfo = new ProcessStartInfo();
- myProcess.StartInfo.CreateNoWindow = false;
- myProcess.StartInfo.FileName = "cmd.exe";
- myProcess.StartInfo.Arguments = "/c PrintTag.bat";
- myProcess.Start();
- myProcess.WaitForExit();
- myProcess.Close();
- }
- else
- {
- MessageBox.Show("批处理命令丢失!");
- }
- }
- }
- else
- {
- MessageBox.Show("请先生成标签文本!");
- }
- }
- private void BuildTagTextbutton_MouseHover(object sender, EventArgs e)
- {
- ToolTip toolTip1 = new ToolTip();
- StringBuilder builder = new StringBuilder();
- builder.AppendLine("1-1-1表示第1货架第1层第1位置");
- builder.AppendLine("1-1-1-10表示第1货架第1层1到10位置");
- builder.AppendLine("1-1-9-1-10表示第1货架第1到9层第1到10位置");
- builder.AppendLine("1-10-1-9-1-10表示第1到10货架第1到9层第1到10位置");
- builder.AppendLine("位置后面加'-o'或'-O'表示打印到中间,位置后面加'-b'或'-B'表示反着打印到上面");
- toolTip1.ToolTipTitle = "位置信息解释";
- toolTip1.SetToolTip(BuildTagTextbutton, builder.ToString());
- }
- }
- }