Shell Method

How can I load a folder up in the windows explorer?
[52 byte] By [uk_boy] at [2007-12-16]
# 1


Dim pr As New Process
pr.StartInfo.FileName =
"c:\"
pr.Start()

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
Is it possible to get a user to insert a folder address in a text box click on a button and get the folder to load?
uk_boy at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
Of course. Just put that code in the button's click event and take the path string from the textbox. You should definitely check that the folder exists first, though, by calling IO.Directory.Exists. You might even like to have the user select a folder using a FolderBrowserDialog rather than typing directly, or perhaps include both options.
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
Well I put the code as:
-

Dim pr As New Process

pr.StartInfo.FileName = Command.file1

pr.Start()

--

and I also tried:

-

Dim ob1

ob1 = Command.file1

Dim pr As New Process

pr.StartInfo.FileName = ob1

pr.Start()

--

but neither work, can someone give me a bit of help?

uk_boy at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
Assuming Command.file1 is of type String and contains a folder path, try this:

If IO.Directory.Exists(Command.file1) Then
Process.Start(Command.file1)
Else
MessageBox.Show("The folder """ & Command.file1 & """ does not exist.")
End If


jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6
It still does not work!! Command.file1 is a textbox in a form. When I run it I get an error saying that it is not a string. Can I change the textbox so it is regonized as a string?
uk_boy at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
use the .ToString() function to return a string for most types.
Try this code out.


Dim sFile as String = Command.file1.Text.ToString()
If IO.File.Exists(sFile) Then
Dim pr As New Process
pr.StartInfo.FileName = sFile
pr.Start()

End If

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8
uk_boy wrote:
It still does not work!! Command.file1 is a textbox in a form. When I run it I get an error saying that it is not a string. Can I change the textbox so it is regonized as a string?
If it is a TextBox then you need to use its Text property to get the string it contains. I suggest you explore the classes you are using a little bit.
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9

I explored the classes but still I could not do it. For some reason the textbox is still not being converted into a string so when it gets to the:

"IO.File.Exists(sFile)"

It takes it as false so no action happens. How can I fix this problem?

uk_boy at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 10
If it's stopping there, that means the file they are typing doesn't exits.

Can you paste some of your code? Just the relevant stuff, and i can help you out further.
Dustin.

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 11
For my code I am using this:

Dim sFile as String = Command.file1.Text.ToString()

If IO.File.Exists(sFile) Then
Dim pr As New
Process
pr.StartInfo.FileName = sFile
pr.Start()
Else
MessageBox.Show("The folder """ & Command.file1 & """ does not exist.")
End If

To test my code I put "C:\" in the command.file1 which is a textbox, I also tried "C:\Program Files" both these files exist but both these things brought up the else statement.

uk_boy at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 12
Well, it seems to me your not passing a file into the textbox, your passing a directory! There's a difference.

If you want to check if a directory exists, then use...

If IO.Directory.Exists() Then ....
IO.File.Exists() is used for checking if FILES exist! Not directories.

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 13
Oh it still does not work this what my code is at the moment:

Dim sFile As String = Me.TextBox.ToString()

If IO.Directory.Exists(sFile) Then

Dim pr As New Process

pr.StartInfo.FileName = sFile

pr.Start()

Else

MessageBox.Show("The folder """ & TextBox.Text & """ does not exist.")

End If

But it still seems not to be working it always jumps straight to the else part even with folders that I know exist.

uk_boy at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 14
Your first line, should be...

Dim sFile As String = Me.TextBox.Text.ToString()

Don't forget to add the .Text before the .ToString().

If that doesn't help, debug the code, and check the value of sFile at the If statement.

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...