Auto enable ToS/DSCP setting in the Registry in C#

16 04 2009

//Check for registry keys
RegistryKey rkToS = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\TcpIp\\Parameters"true);
// If the return value is null, the key doesn't exist
if (rkToS == null)
{
    // The key doesn't exist; create it
    rkToS = Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Services\\TcpIp\\Parameters");
}
// Attempt to retrieve the value for DisableUserTOSSetting; if null returned, the value doesn't exist in the registry.
if (rkToS.GetValue("DisableUserTOSSetting") != null)
{
    // The value exists; 
    int checkToSKey = (int)rkToS.GetValue("DisableUserTOSSetting");
    //Check if it has the right value
    if (checkToSKey != 0)
    {
        //Wrong value...correcting it
        rkToS.SetValue("DisableUserTOSSetting", 0);
        MessageBox.Show("I have just changed the Registry Key to enable ToS values.\nFor this to take effect, you need to restart your computer!");
        return;
    }
}
else
{
    //add value
    rkToS.SetValue("DisableUserTOSSetting", 0);
    MessageBox.Show("I have just added the Registry Key to enable ToS values.\nFor this to take effect, you need to restart your computer!");
    return;
}





How to download a file from a URL and save onto local directory in C#

12 04 2009

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace WindowsFormsApplication1
{
public partial class Form1Form
{
public Form1()
{
InitializeComponent();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost/aaa.txt");
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream s = resp.GetResponseStream();
byte[] buf = new byte[16384];
using (FileStream fs = new FileStream(@"C:\bbb.txt"FileMode.Create, FileAccess.Write))
{
for (; ; )
{
int len = s.Read(buf, 0, buf.Length);
if (len == 0) break;
fs.Write(buf, 0, len);
}
}
s.Close();
}
}
}








Follow

Get every new post delivered to your Inbox.

Join 180 other followers