Join 244,296 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 862 people online right now. Registration is fast and FREE... Join Now!
Hi, i'm new @ vb.net and i can't find how to open a FolderBrowserDialog, let the user select a portable device and then check every time if THAT portable device the user has selected is still in the computer, if not, then then the program runs another form
okok., look, i want to let the program check if the device is still inserted, thats all, but i can't make a script of it and can't find it anywhere can you help me?
this what i have now
CODE
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim cola As String
If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then cola = FolderBrowserDialog1.SelectedPath
Else 'als niet op OK werd geklikt 'gebeurt er niets en dit wordt aan gebruiker 'meegedeeld MessageBox.Show("You have to choose somthing, if you don't StickKey can't work!", "Fout", _ MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Me.Close() End If
End Sub Private Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 'HERE HAS TO BE THE SCRIPT THAT CHECKS IF THE DEVICE IS STILL INSERTED, 'or to say it in another way: a script how checks if the file/folder still can be found,
End Sub End Class
I'f tried to make let the program write a .bat file (with io.streamwriter) then run the .bat file that then the .bat file copy's programs to that folder, but it all crashed so can you write a code? or someone else?
Everyone here is helping in their free time, and sometimes people have more important things to do.
The following will help determine if a file or folder exists, so just have your project check to see whether the device directory is present, e.g f;/ or c:/ ....
CODE
Private Function DirExists() As Integer Dim DirName as String DirName ="Directory here" Dim Dir as New DirectoryInfo(DirName)
Return If Not Dir Exists Then MessageBox.Show("Device Not Present") Else MessageBox.Show("Device Found") End If End Function
It will need adapting for your project.
DirName can always be set through an inputbox, if needed. You can also use Dir.LastAccessTime (Other Functions also available, such as .CreationTime) to show information about the directory.
E.G, My laptop pics up USB sticks a F:/, i would simply set DirName = "F:/" to see if there is anything present as F:/
Hope this helps
This post has been edited by AdamR: 1 Oct, 2008 - 05:50 AM
Everyone here is helping in their free time, and sometimes people have more important things to do.
The following will help determine if a file or folder exists, so just have your project check to see whether the device directory is present, e.g f;/ or c:/ ....
CODE
Private Function DirExists() As Integer Dim DirName as String DirName ="Directory here" Dim Dir as New DirectoryInfo(DirName)
Return If Not Dir Exists Then MessageBox.Show("Device Not Present") Else MessageBox.Show("Device Found") End If End Function
It will need adapting for your project.
DirName can always be set through an inputbox, if needed. You can also use Dir.LastAccessTime (Other Functions also available, such as .CreationTime) to show information about the directory.
E.G, My laptop pics up USB sticks a F:/, i would simply set DirName = "F:/" to see if there is anything present as F:/
Hope this helps
really thank you man, if you need help you always can send a pm to me, i still have got some problems: Error 2 'End If' must be preceded by a matching 'If'. Form1.vb 34 1 WindowsApplication1 Error 1 'ElseIf' must be preceded by a matching 'If' or 'ElseIf'. Form1.vb 32 1 WindowsApplication1
I've tried several things (like doing what the errors say, changing ElseIf and all that kind of stuff) but it did'nt worked and, now i will have patience
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.
Private Function DirExists() As Integer Dim DirName As String DirName = "c:\WINDOWS" Dim Dir As New DirectoryInfo(DirName)
If Not DirExists Then MessageBox.Show("Device Found") Else MessageBox.Show("Device Not Found") End If End Function
Worked for me, although i think it would be a better approach to try out the system io watcher as magicmonkey said
Yeah, it think i do the system io watcher why? System.StackOverflowException was unhandled InnerException: An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
I don't even know what that means, so i'm going to try that thank you guys!
Yeah, it think i do the system io watcher why? System.StackOverflowException was unhandled InnerException: An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
I don't even know what that means, so i'm going to try that thank you guys!
That means that your code went into an endless loop. you call one function that calls another function that calls the original function again is a typical example. When you run into this exception look at the Stack Trace window in Visual Studio and you can see how your code went into the endless loop.
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!!
look @ this i've it from msdn and changed it
CODE
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.
' 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: 2 Oct, 2008 - 09:17 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.
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
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!
CODE
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
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!
CODE
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,