Author Topic: Get INI file path for reading/writing  (Read 3318 times)

Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
Get INI file path for reading/writing
« on: May 31, 2017, 10:44:07 AM »
I recently added some functionality to one of my profiles that allows the user to obtain the file paths for existing INI files as well as new INI files. This is handy when used in conjunction with the VAExtensions plugin by Antaniserse. With the file paths available one can construct VA logic to read or write an INI file.

The VB.net code to get the file path from the OpenFileDialog or SaveFileDialog isn't very complex, but I figured I'd post my code for the benefit of other users. No additional Referenced Assemblies beyond the ones provided by default by VA are needed.

Open File Dialog (useful for selecting INI files to be read by VA via VAExtensions)
Code: [Select]
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Data
Imports System.Drawing
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Linq
Imports System.Xml.Linq
Imports System.Threading.Tasks

Public Class VAInline

    Public Sub Main()

        'Source = https://stackoverflow.com/questions/30308485/get-full-path-using-openfiledialog
        Dim openFileDialog1 As New OpenFileDialog()
       
        'Specify directory that pulls up when dialog opens. This is optional.
        openFileDialog1.InitialDirectory = "C:\your initial directory here"
        'Specify file type filter selection options. This is optional.
        openFileDialog1.Filter = "ini files (*.ini)|*.ini|All files (*.*)|*.*"
        'Specify window title for OpenFileDialog (upper left corner of window). This is optional.
        openFileDialog1.Title = "your window title here"
       
        If openFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.Ok Then
            'MessageBox.Show(openFileDialog1.fileName)
            'Save path of selected file to VA variable
            VA.SetText("INI import path", openFileDialog1.fileName)
        End If

    End Sub

End Class

Save File Dialog (useful for selecting/creating INI files to be written by VA via VAExtensions)
Code: [Select]
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Data
Imports System.Drawing
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Linq
Imports System.Xml.Linq
Imports System.Threading.Tasks

Public Class VAInline

    Public Sub Main()

        'Source = https://stackoverflow.com/questions/30308485/get-full-path-using-openfiledialog but for SaveFileDialog instead of OpenFileDialog
        Dim saveFileDialog1 As New SaveFileDialog()

        'Specify directory that pulls up when dialog opens. This is optional.
        saveFileDialog1.InitialDirectory = "C:\your initial directory here"
        'Specify file type filter selection options. This is optional.
        saveFileDialog1.Filter = "ini files (*.ini)|*.ini|All files (*.*)|*.*"
        'Specify window title for OpenFileDialog (upper left corner of window). This is optional.
        saveFileDialog1.Title = "your window title here"
       
        If saveFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.Ok Then
            'MessageBox.Show(saveFileDialog1.fileName)
            'Save path of created/selected file to VA variable
            VA.SetText("INI export path", saveFileDialog1.fileName)
        End If

    End Sub

End Class

Of course you can get the file path for any file type using this method (and the above functionality could be just the tip of the iceberg), but I've kept it simple for what I need. Enjoy!  :)