vb.net filecopy (simple file manager)

darenkov

New member
Joined
Jul 9, 2005
Messages
3
Programming Experience
1-3
Hi,

I am a vb.net newbie and have been asked by my boss to create a small app that will allow him to select a file and have it copied to a different location.

I am not sure which common dialog to use?! I notice there is one for opening files but I don't want to actually open the file, I just want to allow him to select it and press "OK", and then to select a different location where he wants to move it to.

Does anyone have any suggestions? Should I perhaps be using a treeview control instead?

cheers
 
You need a SaveFileDialog ... i think i've already replied about certain topic ... try to search for it ... however if you cannot find solution feel free to ask for more help i'd be glad to help you .... Cheers ;)

Ah here is ... take a look at this thread http://www.vbdotnetforums.com/showthread.php?t=2743&highlight=SaveFileDialog

there is an attachment that could help you out ... :)
 
Hi thanks for the quick reply.

I am aware of the savefile dialog but I was hoping that there was something else without the save button, as I want to just get the file path of the original file, and have the user press ok. Once they have done this I want them to click another button which allows them to select a destination folder. Then I was going to get them to click on a "copy" or "move" file to location button depending on which they want to do.

Basically I just want to open a file dialog which allows them to select a file and retrieve the path information, but only give them the choice to press "OK" or "Cancel" and no other buttons. :)

Any further ideas?
 
Using the OpenFileDialog and SaveFileDialog doesn't actually open or save a file. It is still up to you to do that in code. It simply provides functionality that is appropriate for those processes. You should use an OpenFileDialog to allow the user to select a source file, then use a SaveFileDialog to allow the user to select a destination. You would then use code like the following to perform the copy operation:
VB.NET:
IO.File.Copy(Me.OpenFileDialog1.FileName, Me.SaveFileDialog1.FileName)
Edit:
You can change certain aspects of the appearance and behaviour of each of those dialogues. Check the help documentation for details.
 
jazz this up:
VB.NET:
Option Explicit On 
Option Strict On

Friend Class frmMain
	Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

	Public Sub New()
		MyBase.New()

		'This call is required by the Windows Form Designer.
		InitializeComponent()

		'Add any initialization after the InitializeComponent() call

	End Sub

	'Form overrides dispose to clean up the component list.
	Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
		If disposing Then
			If Not (components Is Nothing) Then
				components.Dispose()
			End If
		End If
		MyBase.Dispose(disposing)
	End Sub

	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer

	'NOTE: The following procedure is required by the Windows Form Designer
	'It can be modified using the Windows Form Designer.  
	'Do not modify it using the code editor.
	Private WithEvents txtSource As System.Windows.Forms.TextBox
	Private WithEvents btnSource As System.Windows.Forms.Button
	Private WithEvents btnPerform As System.Windows.Forms.Button
	Private WithEvents radMove As System.Windows.Forms.RadioButton
	Private WithEvents radCopy As System.Windows.Forms.RadioButton
	Private WithEvents lblSource As System.Windows.Forms.Label
	Private WithEvents lblDestination As System.Windows.Forms.Label
	Private WithEvents btnExit As System.Windows.Forms.Button
	Private WithEvents ofdSource As System.Windows.Forms.OpenFileDialog
	Private WithEvents sfdDestination As System.Windows.Forms.SaveFileDialog
	Private WithEvents txtDestination As System.Windows.Forms.TextBox
	Private WithEvents btnDestination As System.Windows.Forms.Button
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.txtSource = New System.Windows.Forms.TextBox
		Me.txtDestination = New System.Windows.Forms.TextBox
		Me.btnSource = New System.Windows.Forms.Button
		Me.btnDestination = New System.Windows.Forms.Button
		Me.btnPerform = New System.Windows.Forms.Button
		Me.radMove = New System.Windows.Forms.RadioButton
		Me.radCopy = New System.Windows.Forms.RadioButton
		Me.lblSource = New System.Windows.Forms.Label
		Me.lblDestination = New System.Windows.Forms.Label
		Me.btnExit = New System.Windows.Forms.Button
		Me.ofdSource = New System.Windows.Forms.OpenFileDialog
		Me.sfdDestination = New System.Windows.Forms.SaveFileDialog
		Me.SuspendLayout()
		'
		'txtSource
		'
		Me.txtSource.Location = New System.Drawing.Point(8, 40)
		Me.txtSource.Name = "txtSource"
		Me.txtSource.Size = New System.Drawing.Size(376, 20)
		Me.txtSource.TabIndex = 0
		Me.txtSource.Text = ""
		'
		'txtDestination
		'
		Me.txtDestination.Location = New System.Drawing.Point(8, 112)
		Me.txtDestination.Name = "txtDestination"
		Me.txtDestination.Size = New System.Drawing.Size(376, 20)
		Me.txtDestination.TabIndex = 1
		Me.txtDestination.Text = ""
		'
		'btnSource
		'
		Me.btnSource.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
		Me.btnSource.Location = New System.Drawing.Point(392, 40)
		Me.btnSource.Name = "btnSource"
		Me.btnSource.Size = New System.Drawing.Size(24, 20)
		Me.btnSource.TabIndex = 2
		Me.btnSource.Text = "..."
		'
		'btnDestination
		'
		Me.btnDestination.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
		Me.btnDestination.Location = New System.Drawing.Point(392, 112)
		Me.btnDestination.Name = "btnDestination"
		Me.btnDestination.Size = New System.Drawing.Size(24, 20)
		Me.btnDestination.TabIndex = 3
		Me.btnDestination.Text = "..."
		'
		'btnPerform
		'
		Me.btnPerform.Location = New System.Drawing.Point(24, 192)
		Me.btnPerform.Name = "btnPerform"
		Me.btnPerform.TabIndex = 4
		Me.btnPerform.Text = "&Perform"
		'
		'radMove
		'
		Me.radMove.Location = New System.Drawing.Point(136, 160)
		Me.radMove.Name = "radMove"
		Me.radMove.TabIndex = 5
		Me.radMove.Text = "&Move"
		'
		'radCopy
		'
		Me.radCopy.Checked = True
		Me.radCopy.Location = New System.Drawing.Point(16, 160)
		Me.radCopy.Name = "radCopy"
		Me.radCopy.TabIndex = 6
		Me.radCopy.TabStop = True
		Me.radCopy.Text = "&Copy"
		'
		'lblSource
		'
		Me.lblSource.Location = New System.Drawing.Point(8, 16)
		Me.lblSource.Name = "lblSource"
		Me.lblSource.TabIndex = 7
		Me.lblSource.Text = "Source File:"
		Me.lblSource.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
		'
		'lblDestination
		'
		Me.lblDestination.Location = New System.Drawing.Point(8, 88)
		Me.lblDestination.Name = "lblDestination"
		Me.lblDestination.TabIndex = 8
		Me.lblDestination.Text = "Destination:"
		Me.lblDestination.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
		'
		'btnExit
		'
		Me.btnExit.Location = New System.Drawing.Point(344, 192)
		Me.btnExit.Name = "btnExit"
		Me.btnExit.TabIndex = 9
		Me.btnExit.Text = "E&xit"
		'
		'ofdSource
		'
		Me.ofdSource.Title = "Select A File"
		'
		'sfdDestination
		'
		Me.sfdDestination.Title = "Select A Destination"
		'
		'frmMain
		'
		Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
		Me.ClientSize = New System.Drawing.Size(424, 223)
		Me.Controls.Add(Me.btnExit)
		Me.Controls.Add(Me.lblDestination)
		Me.Controls.Add(Me.lblSource)
		Me.Controls.Add(Me.radCopy)
		Me.Controls.Add(Me.radMove)
		Me.Controls.Add(Me.btnPerform)
		Me.Controls.Add(Me.btnDestination)
		Me.Controls.Add(Me.btnSource)
		Me.Controls.Add(Me.txtDestination)
		Me.Controls.Add(Me.txtSource)
		Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
		Me.MaximizeBox = False
		Me.Name = "frmMain"
		Me.Text = "Form1"
		Me.ResumeLayout(False)

	End Sub

#End Region

	Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		ofdSource.FileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\*.*"
		sfdDestination.FileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\*.*"
	End Sub

	Private Sub btnSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSource.Click
		Dim dlgResult As DialogResult = ofdSource.ShowDialog()
		If dlgResult <> DialogResult.Cancel Then txtSource.Text = ofdSource.FileName
	End Sub

	Private Sub btnDestination_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDestination.Click
		Dim dlgResult As DialogResult = sfdDestination.ShowDialog
		If dlgResult <> DialogResult.Cancel Then txtDestination.Text = sfdDestination.FileName
	End Sub

	Private Sub btnPerform_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPerform.Click
		If System.IO.File.Exists(txtSource.Text) = True Then
			Try
				If radCopy.Checked = True Then
				    System.IO.File.Copy(txtSource.Text, txtDestination.Text, True)
				Else
				    System.IO.File.Move(txtSource.Text, txtDestination.Text)
				End If
			Catch ex As Exception
			    MessageBox.Show("Can't copy or move!" & ControlChars.NewLine & ex.Message, "Can't copy or move...", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
		End If
	End Sub

	Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
		Me.Close()
	End Sub
End Class
 
Back
Top