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#.
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.
First we will detect the pen drive.
Now click on eject the pen drive
Great Article and little Project. God Bless
ReplyDeleteThanks for your valuable comment.
DeleteGreat sir..
ReplyDeleteThanks for you valuable comment. .....
DeleteNice article.
ReplyDeleteBut the download link seems to be link another project?
Is a ASP project.
Sorry my mistake please check the correct url:
Deletehttps://drive.google.com/file/d/1jeLsCE7VuJ-I7z32VASmE_2kzXDMVaGc/view?usp=sharing
Possible to set password and write access video file
ReplyDeleteHi Thanks for visiting the blog. Will check and let you know
Delete