- Posted by Admin on May 17, 2009
Changing the IP address of the computer automatically is very easy. Microsoft trickily stores the IP information in registry. Refer the below code that changes the IP address of the host server. This can be used in windows application or windows services to change the IP address of the local computer and can also be used in asp.net to change the IP address of the server that hosts your asp.net application. Make sure that Microsoft.Win32 namespace is visible to the below code wherever it is placed because Win32 is needed for manipulating Windows registry. Server restart is needed for new IP addresses to come into the effect.
RegistryKey key = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces", true);
// Registry key where IP information is stored.
foreach (string s in key.GetSubKeyNames())
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\" + s, true);
if (rk.ValueCount >= 12) // In general, active network card will have more than 12 setting values
{
try
{
string[] s1 = { "12.24.36.48", "13.26.39.64" }; // Array of IP addresses to set.
rk.SetValue("IPAddress", s1);
}
catch (Exception exc)
{
// Error message logic here.
}
}
rk.Close();
}
NOTE: Do not use this code uniethically.
Moreover, it requires Full Trust level enabled for the ASP.NET website to run above code, otherwise security exception will occur.
Security Exception
Description:
The application attempted to perform an operation not allowed by the
security policy. To grant this application the required permission please
contact your system administrator or change the application's trust level in the
configuration file.
Exception Details:
System.Security.SecurityException: Requested registry access is not
allowed.
- Posted by Admin on May 13, 2009
You can use the below class for fetching the contents of any HTTP URL using the HTTP GET method. This makes use the webrequest classes of .NET class library.
public static class GetUrl
{
public static string FetchURL(string url)
{
const int bufSizeMax = 65536; // max read buffer size conserves memory
const int bufSizeMin = 8192; // min size prevents numerous small reads
StringBuilder sb;
// A WebException is thrown if HTTP request fails
try
{
// Create an HttpWebRequest using WebRequest.Create (see .NET docs)!
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Http.Get;
// Execute the request and obtain the response stream
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
// Content-Length header is not trustable, but makes a good hint.
// Responses longer than int size will throw an exception here!
int length = (int)response.ContentLength;
// Use Content-Length if between bufSizeMax and bufSizeMin
int bufSize = bufSizeMin;
if (length > bufSize)
bufSize = length > bufSizeMax ? bufSizeMax : length;
// Allocate buffer and StringBuilder for reading response
byte[] buf = new byte[bufSize];
sb = new StringBuilder(bufSize);
// Read response stream until end
while ((length = responseStream.Read(buf, 0, buf.Length)) != 0)
sb.Append(Encoding.UTF8.GetString(buf, 0, length));
return sb.ToString();
}
catch (Exception ex)
{
sb = new StringBuilder(ex.Message);
return sb.ToString();
}
}
}
EXAMPLE
string htmlcontent = GetUrl.FetchURL("http://ask4asp.net");
This will fetch the html content of the ask4asp.net home page.
- Posted by Admin on May 13, 2009
Sometimes, we may need to act on some information(object) which is critical for the application and we need to make sure that the information contained by that object is consistent. The way to keep the information in the object consistent is to avoid the access of that object by more than one thread or program. In short, we need to achieve mutual exclusion for that object for predefined number of statements or block of code. C# offers this by several ways such as lock, mutex and semaphores. lock statement is the easiest way to lock the object and the related statements.
The basic syntax is below.
Object thisLock = new Object();
lock (thisLock)
{
// Critical code section
}
Suppose you act upon one DataTable object and you do not want to alter the content of it by more than one threads then option available is to lock that datatable object.
DataTable dt = DatabaseOperations.GetDataTable(SQL);
lock(dt)
{
// critical statements to alter the contents inside dt
}
In short, lock enables us to achieve the mutually exclusive access to the locked object such that, when it is locked, no other thread can access that locked object until its lock is released. The thread that attempts to gain the access of the locked object gets to the SUPENDED state until the lock is removed.
Examples
lock(this) - locks the current instance of the object in which the statemens are being executed.
lock(typeof(ClassName)) - locks the static members of the class ClassName