1

我正在制作一个使用条形码来存储与该项目相关的信息的项目。对于每个条形码,都有一个包含其信息的文本文件。例如,条形码 0000000025 的所有信息都存储在名为 0000000025.txt 的文件中。我希望能够将条形码输入到文本框中,它会告诉 StreamReader 根据条形码读取哪个文件。而不是手动编码 300 if else 语句,如下所示:StreamReader("C:\\ITRS_equipment_log\\0000000025.txt");

有没有办法告诉它

StreamReader("C:\\ITRS_equipment_log\\"*pull the barcode from the first textbox and put it here*".txt");  

谢谢

private void buttonSubmit_Click(object sender, EventArgs e)
{      
    if (!String.IsNullOrEmpty(textBox1.Text))
    {
         textBox1.SelectionStart = 0;
         textBox1.SelectionLength = textBox1.Text.Length;

         TextReader textReader = new StreamReader("C:\\ITRS_equipment_log\\0000000025.txt");

         //model textbox receives the model of equipment associated with barcode in the text file
         modelTextbox.Text = textReader.ReadLine();

         //serial textbox receives the serial number of equipment associated with barcode in the text file
         serialTextbox.Text = textReader.ReadLine();

         //history textbox receives the history of equipment associated with barcode in the text file
         historyTextbox.Text = textReader.ReadToEnd();
         textReader.Close();

    }
}
4

1 回答 1

2

这应该为您将条形码文本插入到文件名中:

TextReader textReader
    = new StreamReader(String.Format(@"C:\ITRS_equipment_log\{0}.txt", textBox1.Text));
于 2012-04-10T01:26:21.777 回答