Problem with if/then statement and streamreader
In my program I implemented proxy support.
I set up an if/then statement with a checkbox so basically it says if the checkbox is checked activate the proxy and use the proxies that the user uploaded into a list box.
I also have some streamreaders set up in my code which imports the text of a textfile in a text box. When I try to do proxies and try to import the text file it gives me some error saying the file could not be found but when I dont check the check box to use the proxy it imports it just fine.
This is the code for the checkbox:
if (chkBoxProxy.Checked)
{
if (listBox1开发者_开发百科.SelectedIndex < listBox1.Items.Count - 1)
{
listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
listBox1.SetSelected(listBox1.SelectedIndex, true);
RefreshIESettings(listBox1.SelectedItem.ToString());
for (int i = 0; i < numericUpDown2.Value; i++)
{
listBox4.SetSelected(i, true);
listBox4.SelectedItem.ToString();
account();
}
}
}
else
{
for (int i = 0; i < numericUpDown2.Value; i++)
{
listBox4.SetSelected(i, true);
listBox4.SelectedItem.ToString();
account();
}
}
And this is the code for one of the several stream readers that I have:
StreamReader stream1 = new StreamReader("website.txt");
string stream2 = stream1.ReadToEnd();
txtBoxImportWebsite.Text = stream2;
stream1.Close();
stream1.Dispose();
boy your code is not complete enough, we should see exactly all the places where you call your stream management from. From where/what is the file website.txt
created? why are you not specifying a full path when you open it?
as a general rule you should wrap your Streams with using so close and dispose is called automatically when using block ends and also in case of exceptions, you could rewrite your last fragment in this way:
using (var stream1 = new StreamReader("website.txt"))
{
txtBoxImportWebsite.Text = stream1.ReadToEnd();
}
精彩评论