Question about SaveFileDialog

I can get it to work perfectly, but only if I am overwriting a file. I cannot get it to create a new file then save.

Say, I type a filename into the Dialog instead of choosing one from the list. If I do this it won't work. What am I doing wrong.

Enjoy!




Answer this question

Question about SaveFileDialog

  • traimo

    Troy,

    If the file does not exist you have to create it. Please use the following code as reference.

    With New SaveFileDialog
    .CheckFileExists = False
    .Filter = "text files(*.txt)|*.txt"

    Dim SaveDlg As DialogResult = .ShowDialog
    If SaveDlg = DialogResult.OK Then
    If Not System.IO.File.Exists(.FileName) Then
    System.IO.File.Create(.FileName)
    'Create the file here
    End If
    End If

    Hope this was helpful.



  • Savvy

    I made a new application with the following class:

    Public Class Form1

    Protected b() As Byte

    Private Function ReadBytes(ByRef Buffer As Byte(), ByVal FileName As String) As Boolean
    Dim fi As New IO.FileInfo(FileName)
    Dim bytecount As Integer = CInt(fi.Length)

    ReDim Buffer(bytecount)

    Dim fs As New IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read)
    Dim br As New IO.BinaryReader(fs)

    Buffer = br.ReadBytes(bytecount)

    br.Close()
    fs.Close()
    fs.Dispose()

    Return True
    End Function

    Private Function WriteBytes(ByRef Buffer As Byte(), ByVal FileName As String) As Boolean

    Try
    My.Computer.FileSystem.WriteAllBytes(FileName, Buffer, True)
    Return True
    Catch ex As Exception
    MsgBox(ex.Message)
    Return False
    End Try

    End Function

    Private Function fileLoad() As Boolean
    Dim open As New OpenFileDialog()

    With open
    .CheckFileExists = True
    .CheckPathExists = True
    .Filter = "All Files (*.*)|*.*"
    .FilterIndex = 0
    .Title = "Open"
    End With

    If open.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Function
    'ReadBytes(b, fname)
    Dim b As Byte() = My.Computer.FileSystem.ReadAllBytes(open.FileName)
    End Function

    Private Function fileSave() As Boolean
    Dim save As New SaveFileDialog

    With save
    .CheckFileExists = False
    .CheckPathExists = True
    .Filter = "All Files (*.*)|*.*"
    .FilterIndex = 0
    .OverwritePrompt = True
    .Title = "Save"
    End With

    If save.ShowDialog <> Windows.Forms.DialogResult.OK Then Exit Function
    If Not IO.File.Exists(save.FileName) Then IO.File.Create(save.FileName)
    WriteBytes(b, save.FileName)

    Return True
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    fileLoad()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    fileSave()
    End Sub
    End Class

    See if you can get it to work on your machine. I have no clue what I am doing wrong.
    The form has only two controls: 2 Buttons; one labeled Open and the other labeled Save.

    Thank you for your continued efforts to resolve my problem.

    Enjoy!


  • rhapsodyv

    As far as I know the only thing that is accessing the file is my program.
    I tried the code you posted and I got the same message whether the 3rd param was true or false.
    It said it cannot access the file because it is being used by another process. Maybe it has something to do with the way I have opened the file. I used the following function:

    Private Function ReadBytes(ByRef Buffer As Byte(), ByVal FileName As String) As Boolean
    Dim fi As New IO.FileInfo(FileName)
    Dim bytecount As Integer = CInt(fi.Length)

    ReDim Buffer(bytecount)

    Dim fs As New IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read)
    Dim br As New IO.BinaryReader(fs)

    Buffer = br.ReadBytes(bytecount)

    br.Close()
    fs.Close()
    fs.Dispose()

    Return True
    End Function

    That must obviously be inefficient because my WriteBytes function was so. Thanks for all your help.

    Enjoy!



  • Jesse Blankenbiller

    Does this occur on any file you try and write using this routine

    If you try calling this routine from another very simple program which create a byte array and then tries to write it does this work

    If thr function writes from another very simple application and/or can write using this method then it would indicate there is something in you application that is causing the problem.

    If you can get it to repro - only on this file and from a very simple app calling this function then it may be an issue.


  • pjella

    All the save dialog is doing is giving you a dialog where you can select a filename or enter a filename that you want ot use in you save action. You still need to actually write the action that you want to do.

    So similar to the previous example just using My.Computer.FileSystem.WriteAllText to write the contents of the textbox to the file.

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    With New SaveFileDialog
    .CheckFileExists = False
    .Filter = "text files(*.txt)|*.txt"
    Dim SaveDlg As DialogResult = .ShowDialog
    If SaveDlg = System.Windows.Forms.DialogResult.OK Then
    My.Computer.FileSystem.WriteAllText(.FileName, Textbox1.text, False) '/Write the file with contents
    End If
    End With
    End Sub
    End Class


  • Jelgab

    Wow, talk about recycle - reduce - reuse. This is great. The code worked perfectly. I guess I had two unnecessary functions.

    Yeah, and about creating another local byte array, I didn't have that in my normal code. I put it in there trying to figure something out. heh. I originally had: b() = My.Computer.FileSystem.ReadAllBytes(open.FileName).
    I guess since I had the parenthesis after the b it didn't work.

    Thanks for your trouble. I appreciate it greatly.

    Enjoy!


  • George.R.C

    The following works just fine for loading a byte array from a file and saving it back to a file.

    Public Class Form1

    Protected b() As Byte

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    fileLoad()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    fileSave()
    End Sub

    Private Function fileLoad() As Boolean
    Dim open As New OpenFileDialog()
    With open
    .CheckFileExists = True
    .CheckPathExists = True
    .Filter = "All Files (*.*)|*.*"
    .FilterIndex = 0
    .Title = "Open"
    End With

    If open.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Function
    b = My.Computer.FileSystem.ReadAllBytes(open.FileName)
    Return True
    End Function

    Private Function fileSave() As Boolean
    Dim save As New SaveFileDialog
    With save
    .CheckFileExists = False
    .CheckPathExists = True
    .Filter = "All Files (*.*)|*.*"
    .FilterIndex = 0
    .OverwritePrompt = True
    .Title = "Save"
    End With

    If save.ShowDialog <> Windows.Forms.DialogResult.OK Then Exit Function
    My.Computer.FileSystem.WriteAllBytes(save.FileName, b, False)

    Return True
    End Function

    End Class

    In you code in the load event your creating another load byte array called b which will have local scope only and therefore no be accessible in the save routine. The WriteallBytes method call you made had append set to True - my assumption is that for a copy of a file this would be set to false to create a exact copy of the bytes not append them onto existing contents of file.

    This is stripped back and uses just the My.Computer.FileSystem.WriteAllBytes


  • greeniguana

    Ok, I got the program to create a file, but it still causes an error this is what i got.

    Private Function fileSave() As Boolean
    Dim save As New SaveFileDialog
    'WriteValues()
    Try
    With save
    .CheckFileExists = False
    .CheckPathExists = True
    .Filter = "All Files (*.*)|*.*"
    .FilterIndex = 0
    .OverwritePrompt = True
    .Title = "Save"
    End With

    If save.ShowDialog <> Windows.Forms.DialogResult.OK Then Exit Try
    If Not IO.File.Exists(save.FileName) Then IO.File.Create(save.FileName)
    WriteBytes(b, save.FileName)
    Catch ex As Exception
    MsgBox("Failed to save file.", MsgBoxStyle.OkOnly, "Failure")
    End Try
    Return True
    End Function


    And here is the WriteBytes Function:

    Private Function WriteBytes(ByRef Buffer As Byte(), ByVal FileName As String) As Boolean
    Dim fs As New IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Write)
    Dim bw As New IO.BinaryWriter(fs)

    bw.Write(Buffer, 0, Buffer.Length)

    bw.Close()
    fs.Close()
    fs.Dispose()

    Return True
    End Function

    In my code b is a byte array that I use to store the bytes of a file in. This process has always worked when I was overwriting a file but now it is giving me trouble. Any ideas

    Enjoy!



    Edit: I just found out the error. In my WriteBytes function where I create a new filestream (fs) it says I cannot access the file (to write to it) because it is being used by another process. I added IO.FileShare.Write and it still gives the error. Any Ideas


  • Phil Borg

    Things I'd do/try

    1. Find out what has got the file open.

    2. Put some exception handling in you code to identify these problems.

    3. Possibly try a simpler approach if your using My.Computer.FileSystem.WriteAllBytes which has an 3rd parameter append or overwrite.

    Private Function WriteBytes(ByRef Buffer As Byte(), ByVal FileName As String) As Boolean
    Try
    My.Computer.FileSystem.WriteAllBytes(FileName, Buffer, False)
    Return True
    Catch ex As Exception
    MsgBox(ex.Message)
    Return False
    End Try
    End Function

    I suppose 1 and 2 are the most important.


  • MiggyE

    You can actually reduce it further if you really wanted to and all those properties can be set at design time if your really wanted by dropping those two controls on the form. Potentially reducing the code down to 4 lines of actual code.

    Public Class Form1

    Protected b() As Byte

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim open As New OpenFileDialog()
    With open
    .CheckFileExists = True
    .CheckPathExists = True
    .Filter = "All Files (*.*)|*.*"
    .FilterIndex = 0
    .Title = "Open"
    End With

    If open.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Function
    b = My.Computer.FileSystem.ReadAllBytes(open.FileName)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim save As New SaveFileDialog
    With save
    .CheckFileExists = False
    .CheckPathExists = True
    .Filter = "All Files (*.*)|*.*"
    .FilterIndex = 0
    .OverwritePrompt = True
    .Title = "Save"
    End With

    If save.ShowDialog <> Windows.Forms.DialogResult.OK Then Exit Function
    My.Computer.FileSystem.WriteAllBytes(save.FileName, b, False)
    End Sub
    End Class


  • Question about SaveFileDialog