Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > SaveFileDialog Filter Save As Type Asks to Overwrite Old Extension Bug


Reply
 
Thread Tools Display Modes
  #1  
Old 02-27-2012, 05:03 AM
the_maxx's Avatar
the_maxx the_maxx is offline
Newcomer
 
Join Date: Aug 2011
Location: Holland
Posts: 16
Question SaveFileDialog Filter Save As Type Asks to Overwrite Old Extension Bug


Hi there. I've been searching this for a while but can't find out how to fix it yet. I think it may be a known bug but not sure how to work around it...

I have a VB.NET program with a SaveFileDialog control with the filter set as ".asx|*.asx|.m3u|*.m3u" (without the quotes). It works fine to save a file, but if I then try to save a file with the same name of an existing file and then change the extension in the save as type box, it still thinks I want to save the file as the existing file and asks to overwrite the file, disregarding the newly selected file extension.

I have tried setting the SaveFileDialog.FileName = Nothing after saving a file, so the Save File Dialog Box the File name box is empty, but then I like to click the existing file to get the file name I want, then select a new extension and try to save, but it still asks to overwrite the file with the other extension.

I hope my situation is clear and sorry if it has been already asked a million times...but can anyone point me in the right direction or tell me how I can save the file with the currently selected extension, not the previous selected extension when the file name was entered?

Thanks for any help. Here is the code I'm using if it helps...

Code:
        If SaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then

            If Path.GetExtension(SaveFileDialog.FileName) = ".asx" Then

                ' Code Here to Prepare the CodeASX ...

                File.WriteAllText(SaveFileDialog.FileName, CodeASX)

            End If

            If Path.GetExtension(SaveFileDialog.FileName) = ".m3u" Then

                ' Code Here to Prepare the CodeM3U ...

                File.WriteAllText(SaveFileDialog.FileName, CodeM3U)

            End If

            ' SaveFileDialog.FileName = Nothing

        End If
Reply With Quote
  #2  
Old 02-27-2012, 06:23 AM
DrPunk's Avatar
DrPunk DrPunk is online now
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,278
Default

You want to set AddExtension of the SaveFileDialog to false.

But it still has its quirks. Like, it appears to check the extension with known extensions and acts accordingly. UNLESS *.* is the selected filter.

For example, a SaveFileDialog with AddExtension = False and Filter = "Text Files|*.txt|All Files|*.*"

With Text Files as the selected filter, entering "test.xml" will return "test.xml". Entering "test.foo" will return "test.foo.txt".

With All Files as the selected filter, entering "test.xml" will return "test.xml". Entering "test.foo" will return "test.foo".

At least that's the results I get. Pretty annoying.
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 02-27-2012, 08:36 AM
the_maxx's Avatar
the_maxx the_maxx is offline
Newcomer
 
Join Date: Aug 2011
Location: Holland
Posts: 16
Default

Yes...it is quite frustrating...

For a temporary work-around, I will set the SaveFileDialog.Filter to:

*.asx or *.m3u|*.*

The user will have to select a file to overwrite or type in a new filename with an extension.

If the user types in a new filename without including an extension I will use something like:

If Path.GetExtension(SaveFileDialog.FileName) = "" Then
MsgBox("Error - A file extension was not specified.")
Exit Sub
End If

If Path.GetExtension(SaveFileDialog.FileName) <> ".asx" _
And Path.GetExtension(SaveFileDialog.FileName) <> ".m3u" Then
MsgBox("Error - A file extension " & Path.GetExtension(SaveFileDialog.FileName) & " was specified.")
Exit Sub
End If

SaveFileDialog.FileName = Nothing

Anyone have more ideas or experience with working around this issue? I feel like I am missing something very obvious! Hehe, seems like I am making something very simple into something so complicated!
Reply With Quote
  #4  
Old 02-27-2012, 09:06 AM
DrPunk's Avatar
DrPunk DrPunk is online now
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,278
Default

I'd be tempted to use the defaultext property to ensure there is always an extension on the file.

Cos it seems that even with AddExtension set to True, if the file filter is set to *.* then it won't add an extension to the file.

If you know there is always going to be an extension on the file then you can always strip off the extension it has itself added if you really want to.

For example you could use LastIndexOf to search for the period of the last extension on the name. Using the return value of that you could use LastIndexOf to see if there's another extension on the file, and if so you take the last extension off.

Note: They are maybe some strings that will cause an error here. This is just a demonstration.
Code:
Private Function SingleExtension(ByVal filename As String) As String
    Dim i1 As Integer
    Dim i2 As Integer

    i1 = filename.LastIndexOf(".")
    If i1 > 0 Then
        i2 = filename.LastIndexOf(".", i1 - 1)

        If i2 > 0 Then
            Return filename.Substring(0, i1)
        Else
            ' Only one extension
            Return filename
        End If
    Else
        ' No extension
        Return filename
    End If
End Function
__________________
There are no computers in heaven!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->