24 October 2012

Enable USB Port using C#.Net

Today ,We will discuss about Enable and Disable USB Port using C Sharp .Net. First ,Create a Application for Enable and Disable USB Port using C Sharp .Net. In our Example: Two Buttons in the form. One is Enable. Another one is disable. Now, we are going to our Coding Section:
using System.Security.Principal;
public static bool IsUserAnAdministrator;
private void btn_Enable_Click(object sender, EventArgs e)
{ Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 3, Microsoft.Win32.RegistryValueKind.DWord);
}
private void btn_Disable_Click(object sender, EventArgs e)
{ Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 4, Microsoft.Win32.RegistryValueKind.DWord);
}
private void FrmEnabledisableUSBPort_Load(object sender, EventArgs e)
{
if (IsAnAdministrator())
{
btn_Disable.Enabled = true;
btn_Enable.Enabled = true;
}
else
{
btn_Disable.Enabled = false;
btn_Enable.Enabled = false;
}
}
To check User is Admin or Not.
bool IsAnAdministrator()
{
WindowsIdentity MyIdentity = WindowsIdentity.GetCurrent();
WindowsPrincipal MyPrincipal = new WindowsPrincipal(MyIdentity);
return MyPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
conclusion: Thanks for reading this Article.

No comments: