Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

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!




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

2 Pages V  1 2 >  
Reply to this topicStart new topic

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

japie
30 Sep, 2008 - 06:28 AM
Post #1

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 15


My Contributions
Hi, i'm new @ vb.net pirate.gif
and i can't find how to open a FolderBrowserDialog, crazy.gif 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

Sorry for my bad english i'm dutch

User is offlineProfile CardPM
+Quote Post


Jayman
RE: How To Let The User Select A File And Then Open Let The Program Look I
30 Sep, 2008 - 07:48 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,063



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Use the ShowDialog function to display the dialog window.
CODE

' Display the openFile dialog.
Dim result As DialogResult = openFileDialog1.ShowDialog()


Post the code that you are attempting to use to verify that the drive exists and we will help you get it working.


User is online!Profile CardPM
+Quote Post

japie
RE: How To Let The User Select A File And Then Open Let The Program Look I
30 Sep, 2008 - 08:18 AM
Post #3

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 15


My Contributions
QUOTE(jayman9 @ 30 Sep, 2008 - 08:48 AM) *

Use the ShowDialog function to display the dialog window.
CODE

' Display the openFile dialog.
Dim result As DialogResult = openFileDialog1.ShowDialog()


Post the code that you are attempting to use to verify that the drive exists and we will help you get it working.

Ewm,
how to make a string of result?
i only get a "1"
User is offlineProfile CardPM
+Quote Post

Jayman
RE: How To Let The User Select A File And Then Open Let The Program Look I
30 Sep, 2008 - 08:51 AM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,063



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Why do you need to make a string of result?
User is online!Profile CardPM
+Quote Post

japie
RE: How To Let The User Select A File And Then Open Let The Program Look I
30 Sep, 2008 - 11:19 AM
Post #5

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 15


My Contributions
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?

User is offlineProfile CardPM
+Quote Post

japie
RE: How To Let The User Select A File And Then Open Let The Program Look I
30 Sep, 2008 - 10:14 PM
Post #6

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 15


My Contributions
hello??
can anywhone help me???
User is offlineProfile CardPM
+Quote Post

AdamR
RE: How To Let The User Select A File And Then Open Let The Program Look I
1 Oct, 2008 - 05:46 AM
Post #7

D.I.C Head
**

Joined: 23 Sep, 2008
Posts: 144



Thanked: 7 times
My Contributions
Japie, please, don't rush smile.gif Patience Is A Virtue

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 smile.gif

This post has been edited by AdamR: 1 Oct, 2008 - 05:50 AM
User is offlineProfile CardPM
+Quote Post

japie
RE: How To Let The User Select A File And Then Open Let The Program Look I
1 Oct, 2008 - 09:19 AM
Post #8

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 15


My Contributions
QUOTE(AdamR @ 1 Oct, 2008 - 06:46 AM) *

Japie, please, don't rush smile.gif Patience Is A Virtue

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 smile.gif

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 tongue.gif
User is offlineProfile CardPM
+Quote Post

AdamR
RE: How To Let The User Select A File And Then Open Let The Program Look I
1 Oct, 2008 - 09:42 AM
Post #9

D.I.C Head
**

Joined: 23 Sep, 2008
Posts: 144



Thanked: 7 times
My Contributions
Hey, no problem

(It says this post was helpful under the posts, click them when people help you out, its a thanks biggrin.gif)

Right, can you copy and paste the sub procedures where the errors are onto here and i'll take a look smile.gif
User is offlineProfile CardPM
+Quote Post

japie
RE: How To Let The User Select A File And Then Open Let The Program Look I
1 Oct, 2008 - 10:14 AM
Post #10

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 15


My Contributions
Fat sencteces, gives errors



Dim DirName As String
DirName = FolderBrowserDialog1.SelectedPath
Dim Dir As New IO.DirectoryInfo(DirName)

Return
MessageBox.Show("Device Not Present")
Else
MessageBox.Show("Device Found")
End If
User is offlineProfile CardPM
+Quote Post

japie
RE: How To Let The User Select A File And Then Open Let The Program Look I
1 Oct, 2008 - 10:25 AM
Post #11

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 15


My Contributions
this to
Return If Not Dir Exists Then
User is offlineProfile CardPM
+Quote Post

magicmonkey
RE: How To Let The User Select A File And Then Open Let The Program Look I
1 Oct, 2008 - 11:25 AM
Post #12

D.I.C Regular
***

Joined: 12 Sep, 2008
Posts: 484



Thanked: 93 times
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

AdamR
RE: How To Let The User Select A File And Then Open Let The Program Look I
1 Oct, 2008 - 11:31 AM
Post #13

D.I.C Head
**

Joined: 23 Sep, 2008
Posts: 144



Thanked: 7 times
My Contributions
CODE

    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 smile.gif
User is offlineProfile CardPM
+Quote Post

japie
RE: How To Let The User Select A File And Then Open Let The Program Look I
2 Oct, 2008 - 06:39 AM
Post #14

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 15


My Contributions
QUOTE(AdamR @ 1 Oct, 2008 - 12:31 PM) *

CODE

    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 smile.gif

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!
User is offlineProfile CardPM
+Quote Post

magicmonkey
RE: How To Let The User Select A File And Then Open Let The Program Look I
2 Oct, 2008 - 08:18 AM
Post #15

D.I.C Regular
***

Joined: 12 Sep, 2008
Posts: 484



Thanked: 93 times
My Contributions
QUOTE(japie @ 2 Oct, 2008 - 09:39 AM) *

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.
User is offlineProfile CardPM
+Quote Post

japie
RE: How To Let The User Select A File And Then Open Let The Program Look I
2 Oct, 2008 - 09:10 AM
Post #16

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 15


My Contributions
QUOTE(magicmonkey @ 1 Oct, 2008 - 12:25 PM) *

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.gif devil.gif devil.gif devil.gif devil.gif devil.gif splat.gif ohmy.gif

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.


        ' 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: 2 Oct, 2008 - 09:17 AM
User is offlineProfile CardPM
+Quote Post

magicmonkey
RE: How To Let The User Select A File And Then Open Let The Program Look I
2 Oct, 2008 - 10:50 AM
Post #17

D.I.C Regular
***

Joined: 12 Sep, 2008
Posts: 484



Thanked: 93 times
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

japie
RE: How To Let The User Select A File And Then Open Let The Program Look I
2 Oct, 2008 - 11:06 AM
Post #18

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 15


My Contributions
QUOTE(magicmonkey @ 2 Oct, 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.

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
User is offlineProfile CardPM
+Quote Post

magicmonkey
RE: How To Let The User Select A File And Then Open Let The Program Look I
2 Oct, 2008 - 12:37 PM
Post #19

D.I.C Regular
***

Joined: 12 Sep, 2008
Posts: 484



Thanked: 93 times
My Contributions
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

User is offlineProfile CardPM
+Quote Post

japie
RE: How To Let The User Select A File And Then Open Let The Program Look I
3 Oct, 2008 - 06:33 AM
Post #20

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 15


My Contributions
QUOTE(magicmonkey @ 2 Oct, 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!

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,

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 04:42PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month