Sunday, 29 March 2020

Eject Pen drive (Flash Drive) In C#.Net

3/29/2020 - By Pranav Singh 8

This article will show you how you can eject connect pen drive or flash drive to you system using c#.net. In this first I will show you to detect the pen drive and then by clicking on button you will eject the pen drive  or flash drive by c#.

So first we will create a form in windows application.



Now on detect pen drive button add the below code. This code will help us to detect the pen drive and  we will be able to verify that the pen drive is connected or not.

/// Detect Pendrive
private void btnDetect_Click(object sender, EventArgs e)
{
    var drives = DriveInfo.GetDrives().Where(d => d.IsReady & d.DriveType == DriveType.Removable);
    if (drives.FirstOrDefault() != null)
    {
        pictureBox1.Image = Image.FromFile("img/connected.jpg");
        lbMessage.Text = "Pendrive Detcted";
    }
    else
    {
        lbMessage.Text = "No Pendrive Connected...";
    }

}


Now we will add the below code on eject button click. In this I have called a method which will eject the pen drive. You can check the code.


private void btnEject_Click(object sender, EventArgs e)
{
    var drives = DriveInfo.GetDrives().Where(d => d.IsReady & d.DriveType == DriveType.Removable);
    if (drives.FirstOrDefault() != null)
    {
        string status = EjectPendriveManager.EjectPenDrive(Convert.ToChar(drives.FirstOrDefault().Name.Replace(":\\""")));
        lbMessage.Text = status;
        pictureBox1.Image = Image.FromFile("img/not-connected.jpg");
    }
    else
    {
        lbMessage.Text = "No Pendrive Connected...";
    }

}


 In above code I have prepared a class library code. Please check the code below.

using System;
using System.Runtime.InteropServices;

public static class EjectPendriveManager
{
    const int OPEN_EXISTING = 3;
    const uint GENERIC_READ = 0x80000000;
    const uint GENERIC_WRITE = 0x40000000;
    const uint IOCTL_STORAGE_EJECT_MEDIA = 0x2D4808;

    [DllImport("kernel32")]
    private static extern int CloseHandle(IntPtr handle);
    [DllImport("kernel32")]
    private static extern int DeviceIoControl(IntPtr deviceHandle, uint ioControlCode, IntPtr inBuffer, int inBufferSize, IntPtr outBuffer, int outBufferSize, ref int bytesReturned, IntPtr overlapped);
    [DllImport("kernel32")]
    private static extern IntPtr CreateFile(string filename, uint desiredAccess, uint shareMode, IntPtr securityAttributes, int creationDisposition, int flagsAndAttributes, IntPtr templateFile);
    





    /// To eject pendrive
  public static string EjectPenDrive(char driveLetterCharacter)
    {
        string pathfordrive = "\\\\.\\" + driveLetterCharacter + ":";
        IntPtr handle = CreateFile(pathfordrive, GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
        if ((long)handle == -1)
        {
            return "Unable to open drive " + driveLetterCharacter;
        }

        int dummy = 0;
        DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, ref dummy, IntPtr.Zero);
        CloseHandle(handle);
        return "OK - Drive removed successfully..";
    }
}


  
   
Now we have done run the application and check the output.


 Here is the drive list.



First we will detect the pen drive.


Now click on eject the pen drive




Tags: ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

8 comments:

  1. Great Article and little Project. God Bless

    ReplyDelete
  2. Nice article.

    But the download link seems to be link another project?
    Is a ASP project.

    ReplyDelete
    Replies
    1. Sorry my mistake please check the correct url:
      https://drive.google.com/file/d/1jeLsCE7VuJ-I7z32VASmE_2kzXDMVaGc/view?usp=sharing

      Delete
  3. Possible to set password and write access video file

    ReplyDelete
    Replies
    1. Hi Thanks for visiting the blog. Will check and let you know

      Delete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top