I have an application where all of the code is in a singe file, so I'm looking at tidying it up and create separate classes for some of the re-occurring code instead of having the same code duplicated throughout the application. One such action that's duplicated a lot is setting up a WebClient
and setting a proxy to do things like download images, check for app updates etc.
I've created a separate 'Proxy.cs' file and added the following code:
class Proxy
{
public static WebClient setProxy()
{
WebClient wc = new WebClient();
wc.Proxy = null;
if (Properties.Settings.Default.useProxy == true)
{
WebProxy proxy = new WebProxy(Properties.Settings.Default.proxyAddress);
if (Properties.Settings.Default.proxyAuth == true)
{
proxy.Credentials = new NetworkCredential(Properties.Settings.Default.proxyUser, Properties.Settings.Default.proxyPass);
proxy.UseDefaultCredentials = false;
proxy.BypassProxyOnLocal = false;
}
wc.Proxy = proxy;
}
return wc;
}
}
The idea being that when I check for updates, download new images etc, I can just call this class each time to configure the WebClient/Proxy
. However I cannot seem to get it working. In my main application, I'm calling it like so:
Proxy.setProxy();
byte[] bytes = wc.DownloadData(URL);
However I get the following error in my main application:
The name 'wc' does not exist in the current context
I'm still fairly new to C# and can;t work out how to actually get this working. Any pointers appreciated.