Format Usb Drive

Format Usb Drive Programmically in vb.net

Page 1 of 1

5 Replies - 13830 Views - Last Post: 02 December 2008 - 06:27 AM Rate Topic: -----

#1 mannkaraja  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 26-November 08

Format Usb Drive

Post icon  Posted 01 December 2008 - 06:28 AM

Hello everybody

i want to do format usb drive from vb.net code. have u any idea then let me know.

i am using following code..

.....
Private Declare Function SHFormatDrive Lib "shell32" (ByVal hwnd As Long, ByVal iDrive As Long, ByVal iCapacity As Long, ByVal iFormatType As Long) As Long



Dim ret As Long
		'' I want a full format
	  ret = SHFormatDrive(hWnd, 4, -1, 1)	

	  Select Case ret
		  Case -1
			MsgBox("Error during format operation")
		  Case -2
			  MsgBox("Operation canceled by user")
		   Case -3
			   MsgBox("This drive cannot be formatted")
		   Case Else
			   MsgBox("Done")

.....

at call function SHFormat i have to pass "Me.hWnd" .. so please if any body know how to do this using vb.net..
and hWnd is passed for parent execution
as per my knowledge "Me" is not supported

Please help me out or if you have other idea then also tell me


Thanks in advance

Is This A Good Question/Topic? 1
  • +

Replies To: Format Usb Drive

#2 VB Crash  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 130
  • Joined: 28-November 08

Re: Format Usb Drive

Posted 01 December 2008 - 09:29 AM

Plase post your code like this..Posted Image
Try make *.bat file and code inside: Format G:/q

Vace
Was This Post Helpful? 0
  • +
  • -

#3 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Format Usb Drive

Posted 01 December 2008 - 01:37 PM

You can get the current forms handle with Me.Handle.ToInt32(). I think you want to change the long to integers as well. You would of used long in vb6 however in .net an integer is 32bits and a long is 64bits.
Was This Post Helpful? 0
  • +
  • -

#4 mannkaraja  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 26-November 08

Re: Format Usb Drive

Posted 01 December 2008 - 09:11 PM

View Postmagicmonkey, on 1 Dec, 2008 - 12:37 PM, said:

You can get the current forms handle with Me.Handle.ToInt32(). I think you want to change the long to integers as well. You would of used long in vb6 however in .net an integer is 32bits and a long is 64bits.


hi , thnxs for your reply. it helpful to me . but code not run successful

 
Dim ret As Long
		'' I want a full format
		ret = SHFormatDrive(Me.Handle.ToInt32(), 9, 0, 0)

		Select Case ret
			Case -1
				MsgBox("Error during format operation")
			Case -2
				MsgBox("Operation canceled by user")
			Case -3
				MsgBox("This drive cannot be formatted")
			Case Else
				MsgBox("Done")
		End Select
	End Sub




here i passed "9" for drive named "i" as 0=A , 1= B so I =9,
but it give error: Error during format operation..
can you please tell me whats wrong with this?
thanks in advance

** Edit ** :code:
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5304
  • View blog
  • Posts: 27,198
  • Joined: 10-May 07

Re: Format Usb Drive

Posted 01 December 2008 - 09:16 PM

View PostVB Crash, on 1 Dec, 2008 - 10:29 AM, said:

Try make *.bat file and code inside: Format G:/q

I would put a little more effort into it than that. What if you have a friends flash drive in, & it bumps yours to the next drive letter.

Sorry friend, I just formatted your flash drive.

You can use the Windows API commands GetLogicalDriveStrings & GetDriveType to determine which drives on your system are removable type.
Was This Post Helpful? 0
  • +
  • -

#6 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Format Usb Drive

Posted 02 December 2008 - 06:27 AM

If you look it up at PINVOKE.NET it mentions some default values you should use.

This post has been edited by magicmonkey: 02 December 2008 - 06:27 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1