在c#中更改StreamReader文件扩展名

时间:2012-04-10 01:23:21

标签: c# streamreader file-extension

我正在制作一个使用条形码存储与项目相关的信息的项目。对于每个条形码,都有一个包含其信息的文本文件。一个例子是条形码0000000025将其所有信息存储在名为0000000025.txt的文件中。我希望能够将条形码输入到文本框中,它会根据条形码告诉StreamReader要读取哪个文件。如果是这样的其他语句,请不要手动编码300: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();

    }
}

1 个答案:

答案 0 :(得分:2)

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

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