locked
Problem savefiledialog 1.1 to 2.0 conversion RRS feed

  • Question

  • All:

    I have an .net 1.1 framework application I would like to update to 3.0 framework (or at least 2.0), but I've just run into a real problem. The savefile dialog code which works fine in 1.1 does not work in 2.0 and up. Line for line the code is the same (there is no difference in line numbers at all). I'm baffled.

    With the exception of throwing an error for the Quicktime 7 control, VS2008 indicated no errors in converting 1.1

    I open my 1.1 (VS 2003) version and step through line-by-line. Works as advertised.
    I open my 2.0 (VS 2008) version and step through line-by-line. Appears to be working, dialog opens fine, I enter a filename, code continues; but when complete, there is no file in the folder.

    In VS2008 I'm compiling for x86 CPU to eliminate the Quicktime exception.

    Any suggestions where I can start looking?

    TIA for any help.
    Norm

    I'm wondering if the problem lies in the block of code for the small XML file I'm writing. 

    Imports System.IO
    
    Public Class Form1
    
      Private fn As String
      Private projectFile As String
      Private shortName As String
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        sfd.InitialDirectory = Environment.SpecialFolder.Personal
        sfd.Filter = "Enscription Project (*.vpt)|*.vpt"
        sfd.Title = "Trying to fix problem"
    
        If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
          fn = sfd.FileName
          shortName = Path.GetFileNameWithoutExtension(fn)
          projectFile = shortName
          WriteXML()
        Else
          Exit Sub
        End If
      End Sub
    
    
      Private Sub WriteXML()
    
        Dim writer As New Xml.XmlTextWriter(projectFile, Nothing)
    
        writer.WriteStartDocument(True)
        writer.Formatting = Xml.Formatting.Indented
        writer.WriteComment("Created using Enkadia Enscription - copyright 2010")
    
        writer.WriteStartElement("EnkadiaEnscription")
        writer.WriteStartElement("Project")
        writer.WriteElementString("Transcription", fn & ".vtn")
        writer.WriteElementString("EditorsNotes", fn & ".ved")
        writer.WriteElementString("VideoClip", fn & ".mp4")
    
        writer.WriteEndElement()
        writer.WriteEndElement()
    
        writer.Flush()
        writer.Close()
    
      End Sub
    End Class


     

    • Edited by normschaef Tuesday, May 25, 2010 3:52 PM added code block
    Tuesday, May 25, 2010 2:33 PM

Answers

  • Hello,

    It seems that you do not include path information in your file name.  Thus the file may be saved to another location.  I suggest to deal with complete file paths instead, such as "C:\directory\file.ext".

    Best regards,
    Herfried K. Wagner [MVP]

    Tuesday, May 25, 2010 5:57 PM
  • Have a look at this code , note the filestream create in the WriteXML() sub and the use of the filename with extension changed in the XML document to avoid a double extension .

     

    Imports System.IO

    Public Class Form1

        
    Private fn As String
        Private projectFile As String
        Private shortName As String

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
    Dim sfd As New SaveFileDialog
            sfd.InitialDirectory = Environment.SpecialFolder.Personal
            sfd.Filter = 
    "Enscription Project (*.vpt)|*.vpt"
            sfd.Title = "Trying to fix problem"

            If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                fn = sfd.FileName
                shortName = Path.GetFileNameWithoutExtension(fn)
                projectFile = shortName
                WriteXML()
            
    Else
                Exit Sub
            End If
        End Sub


        Private Sub WriteXML()

            
    'Open filestream using filename from SaveFileDialog
            Dim fs As New FileStream(fn, FileMode.OpenOrCreate)

            
    'Create XmlTextWriter to write to stream
            Dim writer As New Xml.XmlTextWriter(fs, Nothing)

            writer.WriteStartDocument(
    True)
            writer.Formatting = Xml.Formatting.Indented
            writer.WriteComment(
    "Created using Enkadia Enscription - copyright 2010")

            writer.WriteStartElement(
    "EnkadiaEnscription")
            writer.WriteStartElement(
    "Project")
            writer.WriteElementString(
    "Transcription", Path.ChangeExtension(fn, ".vtn"))
            writer.WriteElementString(
    "EditorsNotes", Path.ChangeExtension(fn, ".ved"))
            writer.WriteElementString(
    "VideoClip", Path.ChangeExtension(fn, ".mp4"))

            writer.WriteEndElement()
            writer.WriteEndElement()

            writer.Flush()
            writer.Close()

        
    End Sub
    End
     Class


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    Tuesday, May 25, 2010 10:43 PM
  • You can do this in the WriteXML() sub


        
    Private Sub WriteXML()

            
    Using writer As New Xml.XmlTextWriter(New FileStream(fn, FileMode.OpenOrCreate), Nothing)

                writer.WriteStartDocument(
    True)
                writer.Formatting = Xml.Formatting.Indented
                writer.WriteComment(
    "Created using Enkadia Enscription - copyright 2010")

                writer.WriteStartElement(
    "EnkadiaEnscription")
                writer.WriteStartElement(
    "Project")
                writer.WriteElementString(
    "Transcription", Path.ChangeExtension(fn, ".vtn"))
                writer.WriteElementString(
    "EditorsNotes", Path.ChangeExtension(fn, ".ved"))
                writer.WriteElementString(
    "VideoClip", Path.ChangeExtension(fn, ".mp4"))

                writer.WriteEndElement()
                writer.WriteEndElement()

                writer.Flush()
                writer.Close()
            
    End Using
        End Sub


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    Tuesday, May 25, 2010 10:50 PM
  • If you do not want the whole filename for Transcription etc then you can do this

    Imports System.IO

    Public Class Form1

        
    Private fn As String

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
    Dim sfd As New SaveFileDialog
            sfd.InitialDirectory = Environment.SpecialFolder.Personal
            sfd.Filter = 
    "Enscription Project (*.vpt)|*.vpt"
            sfd.Title = "Trying to fix problem"

            If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                fn = sfd.FileName
                WriteXML()
            
    Else
                Exit Sub
            End If
        End Sub

        Private Sub WriteXML()

            
    Dim shortName As String = Path.GetFileNameWithoutExtension(fn)

            
    Using writer As New Xml.XmlTextWriter(New FileStream(fn, FileMode.OpenOrCreate), Nothing)

                writer.WriteStartDocument(
    True)
                writer.Formatting = Xml.Formatting.Indented
                writer.WriteComment(
    "Created using Enkadia Enscription - copyright 2010")

                writer.WriteStartElement(
    "EnkadiaEnscription")
                writer.WriteStartElement(
    "Project")
                writer.WriteElementString(
    "Transcription", Path.ChangeExtension(shortName, ".vtn"))
                writer.WriteElementString(
    "EditorsNotes", Path.ChangeExtension(shortName, ".ved"))
                writer.WriteElementString(
    "VideoClip", Path.ChangeExtension(shortName, ".mp4"))

                writer.WriteEndElement()
                writer.WriteEndElement()

                writer.Flush()
                writer.Close()
            
    End Using
        End Sub
    End
     Class


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261

    Tuesday, May 25, 2010 11:00 PM

All replies

  • Hello,

    It seems that you do not include path information in your file name.  Thus the file may be saved to another location.  I suggest to deal with complete file paths instead, such as "C:\directory\file.ext".

    Best regards,
    Herfried K. Wagner [MVP]

    Tuesday, May 25, 2010 5:57 PM
  • Have a look at this code , note the filestream create in the WriteXML() sub and the use of the filename with extension changed in the XML document to avoid a double extension .

     

    Imports System.IO

    Public Class Form1

        
    Private fn As String
        Private projectFile As String
        Private shortName As String

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
    Dim sfd As New SaveFileDialog
            sfd.InitialDirectory = Environment.SpecialFolder.Personal
            sfd.Filter = 
    "Enscription Project (*.vpt)|*.vpt"
            sfd.Title = "Trying to fix problem"

            If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                fn = sfd.FileName
                shortName = Path.GetFileNameWithoutExtension(fn)
                projectFile = shortName
                WriteXML()
            
    Else
                Exit Sub
            End If
        End Sub


        Private Sub WriteXML()

            
    'Open filestream using filename from SaveFileDialog
            Dim fs As New FileStream(fn, FileMode.OpenOrCreate)

            
    'Create XmlTextWriter to write to stream
            Dim writer As New Xml.XmlTextWriter(fs, Nothing)

            writer.WriteStartDocument(
    True)
            writer.Formatting = Xml.Formatting.Indented
            writer.WriteComment(
    "Created using Enkadia Enscription - copyright 2010")

            writer.WriteStartElement(
    "EnkadiaEnscription")
            writer.WriteStartElement(
    "Project")
            writer.WriteElementString(
    "Transcription", Path.ChangeExtension(fn, ".vtn"))
            writer.WriteElementString(
    "EditorsNotes", Path.ChangeExtension(fn, ".ved"))
            writer.WriteElementString(
    "VideoClip", Path.ChangeExtension(fn, ".mp4"))

            writer.WriteEndElement()
            writer.WriteEndElement()

            writer.Flush()
            writer.Close()

        
    End Sub
    End
     Class


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    Tuesday, May 25, 2010 10:43 PM
  • You can do this in the WriteXML() sub


        
    Private Sub WriteXML()

            
    Using writer As New Xml.XmlTextWriter(New FileStream(fn, FileMode.OpenOrCreate), Nothing)

                writer.WriteStartDocument(
    True)
                writer.Formatting = Xml.Formatting.Indented
                writer.WriteComment(
    "Created using Enkadia Enscription - copyright 2010")

                writer.WriteStartElement(
    "EnkadiaEnscription")
                writer.WriteStartElement(
    "Project")
                writer.WriteElementString(
    "Transcription", Path.ChangeExtension(fn, ".vtn"))
                writer.WriteElementString(
    "EditorsNotes", Path.ChangeExtension(fn, ".ved"))
                writer.WriteElementString(
    "VideoClip", Path.ChangeExtension(fn, ".mp4"))

                writer.WriteEndElement()
                writer.WriteEndElement()

                writer.Flush()
                writer.Close()
            
    End Using
        End Sub


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    Tuesday, May 25, 2010 10:50 PM
  • If you do not want the whole filename for Transcription etc then you can do this

    Imports System.IO

    Public Class Form1

        
    Private fn As String

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
    Dim sfd As New SaveFileDialog
            sfd.InitialDirectory = Environment.SpecialFolder.Personal
            sfd.Filter = 
    "Enscription Project (*.vpt)|*.vpt"
            sfd.Title = "Trying to fix problem"

            If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                fn = sfd.FileName
                WriteXML()
            
    Else
                Exit Sub
            End If
        End Sub

        Private Sub WriteXML()

            
    Dim shortName As String = Path.GetFileNameWithoutExtension(fn)

            
    Using writer As New Xml.XmlTextWriter(New FileStream(fn, FileMode.OpenOrCreate), Nothing)

                writer.WriteStartDocument(
    True)
                writer.Formatting = Xml.Formatting.Indented
                writer.WriteComment(
    "Created using Enkadia Enscription - copyright 2010")

                writer.WriteStartElement(
    "EnkadiaEnscription")
                writer.WriteStartElement(
    "Project")
                writer.WriteElementString(
    "Transcription", Path.ChangeExtension(shortName, ".vtn"))
                writer.WriteElementString(
    "EditorsNotes", Path.ChangeExtension(shortName, ".ved"))
                writer.WriteElementString(
    "VideoClip", Path.ChangeExtension(shortName, ".mp4"))

                writer.WriteEndElement()
                writer.WriteEndElement()

                writer.Flush()
                writer.Close()
            
    End Using
        End Sub
    End
     Class


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261

    Tuesday, May 25, 2010 11:00 PM
  • All:

    Thanks for all the tips.  In the end, it was indeed an error in the way I was handling the files in the XMLWriter code.  The reason I was really baffled was the code worked fine in .NET 1.1 (the filepath seemed to persist from savefiledialog.ShowDialog), but didn't work the same way in .NET 2.0 and up.

    Thanks for sharing the "right" way to handle this.

    Norm

    Wednesday, May 26, 2010 12:47 AM
  • The problem as I see it is you had defined projectfile as the filename without extension of the filename from the open file dialog in this line .

    Dim writer As New Xml.XmlTextWriter(projectFile, Nothing)

    If you use itellesence by clicking just before projectFile and hitting the spacebar you see that the first parameter it calls for is a file stream , a filename or a TextWriter if you had defined one ( 3 overloads ) . What you have is as I said the filename without extension not even a valid path to an existing file or valid filename . Filename as described in the second overload is the path and filename of the file to write to which must include the extension I believe . If you leave out the directory path I think it may write to the current directory or reletive path . You had written

          shortName = Path.GetFileNameWithoutExtension(fn)
          projectFile = shortName

    I think if you had written projectFile = Path.GetFileName(fn) which would have returned the filename only without the directory path but with the extension it may have written to the file in the currentdirectory or reletive path .


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    • Proposed as answer by bdbodger Sunday, May 30, 2010 10:10 AM
    Wednesday, May 26, 2010 1:42 AM