2012-04-10 71 views
1

我正在制作一个项目,该项目使用条形码来存储与项目有关的信息。对于每个条形码,都有一个包含其信息的文本文件。例如,条形码0000000025将其所有信息存储在名为0000000025.txt的文件中。我希望能够将条形码输入到文本框中,并根据条形码告知StreamReader要从中读取哪个文件。代替手工编码300,如果是这样else语句:StreamReader("C:\\ITRS_equipment_log\\0000000025.txt");在C中更改StreamReader文件扩展名#

有没有办法告诉它

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(); 

    } 
} 

回答

2

这应该条形码文本插入到你的文件名:

TextReader textReader 
    = new StreamReader(String.Format(@"C:\ITRS_equipment_log\{0}.txt", textBox1.Text)); 
+0

谢谢!完美地工作! – McAfeeJ 2012-04-10 01:32:58