Results 1 to 7 of 7

Thread: Save file extensions

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    4

    Save file extensions

    Hi all,

    new to VB, im used to C programming, so a few bits here have me confused.


    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click


    timedate.Stop()

    Dim strFileName As String

    With SaveFileDialog1
    .InitialDirectory = "C:\"

    .Filter = " txt(*.txt)|*. |AllFiles (*.*) |*.*"
    .FilterIndex = 1

    .CheckFileExists = False
    .Title = "Save Files"
    .OverwritePrompt = True

    If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    strFileName = .FileName


    Dim myStream As New System.IO.StreamWriter(strFileName & ".GDL", False)
    If TMI = True Then
    myStream.Write("Version 1.4.1" & Chr(13) & Chr(10))
    Else
    myStream.Write("Version 1.4.0" & Chr(13) & Chr(10))
    End If

    For arrayindex = 0 To 512

    If Mid(bigarray(arrayindex), 20, 10) = "FFFFFFFFFF" Then
    GoTo endloop
    End If
    myStream.Write(bigarray(arrayindex) & Chr(13) & Chr(10))
    Next arrayindex
    endloop:
    myStream.Close()
    myStream = Nothing
    timedate.Start()
    End If
    End With

    End Sub


    The problem is that if I save a file I get "myfile.GDL" if I overwrite that I get a new file "myfile.GDL.GDL" ect.

    the other problem being that the filtering in the windows save/open dialog wont show .GDL files unless I select "all files"

    Any help would be great.

    Cheers

  2. #2

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    4

    Re: Save file extensions

    the line

    .Filter = " txt(*.txt)|*. |AllFiles (*.*) |*.*"

    is ment to read

    .Filter = " GDL(*.GDL)|*. |AllFiles (*.*) |*.*"

    copy'd & pasted I changed that.

  3. #3
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Save file extensions

    Welcome to the forums. Please add your code in tags to allow easier reading.

    If you check the SaveFileDialog doco, it shows that the .Filter property is set by splitting the names and types by the pipe | which you have done, but not correctly.

    Code:
    .Filter = "GDL(*.GDL)|*.GDL|AllFiles (*.*)|*.*"
    You have missed the extension in the first filter. This is probly causing both your problems. To note, there is an .AddExtension and .DefaultExt which can be set to ensure that an extension is given when none is entered.

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Save file extensions

    As another hint the StreamWriter class, has a WriteLine method to save you having to append the line terminators (which also exist as constants btw, vbNewLine for example).

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    4

    Re: Save file extensions

    Thats great, Thank you.

    The filter works.

    Not sure why the extension was adding extra extensions, but I have added a simple for next loop to dectect a "." and I write from there

    HTML Code:
     If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    strFileName = .FileName
    
                    For i = 1 To Len(strFileName)
    
                        If Mid(strFileName, i, 1) = "." Then
                            strFileName = Mid(strFileName, 1, i) & "GDL"
                            GoTo loopend
                        End If
                    Next i
    which seems to do the job perfectly

    Thanks for the help

  6. #6
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Save file extensions

    Honestly you should not have to do that. Goto went out with the dinosaurs, there are numerous String. methods to allow you to mess about with data. Example being strFileName .IndexOf(".") to get the position of a "." and strFileName.SubString(xx) to get a partial extract. Loads of new things to learn!

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    4

    Re: Save file extensions

    Thank you again, that link is very helpful!

    and I admit, im using a book thats very outdated for refrence.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width