SaveFileDialog to save automatically

Discussion in 'Mixed Languages' started by QuantumBug, May 12, 2012.

  1. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,322
    60
    #1 QuantumBug, May 12, 2012
    Last edited by a moderator: Apr 20, 2017
    Hi all, I've written this for my XP version of DigitalPID Decryptor, this process creates OEMINFO.INI using a streamwriter...

    Code:
        Private Sub Save_OEM_Info(sender As System.Object, e As System.EventArgs) Handles cmd_saveoem.Click
            Dim SaveDiag2 As New SaveFileDialog()
            Dim MySW As System.IO.StreamWriter
            With SaveDiag2
                .FileName = "OEMINFO"
                .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System)
                .Filter = "INI File (*.ini)|*.ini"
                .CheckPathExists = True
                .OverwritePrompt = True
                .Title = "Save OEM INI File"
                .ShowDialog(Me)
            End With
            Try
                MySW = System.IO.File.CreateText(SaveDiag2.FileName)
                MySW.WriteLine("[General]")
                MySW.WriteLine("Manufacturer=" & oem_manufacturer.Text.ToString)
                MySW.WriteLine("Model=" & oem_model.Text.ToString & Environment.NewLine)
                MySW.WriteLine("[Support Information]")
                MySW.WriteLine("Line1=" & oem_support1.Text.ToString)
                MySW.WriteLine("Line2=" & oem_support2.Text.ToString)
                MySW.WriteLine("Line3=" & oem_support3.Text.ToString)
                MySW.Flush()
                MySW.Dispose()
            Catch ex As Exception
                Return
            End Try
        End Sub
    
    Now how can I get it to save automatically instead of showing the save dialog, esentially saving OEMINFO.INI just by clicking the 'Save' button in the application.

    Thanks in advance guys and gals :)
     
  2. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    #2 stevemk14ebr, May 12, 2012
    Last edited by a moderator: Apr 20, 2017
    Code:
    try
     Dim DBw As New StreamWriter("C:\ProgramData\OEMINFO.ini",
                                            False, System.Text.Encoding.UTF8)
                MySW.WriteLine("[General]")
                MySW.WriteLine("Manufacturer=" & oem_manufacturer.Text.ToString)
                MySW.WriteLine("Model=" & oem_model.Text.ToString & Environment.NewLine)
                MySW.WriteLine("[Support Information]")
                MySW.WriteLine("Line1=" & oem_support1.Text.ToString)
                MySW.WriteLine("Line2=" & oem_support2.Text.ToString)
                MySW.WriteLine("Line3=" & oem_support3.Text.ToString)
                MySW.Flush()
                MySW.Dispose()
                DBw.close
    catch ex as exception
                messagebox.show(ex.message)
                DBw.Close()
    end try
    see if that works, it just uses a stream writer
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,322
    60
    Thank you Steve, I'll impliment it into the XP version and let you know. I don't know why I didn't try that before.
     
  4. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,322
    60
    #4 QuantumBug, May 13, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    A few edits and it worked for me, here is the code for everyone else. Sharing is caring :D

    Using Environment.GetFolderPath is the better option, if you use the below code(string) and your main drive is drive 'D:' or 'E:', it won't be able to find the path.

    Code:
    Dim MySW As New StreamWriter("C:\Windows\System32\OEMINFO.INI")
    


    Code:
            Try
                Dim MySW As New StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.System) + "\OEMINFO.INI", False, System.Text.Encoding.ASCII)
                MySW.WriteLine("[General]")
                MySW.WriteLine("Manufacturer=" & oem_manufacturer.Text.ToString)
                MySW.WriteLine("Model=" & oem_model.Text.ToString & Environment.NewLine)
                MySW.WriteLine("[Support Information]")
                MySW.WriteLine("Line1=" & oem_support1.Text.ToString)
                MySW.WriteLine("Line2=" & oem_support2.Text.ToString)
                MySW.WriteLine("Line3=" & oem_support3.Text.ToString)
                MySW.Flush()
                MySW.Dispose()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
    The *.Text.ToString would essentially be your Textbox, RichTextBox etc. Whatever you decide to use. Thanks again to Steve.
     
  5. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    np, i was just giving u an idea of what to do, i knew it wouldnt be perfect for u first time. glad u got it working
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...