.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> System Process >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Status of USB Port using C#.Net

Posted By:Narayanan       Posted Date: October 24, 2012    Points: 25    Category: System Process    URL: http://www.dotnetspark.com  

Today, we will discuss about Enable and Disable USB Port using C#.Net.Now let's go to our Coding section.
 

First Create an Application in the VS 2008 C#.

Create Two Button in the Form.

One is called "Enable".

Another one is called "Disable".
Go to Your Form Coding Section:

Declare namespace :

using System.Security.Principal; 

Declare Variable for Checking login user is Admin or Normal User

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. 


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend