Create Directory with VS2008

R

Robert

Greetings
New to Visual Studio 2008.
Always us VB6.0 SP5.

I am trying to copy files from A to B.
I select a directory, and copy files. Works ok, but it does not create the
directory first.

snippet.
Private Sub cmdTo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdTo.Click

'Open the Dialog Box to Copy Files.

With FolderBrowserDialog1

..ShowNewFolderButton = True

..Description = "Select the Location for Files to be copied to."

..RootFolder = Environment.SpecialFolder.Desktop

End With

' If a file is not opened, then set the initial directory to the

' FolderBrowserDialog.SelectedPath value.

If lblTo.Text = "" Then

lblTo.Text = FolderBrowserDialog1.SelectedPath

lblTo.Text = Nothing

End If

' Display the openFile dialog.

Dim result As DialogResult = FolderBrowserDialog1.ShowDialog()

' OK button was pressed.

If (result = DialogResult.OK) Then

lblTo.Text = FolderBrowserDialog1.SelectedPath

' Cancel button was pressed.

ElseIf (result = DialogResult.Cancel) Then

Return

End If

Exit Sub

End Sub

Private Sub cmdCopy_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdCopy.Click

''''My.Computer.FileSystem.CreateDirectory(lblTo.Text & "\" &
lblFrom.Text)"""" Does not work

My.Computer.FileSystem.CopyDirectory(lblFrom.Text, lblTo.Text, _

FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing)

Exit Sub

I have added this line

My.Computer.FileSystem.CreateDirectory(lblTo.Text & "\" & lblFrom.Text)

Does not except this. In VB6 I could create a directory with from "\" to.

How do you create a directory, when selection is made.
 
C

Cor Ligthert[MVP]

Robert,

I first will try you to learn how to fish and then give you the fish.

If you test this like you do and it does not work, then simplify your
problem, in this case by instead using a string instead of a variable you
see direct the problem.

My.Computer.FileSystem.CreateDirectory("C:\Robert")

I tried this and it created a folder on Drive C Robert.

As I already expected (but I seldom use the My Class) does it show direct
the problem for you.

Cor
 
Z

Zebra Code

Robert said:
New to Visual Studio 2008.
Always us VB6.0 SP5.
I am trying to copy files from A to B.
I select a directory, and copy files. Works ok, but it does not create
the directory first.
snippet.
Private Sub cmdTo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdTo.Click
'Open the Dialog Box to Copy Files.
With FolderBrowserDialog1
.ShowNewFolderButton = True
.Description = "Select the Location for Files to be copied to."
.RootFolder = Environment.SpecialFolder.Desktop
End With
' If a file is not opened, then set the initial directory to the
' FolderBrowserDialog.SelectedPath value.
If lblTo.Text = "" Then
lblTo.Text = FolderBrowserDialog1.SelectedPath
lblTo.Text = Nothing
End If
' Display the openFile dialog.
Dim result As DialogResult = FolderBrowserDialog1.ShowDialog()
' OK button was pressed.
If (result = DialogResult.OK) Then
lblTo.Text = FolderBrowserDialog1.SelectedPath
' Cancel button was pressed.
ElseIf (result = DialogResult.Cancel) Then
Return
End If
Exit Sub
End Sub
Private Sub cmdCopy_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdCopy.Click
''''My.Computer.FileSystem.CreateDirectory(lblTo.Text & "\" &
lblFrom.Text)"""" Does not work
My.Computer.FileSystem.CopyDirectory(lblFrom.Text, lblTo.Text, _
FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing)
Exit Sub
I have added this line
My.Computer.FileSystem.CreateDirectory(lblTo.Text & "\" & lblFrom.Text)
Does not except this. In VB6 I could create a directory with from "\" to.
How do you create a directory, when selection is made.

Imports System.IO

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Not Directory.Exists(Path.Combine(Application.StartupPath,
"test_dir")) Then _
Directory.CreateDirectory(Path.Combine(Application.StartupPath,
"test_dir"))
End Sub

if you run it in the vs ide, in your bin\debug folder you should find the
test_dir subfolder.
if you run the exe, the subfolder should be in the exe's path.
i just had a look at path combine, looks rather powerful.
btw, i'm also used to classic vb :)

hth, ZC
 
R

Robert

That would work, if I now the Directory Name that is being copied.

As I stated, It is selected from the FolderBrower1.

I am able to get the complete path, but not just the selected Directory
Name.

Thanks away for the sample.
I will keep it if I ever have the need to hardcode a copy to!
 
J

J.B. Moreno

Robert said:
That would work, if I now the Directory Name that is being copied.

As I stated, It is selected from the FolderBrower1.

I am able to get the complete path, but not just the selected Directory
Name.

Thanks away for the sample.
I will keep it if I ever have the need to hardcode a copy to!

Cor's code wasn't just for when you have a hardcoded path.

He just simplified the code to make it more clear what was happening.
If you use a variable in place of the hard coded string, and it doesn't
work, then there's only one answer -- your variable doesn't contain the
right string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top