I am trying to create a feature in my program which will allow a user to use the opendialog to browse for a pdf,jpeg,bmp file.
Select the file and then save a copy of that file to another location.
I currently have a way of doing this with some code, from a sample off msdn. However i feel this is not the best way to do it and it is not working 100% at present.
my current form has two buttons: "Open File" button and "Save File" button, it also has a textbox.
CODE
Public Class frmUpload
Private filename As String
Private Sub openTextFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openTextFile.Click
Try
With odlgTextFile
' Check to ensure that the selected file exists. Dialog box displays
' a warning otherwise.
.CheckFileExists = True
' Check to ensure that the selected path exists. Dialog box displays
' a warning otherwise.
.CheckPathExists = True
' Get or set default extension. Doesn't include the leading ".".
.DefaultExt = "PDF"
' Return the file referenced by a link? If False, simply returns the selected link
' file. If True, returns the file linked to the LNK file.
.DereferenceLinks = True
' Just as in VB6, use a set of pairs of filters, separated with "|". Each
' pair consists of a description|file spec. Use a "|" between pairs. No need to put a
' trailing "|". You can set the FilterIndex property as well, to select the default
' filter. The first filter is numbered 1 (not 0). The default is 1.
.Filter = _
"Adobe PDF(*.pdf)|*pdf|JPEG(*.JPG,*.JPEG,*.JPE,*.JFIF)|*.JPEG|Text Documents(*.txt)|*.txt|All files|*.*"
.Multiselect = False
' Restore the original directory when done selecting
' a file? If False, the current directory changes
' to the directory in which you selected the file.
' Set this to True to put the current folder back
' where it was when you started.
' The default is False.
.RestoreDirectory = True
' Show the Help button and Read-Only checkbox?
.ShowHelp = True
.ShowReadOnly = False
' Start out with the read-only check box checked?
' This only make sense if ShowReadOnly is True.
.ReadOnlyChecked = False
.Title = "Select Artwok To Upload"
' Only accept valid Win32 file names?
.ValidateNames = True
If .ShowDialog = Windows.Forms.DialogResult.OK Then
Try
txtFileContents.Text = My.Computer.FileSystem.ReadAllText(.FileName)
Catch fileException As Exception
Throw fileException
End Try
End If
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, Me.Text)
End Try
End Sub
Private Sub btnSaveTextFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveTextFile.Click
Try
With sdlgTextFile
' See the code demonstrating the OpenFileDialog control
' for examples using most properties, which are the same
' for both controls, for the most part.
' Add the default extension, if the user neglects to add an extension.
' The default is True.
.AddExtension = True
' Check to verify that the output path actually exists. Prompt before
' creating a new file? Prompt before overwriting?
' The default is True.
.CheckPathExists = True
' The default is False.
.CreatePrompt = False
' The default is True.
.OverwritePrompt = True
' The default is True.
.ValidateNames = True
' The default is False.
.ShowHelp = True
' If the user doesn't supply an extension, and if the AddExtension property is
' True, use this extension. The default is "".
.DefaultExt = "PDF"
' Prompt with the current file name if you've specified it.
' The default is "".
.FileName = filename
' The default is "".
.Filter = _
"Adobe PDF(*.pdf)|*pdf|JPEG(*.JPG,*.JPEG,*.JPE,*.JFIF)|*.JPEG|Text Documents(*.txt)|*.txt|All files|*.*"
.FilterIndex = 1
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
My.Computer.FileSystem.WriteAllText(.FileName, txtFileContents.Text, False)
End If
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, Me.Text)
End Try
End Sub
This code moves the files that the user chooses, however now i cannot open them once i have moved them.
I feel there must be a simpler way to do what i want, however with the pressure on me in work i didn't have much time to think about one.
Can anyone help?
Thanks