I am attempting to read some data off of a server for a built in update function and I am getting a compile time error. Is there any way I can get around this? I need this to check if the user has a working internet connection and if not skip the update check. (Unless there are more reliable ways in .Net 4.5)
WebClient client = new WebClient();
try
{
Stream stream = client.OpenRead("http://repo.itechy21.com/updatematerial.txt");
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
StreamReader reader = new StreamReader(stream); // <-- error here
String content = reader.ReadLine();
Error:
stream does not exist in the current context
Just declare the Stream
variable before the try
block. Otherwise its scope is limited to the try
block itself. Remember that the scope of a variable is the block of code where it is declared. Just look at the most-inner opening and closing braces surrounding the variable and you'll immediately know where it's legal to refer to that variable.
Stream stream; // or Stream stream = null;
try
{
stream = client.OpenRead("http://repo.itechy21.com/updatematerial.txt");
}
// rest of code