none
Importing Solution Files with Windows Forms

    Question

  • Hello,
    I am working on a vb.net Windows Forms application.
    I want the application to import Visual Studio Solution files from versions 2005 & 2008 (elsewhere on my Hard Disk).
    I am using Visual Studio 2010. I want the applicaton to find the files entitled - 'Microsoft Visual Studio Solution File, Format Version 11.00
    # Visual Studio 2010.' Thyese are the files I need. I aso know that it will find files - 'Microsoft Visual Studio Solution File, Format Version 9.00
    # Visual Studio 2005'.

    Under is my code thus far. Maybe someone can give me some advice as how to make sure that the applications returns the correct Sln files?

    Imports
    
    
    System
    Imports
    
    
    System.IO
    Public
    
    
    Class Form1
    
    
    Private FileContent As String
    
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    
    
    
    Dim filedialog As New OpenFileDialog
    
    
    With filedialog 
    .Filter =
    
    
    "sln files (*.sln)|*.sln"
    
    
    End With
    
    
    If filedialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
    LoadFile(filedialog.FileName)
    
    
    If isSolutionCorrectVersion() Then
     
    
    
    Else
    
    
    MessageBox.Show("niet de juiste versie") 
    
    
    
    End If
    
    
    End If
    
    
    End Sub
    
    
    Private Sub LoadFile(ByVal filename As String) 
    
    
    
    If File.Exists(filename) Then
    
    
    Dim sr As StreamReader = Nothing
    
    
    Try
    sr =
    
    New StreamReader(filename) 
    FileContent = sr.ReadToEnd
    
    
    
    Catch ex As Exception
    
    
    MessageBox.Show(ex.Message) 
    
    
    
    Finally
    
    
    If Not sr Is Nothing Then
    sr.Close()
    
    
    End If
    
    
    End Try
    
    
    Else
    
    
    MessageBox.Show(filename & " bestand niet gevonden", "VS2010-2005 Converter", MessageBoxButtons.OK) 
    
    
    
    End If
    
    
    End Sub
    
    
    
    Private Function isSolutionCorrectVersion() As Boolean
    
    
    End Function
    End
    
    
    Class
    
    

     

    Tuesday, January 4, 2011 4:36 PM

Answers

  • I am not exactly sure what you mean, but if isSolutionCorrectVersion() should read the solution file you opened in LoadFile() then you could do something like:

     

    Private Function isSolutionCorrectVersion(ByVal slnText As String) As Boolean
      Dim verString As String = "Microsoft Visual Studio Solution File, Format Version 11.00"
      Dim stString As String = "# Visual Studio 2010"
    
      If InStr(slnText, verString) And Instr(slnText, stString) Then
        Return True
      Else
        Return False
      End If
    End Function
    

    Call it after FileContent = sr.ReadToEnd

    like this:

    isSolutionCorrectVersion(FileContent)

     

    You can modify the code isSolutionVersion112010, then make another isSolutionVersion92005 and modify the verString and stString.

    • Proposed as answer by Cor Ligthert Wednesday, January 5, 2011 9:28 AM
    • Marked as answer by Liliane Teng Tuesday, January 11, 2011 9:30 AM
    Wednesday, January 5, 2011 3:19 AM
  • You can just read the first 2 lines and exit the loop. Below is sample code that can read only the first 2 lines and add it textbox.

    Note: This code was written on PC with VS2003,so you can rewrite it with higher version to reduce the lines of code

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      Dim op As New OpenFileDialog
      op.Filter = "sln files (*.sln)|*.sln"
      op.Multiselect = False
      op.RestoreDirectory = True
      If op.ShowDialog = Windows.Forms.DialogResult.OK Then
       If IO.File.Exists(op.FileName) = True Then
        Application.DoEvents()
        Dim oFile As System.IO.FileStream = Nothing
        Dim oRead As System.IO.StreamReader = Nothing
        Dim result As String = ""
        Try
         oFile = New System.IO.FileStream(op.FileName, IO.FileMode.Open, IO.FileAccess.Read)
         oRead = New System.IO.StreamReader(oFile)
         Dim counter As Integer = 0
         While oRead.Peek <> -1
          Dim res As String = oRead.ReadLine.Trim
          If counter = 2 Then
           Exit While
          End If
          If res.Length > 0 Then
           result &= res & vbNewLine
           counter += 1
          End If
         End While
         oRead.Close()
         oFile.Close()
         result = result.Trim
         TextBox1.Text = result
        Catch ex As Exception
        Finally
         If Not oRead Is Nothing Then
          oRead.Close()
         End If
         If Not oFile Is Nothing Then
          oFile.Close()
         End If
        End Try
       End If
      End If
     End Sub
    

    Output : Tested on all versions

    VS2003
         : Microsoft Visual Studio Solution File, Format Version 8.00
    VS2005
         :Microsoft Visual Studio Solution File, Format Version 9.00
         :# Visual Studio 2005
    VS2008
         :Microsoft Visual Studio Solution File, Format Version 10.00
         :# Visual Studio 2008
    vs2010
         :Microsoft Visual Studio Solution File, Format Version 11.00
         :# Visual Studio 2010

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    • Proposed as answer by Cor Ligthert Wednesday, January 5, 2011 9:27 AM
    • Marked as answer by Liliane Teng Tuesday, January 11, 2011 9:30 AM
    Wednesday, January 5, 2011 3:46 AM

All replies

  • I am not exactly sure what you mean, but if isSolutionCorrectVersion() should read the solution file you opened in LoadFile() then you could do something like:

     

    Private Function isSolutionCorrectVersion(ByVal slnText As String) As Boolean
      Dim verString As String = "Microsoft Visual Studio Solution File, Format Version 11.00"
      Dim stString As String = "# Visual Studio 2010"
    
      If InStr(slnText, verString) And Instr(slnText, stString) Then
        Return True
      Else
        Return False
      End If
    End Function
    

    Call it after FileContent = sr.ReadToEnd

    like this:

    isSolutionCorrectVersion(FileContent)

     

    You can modify the code isSolutionVersion112010, then make another isSolutionVersion92005 and modify the verString and stString.

    • Proposed as answer by Cor Ligthert Wednesday, January 5, 2011 9:28 AM
    • Marked as answer by Liliane Teng Tuesday, January 11, 2011 9:30 AM
    Wednesday, January 5, 2011 3:19 AM
  • You can just read the first 2 lines and exit the loop. Below is sample code that can read only the first 2 lines and add it textbox.

    Note: This code was written on PC with VS2003,so you can rewrite it with higher version to reduce the lines of code

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      Dim op As New OpenFileDialog
      op.Filter = "sln files (*.sln)|*.sln"
      op.Multiselect = False
      op.RestoreDirectory = True
      If op.ShowDialog = Windows.Forms.DialogResult.OK Then
       If IO.File.Exists(op.FileName) = True Then
        Application.DoEvents()
        Dim oFile As System.IO.FileStream = Nothing
        Dim oRead As System.IO.StreamReader = Nothing
        Dim result As String = ""
        Try
         oFile = New System.IO.FileStream(op.FileName, IO.FileMode.Open, IO.FileAccess.Read)
         oRead = New System.IO.StreamReader(oFile)
         Dim counter As Integer = 0
         While oRead.Peek <> -1
          Dim res As String = oRead.ReadLine.Trim
          If counter = 2 Then
           Exit While
          End If
          If res.Length > 0 Then
           result &= res & vbNewLine
           counter += 1
          End If
         End While
         oRead.Close()
         oFile.Close()
         result = result.Trim
         TextBox1.Text = result
        Catch ex As Exception
        Finally
         If Not oRead Is Nothing Then
          oRead.Close()
         End If
         If Not oFile Is Nothing Then
          oFile.Close()
         End If
        End Try
       End If
      End If
     End Sub
    

    Output : Tested on all versions

    VS2003
         : Microsoft Visual Studio Solution File, Format Version 8.00
    VS2005
         :Microsoft Visual Studio Solution File, Format Version 9.00
         :# Visual Studio 2005
    VS2008
         :Microsoft Visual Studio Solution File, Format Version 10.00
         :# Visual Studio 2008
    vs2010
         :Microsoft Visual Studio Solution File, Format Version 11.00
         :# Visual Studio 2010

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    • Proposed as answer by Cor Ligthert Wednesday, January 5, 2011 9:27 AM
    • Marked as answer by Liliane Teng Tuesday, January 11, 2011 9:30 AM
    Wednesday, January 5, 2011 3:46 AM
  • I was going to say that, but I did not want to write 30 lines of code...LOL he was already reading the entire file, so I figured I would take the easy way out, for me anyways. Your code is definitely the right way to go though 
    Wednesday, January 5, 2011 3:49 AM