How to let the user select a file and then open let the program look i

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 5904 Views - Last Post: 03 October 2008 - 10:39 AM Rate Topic: -----

#16 japie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-September 08

Re: How to let the user select a file and then open let the program look i

Posted 02 October 2008 - 10:10 AM

View Postmagicmonkey, on 1 Oct, 2008 - 12:25 PM, said:

Instead of using a timer to check there is a built in class in .NET named IO.FileSystemWatcher that can raise an event when ever a directory is changed or a file inside the directory is changed.

Now I am not sure what happens if you remove a thumb drive, but might be worth playing with this object as it might provide you with some powerful options.

b.t.w. :
Fantastic,
but it CANT FIND IF THE DEVICE IS OFFLINE!!
:devil: :devil: :devil: :devil: :devil: :devil: :splat: :o

look @ this
i've it from msdn
and changed it

   Private Sub Run()

		Dim args As String
		args = FolderBrowserDialog1.SelectedPath
		' Create a new FileSystemWatcher and set its properties.
		Dim watcher As New FileSystemWatcher()
		watcher.Path = args
		' Watch for changes in LastAccess and LastWrite times, and
		' the renaming of files or directories. 
		watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
		' Only watch text files.


		' Add event handlers.
		AddHandler watcher.Changed, AddressOf onchanged
		AddHandler watcher.Created, AddressOf onchanged
		AddHandler watcher.Deleted, AddressOf onchanged
		AddHandler watcher.Renamed, AddressOf OnRenamed
		AddHandler watcher.Disposed, AddressOf Onoffline
		AddHandler watcher.Error, AddressOf Onoffline

		' Begin watching.
		watcher.EnableRaisingEvents = True

		' Wait for the user to quit the program.
		label1.Text = "FileSystemWatcher() ONLINE"

	End Sub

	' Define the event handlers.
	Private Sub onchanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
		' Specify what is done when a file is changed, created, or deleted.
		label1.Text = "File: " & e.FullPath & " " & e.ChangeType
	End Sub

	Private Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs)
		' Specify what is done when a file is renamed.
		label1.Text = "File: {0} renamed to {1}" & e.OldFullPath & e.FullPath
	End Sub
	Private Sub Onoffline(ByVal source As Object, ByVal e As ErrorEventArgs)
		llabel1.Text = FolderBrowserDialog1.SelectedPath + " Is offline!"
	End Sub



i've added this into my script and it redirects to other subs
it works fantastic, if a change a file @ my usb-stick,
then he reports everything
BUT
there is 1 problem
if i pull my stick out my pc
it says nothing
so i did this all for nothing or....?

This post has been edited by japie: 02 October 2008 - 10:17 AM

Was This Post Helpful? 0
  • +
  • -

#17 magicmonkey  Icon User is offline

  • D.I.C Regular

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

Re: How to let the user select a file and then open let the program look i

Posted 02 October 2008 - 11:50 AM

hehe, didn't you read my disclaimer, 'I don't know what happens when you remove a thumb drive'.

And you never do something for nothing in programming because you just learned how to use a great class within .NET. I will play around with it a bit and see if I can find a solution.
Was This Post Helpful? 0
  • +
  • -

#18 japie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-September 08

Re: How to let the user select a file and then open let the program look i

Posted 02 October 2008 - 12:06 PM

View Postmagicmonkey, on 2 Oct, 2008 - 11:50 AM, said:

hehe, didn't you read my disclaimer, 'I don't know what happens when you remove a thumb drive'.

And you never do something for nothing in programming because you just learned how to use a great class within .NET. I will play around with it a bit and see if I can find a solution.

Yeah,
I think i hold it in my script,
it detects changes and rename's and all that kind of stuff
but i still need that script how checks if the device is inserted,
i should look @ the that endless loop
and if you vind a solution then post it
thank you
Was This Post Helpful? 0
  • +
  • -

#20 magicmonkey  Icon User is offline

  • D.I.C Regular

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

Re: How to let the user select a file and then open let the program look i

Posted 02 October 2008 - 01:37 PM

Okay, I spent a bit to much time on this, but windows does send a message to all applications when a drive is added or removed. I have put this mess together for you just paste it into a new form and watch the Output window in VS as you remove and add your thumb drive. You can take it from here!

Public Class Form1
	Private Const WM_DEVICECHANGED As Integer = &H219
	Private Const DBT_DEVICEARRIVAL As Integer = &H8000
	Private Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004
	Private Const DBT_DEVTYP_VOLUME As Integer = &H2

	Private Structure DEV_BROADCAST_HDR
		Public DBCH_SIZE As Integer
		Public DBCH_DEVICETYPE As Integer
		Public DBCH_RESERVED As Integer
	End Structure

	Private Structure DEV_BROADCAST_VOLUME
		Public DBCV_SIZE As Integer
		Public DBCV_DEVICETYPE As Integer
		Public DBCV_RESERVED As Integer
		Public DBCV_UNITMASK As Integer
		Public DBCV_FLAGS As Integer
	End Structure

	Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
		MyBase.WndProc(m)
		Select m.Msg
			Case WM_DEVICECHANGED
				Select Case m.WParam.ToInt32
					Case DBT_DEVICEARRIVAL
						Debug.WriteLine("Device Added")
						Dim getHDR As DEV_BROADCAST_HDR = m.GetLParam(GetType(DEV_BROADCAST_HDR))
						If getHDR.DBCH_DEVICETYPE = DBT_DEVTYP_VOLUME Then
							Dim getVolume As DEV_BROADCAST_VOLUME = m.GetLParam(GetType(DEV_BROADCAST_VOLUME))
							Dim Drives As List(Of String) = GetDrives(getVolume)
							For Each selDrive As String In Drives
								Debug.WriteLine(selDrive)
							Next
						End If
					Case DBT_DEVICEREMOVECOMPLETE
						Debug.WriteLine("Device Removed")
						Dim getHDR As DEV_BROADCAST_HDR = m.GetLParam(GetType(DEV_BROADCAST_HDR))
						If getHDR.DBCH_DEVICETYPE = DBT_DEVTYP_VOLUME Then
							Dim getVolume As DEV_BROADCAST_VOLUME = m.GetLParam(GetType(DEV_BROADCAST_VOLUME))
							Dim Drives As List(Of String) = GetDrives(getVolume)
							For Each selDrive As String In Drives
								Debug.WriteLine(selDrive)
							Next
						End If
				End Select

		End Select
	End Sub

	Private Function GetDrives(ByVal DEV_BROADCAST_VOLUME As DEV_BROADCAST_VOLUME) As List(Of String)
		Dim Drives As New List(Of String)
		Dim bitVolumes As New BitArray(New Integer() {DEV_BROADCAST_VOLUME.DBCV_UNITMASK})
		For indexVolume As Integer = 0 To 31
			If bitVolumes(indexVolume) Then
				Drives.Add(Chr(Asc("A") + indexVolume))
			End If
		Next
		Return Drives
	End Function
End Class


Was This Post Helpful? 1

#21 japie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-September 08

Re: How to let the user select a file and then open let the program look i

Posted 03 October 2008 - 07:33 AM

View Postmagicmonkey, on 2 Oct, 2008 - 01:37 PM, said:

Okay, I spent a bit to much time on this, but windows does send a message to all applications when a drive is added or removed. I have put this mess together for you just paste it into a new form and watch the Output window in VS as you remove and add your thumb drive. You can take it from here!

Public Class Form1
	Private Const WM_DEVICECHANGED As Integer = &H219
	Private Const DBT_DEVICEARRIVAL As Integer = &H8000
	Private Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004
	Private Const DBT_DEVTYP_VOLUME As Integer = &H2

	Private Structure DEV_BROADCAST_HDR
		Public DBCH_SIZE As Integer
		Public DBCH_DEVICETYPE As Integer
		Public DBCH_RESERVED As Integer
	End Structure

	Private Structure DEV_BROADCAST_VOLUME
		Public DBCV_SIZE As Integer
		Public DBCV_DEVICETYPE As Integer
		Public DBCV_RESERVED As Integer
		Public DBCV_UNITMASK As Integer
		Public DBCV_FLAGS As Integer
	End Structure

	Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
		MyBase.WndProc(m)
		Select m.Msg
			Case WM_DEVICECHANGED
				Select Case m.WParam.ToInt32
					Case DBT_DEVICEARRIVAL
						Debug.WriteLine("Device Added")
						Dim getHDR As DEV_BROADCAST_HDR = m.GetLParam(GetType(DEV_BROADCAST_HDR))
						If getHDR.DBCH_DEVICETYPE = DBT_DEVTYP_VOLUME Then
							Dim getVolume As DEV_BROADCAST_VOLUME = m.GetLParam(GetType(DEV_BROADCAST_VOLUME))
							Dim Drives As List(Of String) = GetDrives(getVolume)
							For Each selDrive As String In Drives
								Debug.WriteLine(selDrive)
							Next
						End If
					Case DBT_DEVICEREMOVECOMPLETE
						Debug.WriteLine("Device Removed")
						Dim getHDR As DEV_BROADCAST_HDR = m.GetLParam(GetType(DEV_BROADCAST_HDR))
						If getHDR.DBCH_DEVICETYPE = DBT_DEVTYP_VOLUME Then
							Dim getVolume As DEV_BROADCAST_VOLUME = m.GetLParam(GetType(DEV_BROADCAST_VOLUME))
							Dim Drives As List(Of String) = GetDrives(getVolume)
							For Each selDrive As String In Drives
								Debug.WriteLine(selDrive)
							Next
						End If
				End Select

		End Select
	End Sub

	Private Function GetDrives(ByVal DEV_BROADCAST_VOLUME As DEV_BROADCAST_VOLUME) As List(Of String)
		Dim Drives As New List(Of String)
		Dim bitVolumes As New BitArray(New Integer() {DEV_BROADCAST_VOLUME.DBCV_UNITMASK})
		For indexVolume As Integer = 0 To 31
			If bitVolumes(indexVolume) Then
				Drives.Add(Chr(Asc("A") + indexVolume))
			End If
		Next
		Return Drives
	End Function
End Class


Thnx!
I still got 1 question
it looks fine,
no errors and stuff
but..........
how can i RUN it?
it's protected, so i can't do call WndProc
so can you tell me,
Was This Post Helpful? 0
  • +
  • -

#22 magicmonkey  Icon User is offline

  • D.I.C Regular

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

Re: How to let the user select a file and then open let the program look i

Posted 03 October 2008 - 10:39 AM

You dont want to call it, is actually gets called by windows. What you could do is maybe call another method and pass it the list of drives that were attached/removed.

So instead of listing the drives in the output window

  For Each selDrive As String In Drives
								Debug.WriteLine(selDrive)
							Next



You should call some sub like DrivesRemoved(Drives as List(Of String)) and DrivesAdded(Drives as List(Of String)). Let me update it for you, this will clean it up a bit.

Public Class Form1
#Region "Device Changed Code"
    Private Const WM_DEVICECHANGED As Integer = &H219
    Private Const DBT_DEVICEARRIVAL As Integer = &H8000
    Private Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004
    Private Const DBT_DEVTYP_VOLUME As Integer = &H2

    Private Structure DEV_BROADCAST_HDR
        Public DBCH_SIZE As Integer
        Public DBCH_DEVICETYPE As Integer
        Public DBCH_RESERVED As Integer
    End Structure

    Private Structure DEV_BROADCAST_VOLUME
        Public DBCV_SIZE As Integer
        Public DBCV_DEVICETYPE As Integer
        Public DBCV_RESERVED As Integer
        Public DBCV_UNITMASK As Integer
        Public DBCV_FLAGS As Integer
    End Structure

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        Select Case m.Msg
            Case WM_DEVICECHANGED
                Select Case m.WParam.ToInt32
                    Case DBT_DEVICEARRIVAL
                        Dim getHDR As DEV_BROADCAST_HDR = m.GetLParam(GetType(DEV_BROADCAST_HDR))
                        If getHDR.DBCH_DEVICETYPE = DBT_DEVTYP_VOLUME Then
                            Dim getVolume As DEV_BROADCAST_VOLUME = m.GetLParam(GetType(DEV_BROADCAST_VOLUME))
                            Dim Drives As List(Of String) = GetDrives(getVolume)
                            DrivesAdded(Drives)
                        End If
                    Case DBT_DEVICEREMOVECOMPLETE
                        Dim getHDR As DEV_BROADCAST_HDR = m.GetLParam(GetType(DEV_BROADCAST_HDR))
                        If getHDR.DBCH_DEVICETYPE = DBT_DEVTYP_VOLUME Then
                            Dim getVolume As DEV_BROADCAST_VOLUME = m.GetLParam(GetType(DEV_BROADCAST_VOLUME))
                            Dim Drives As List(Of String) = GetDrives(getVolume)
                            DrivesRemoved(Drives)
                        End If
                End Select
        End Select
    End Sub

    Private Function GetDrives(ByVal DEV_BROADCAST_VOLUME As DEV_BROADCAST_VOLUME) As List(Of String)
        Dim Drives As New List(Of String)
        Dim bitVolumes As New BitArray(New Integer() {DEV_BROADCAST_VOLUME.DBCV_UNITMASK})
        For indexVolume As Integer = 0 To 31
            If bitVolumes(indexVolume) Then
                Drives.Add(Chr(Asc("A") + indexVolume))
            End If
        Next
        Return Drives
    End Function
#End Region

    Private Sub DrivesAdded(ByVal Drives As List(Of String))
        'Add your logic here
        Debug.WriteLine("Device Added")
        For Each selDrive As String In Drives
            Debug.WriteLine(selDrive)
        Next
    End Sub

    Private Sub DrivesRemoved(ByVal Drives As List(Of String))
        'Add your logic here
        Debug.WriteLine("Device Removed")
        For Each selDrive As String In Drives
            Debug.WriteLine(selDrive)
        Next
    End Sub
End Class

So add you logic in the DrivesAdded and DrivesRemoved subs. Hopefully that is clearer.


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2