So I'm having a lot of trouble getting this program to work. Its sole purpose is to edit the configuration file for the DOSBox Emulator. Right now, I'm trying to make it so that I can take the DOSBox configuration file, located in the AppData\Local directory, edit it, and then save it. I can easily solve this current issue I have, where I get an error after saving the file which is the title of this; however, when I do I get another error. It goes something along the lines of failing to close a stream. idk, it doesn't actually pop up in Visual Studio. What it really does is save to the file I'm trying to modify. I've never seen that before. I did have it working at one point, where it saved properly with no errors, but it didn't save the settings correctly.
Imports System.Environment
Imports Microsoft.Win32
Imports System.Security.Permissions
Imports System.IO
Public Class Form1
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
'String used to check if the correct file is being used in the OpenFileDialog
Dim filePath As String
'String that contains the path to the AppData\Local\ path
Dim DOSBoxDirectory As String = (GetFolderPath(SpecialFolder.LocalApplicationData) & "\DOSBox")
'Dim ConfigFile As String = My.Computer.FileSystem.ReadAllText(DOSBoxDirectory & "\dosbox-0.74").ToString
Dim ConfigFile As String
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
OpenFileDialog1.FileName = ""
OpenFileDialog1.ShowDialog()
OpenFileDialog1.Filter = "DOSBox Configuration File|.conf"
OpenFileDialog1.InitialDirectory = (GetFolderPath(SpecialFolder.ApplicationData) & "\Local\DOSBox\")
ConfigFile = OpenFileDialog1.OpenFile.ToString
filePath = OpenFileDialog1.FileName
If Not filePath.Contains("dosbox-0.74") Then
MessageBox.Show("An error has occurred. Have you selected the correct file?", "DOSBox Game Switcher")
Else
MessageBox.Show("DOSBox Configuration File Successfully Found. Filepath: " & filePath, "DOSBox Game Switcher")
End If
OpenFileDialog1.Dispose()
End Sub
'Save the modified configuration file
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim NewConfigFile As String
Try
If radQWERTY.Checked Then
NewConfigFile = ConfigFile.Replace("keyboardlayout=dv103", "keyboardlayout=us")
End If
If radDVORAK.Checked Then
NewConfigFile = ConfigFile.Replace("keyboardlayout=us", "keyboardlayout=dv103")
End If
If radDAGGERFALL.Checked Then
NewConfigFile = ConfigFile.Replace("MOUNT C C:\DOSGames\Arena\ARENA - freesize 2000", "#MOUNT C C:\DOSGames\Arena\ARENA - freesize 2000")
NewConfigFile = ConfigFile.Replace("ARENA.BAT", "#ARENA.BAT")
NewConfigFile = ConfigFile.Replace("#MOUNT C C:\DOSGames\Daggerfall - freesize 2000", "MOUNT C C:\DOSGames\Daggerfall - freesize 2000")
NewConfigFile = ConfigFile.Replace("#MOUNT D C:\DOSGames\Daggerfall\DFCD -t cdrom -label Daggerfall", "MOUNT D C:\DOSGames\Daggerfall\DFCD -t cdrom -label Daggerfall")
NewConfigFile = ConfigFile.Replace("#CD DAGGER", "CD DAGGER")
NewConfigFile = ConfigFile.Replace("#DAGGER.EXE", "DAGGER.EXE")
End If
If radARENA.Checked Then
NewConfigFile = ConfigFile.Replace("#MOUNT C C:\DOSGames\Arena\ARENA - freesize 2000", "MOUNT C C:\DOSGames\Arena\ARENA - freesize 2000")
NewConfigFile = ConfigFile.Replace("#ARENA.BAT", "ARENA.BAT")
NewConfigFile = ConfigFile.Replace("MOUNT C C:\DOSGames\Daggerfall - freesize 2000", "#MOUNT C C:\DOSGames\Daggerfall - freesize 2000")
NewConfigFile = ConfigFile.Replace("MOUNT D C:\DOSGames\Daggerfall\DFCD -t cdrom -label Daggerfall", "#MOUNT D C:\DOSGames\Daggerfall\DFCD -t cdrom -label Daggerfall")
NewConfigFile = ConfigFile.Replace("CD DAGGER", "#CD DAGGER")
NewConfigFile = ConfigFile.Replace("DAGGER.EXE", "#DAGGER.EXE")
End If
If txtMemSize.Text IsNot "" Then
NewConfigFile = ConfigFile.Replace("memsize=16", "memsize=" & Val(txtMemSize.Text))
End If
If Not radARENA.Checked And Not radDAGGERFALL.Checked Then
NewConfigFile = ConfigFile.Replace("MOUNT C C:\DOSGames\Arena\ARENA - freesize 2000", "#MOUNT C C:\DOSGames\Arena\ARENA - freesize 2000")
NewConfigFile = ConfigFile.Replace("ARENA.BAT", "#ARENA.BAT")
NewConfigFile = ConfigFile.Replace("MOUNT C C:\DOSGames\Daggerfall - freesize 2000", "#MOUNT C C:\DOSGames\Daggerfall - freesize 2000")
NewConfigFile = ConfigFile.Replace("MOUNT D C:\DOSGames\Daggerfall\DFCD -t cdrom -label Daggerfall", "#MOUNT D C:\DOSGames\Daggerfall\DFCD -t cdrom -label Daggerfall")
NewConfigFile = ConfigFile.Replace("CD DAGGER", "#CD DAGGER")
NewConfigFile = ConfigFile.Replace("DAGGER.EXE", "#DAGGER.EXE")
End If
Catch ex As Exception
MessageBox.Show("An Error Has Ocurred: " & ErrorToString())
End Try
If Not NewConfigFile = "" Then
My.Computer.FileSystem.WriteAllText(DOSBoxDirectory & "\dosbox-0.74.conf", NewConfigFile, False)
End If
End Sub
'Check if ARENA has been checked and ask to install Arena Remapped from Nexus Mods
Private Sub radARENA_CheckedChanged(sender As Object, e As EventArgs) Handles radARENA.CheckedChanged
Dim result = MessageBox.Show("It is recommended that you get the Arena Remapped mod from Nexus Mods. Would you like to download this mod? It will be automatically installed for you. It messes with DOSBox controls, so when you choose to play Daggerfall it will automatically be uninstalled.", "DOSBox Game Switcher", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
saveBinaryFile1()
'UnRar(My.Computer.FileSystem.SpecialDirectories.Temp & "\TES_Arena_Remapped_1_1_5.zip", DOSBoxDirectory)
'My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.Temp & "\TES_Arena_Remapped_1_1_5.zip")
End If
End Sub
'Automatically uninstall Arena Remapped if it exists
Private Sub radDAGGERFALL_CheckedChanged(sender As Object, e As EventArgs) Handles radDAGGERFALL.CheckedChanged
Try
My.Computer.FileSystem.DeleteFile((DOSBoxDirectory & "mapper.txt"))
My.Computer.FileSystem.DeleteFile(DOSBoxDirectory & "mapper-0.74.map")
Catch ex As Exception
End Try
End Sub
'Used to unpack the Arena Remapped mod
Private Sub UnRar(ByVal WorkingDirectory As String, ByVal filepath As String)
' Microsoft.Win32 and System.Diagnostics namespaces are imported
Dim objRegKey As RegistryKey
objRegKey = Registry.ClassesRoot.OpenSubKey("WinRAR\Shell\Open\Command")
' Windows 7 Registry entry for WinRAR Open Command
Dim obj As Object = objRegKey.GetValue("")
Dim objRarPath As String = obj.ToString()
objRarPath = objRarPath.Substring(1, objRarPath.Length - 7)
objRegKey.Close()
Dim objArguments As String
' in the following format
' " X G:\Downloads\samplefile.rar G:\Downloads\sampleextractfolder\"
objArguments = " X " & " " & filepath & " " + " " + WorkingDirectory
Dim objStartInfo As New ProcessStartInfo()
' Set the UseShellExecute property of StartInfo object to FALSE
' Otherwise the we can get the following error message
' The Process object must have the UseShellExecute property set to false in order to use environment variables.
objStartInfo.UseShellExecute = False
objStartInfo.FileName = objRarPath
objStartInfo.Arguments = objArguments
objStartInfo.WindowStyle = ProcessWindowStyle.Hidden
objStartInfo.WorkingDirectory = WorkingDirectory & "\"
Dim objProcess As New Process()
objProcess.StartInfo = objStartInfo
objProcess.Start()
End Sub
Sub saveBinaryFile1()
Dim b() As Byte = My.Resources.TES_Arena_Remapped_1_1_5
Dim f2 As New FileIOPermission(FileIOPermissionAccess.Read, DOSBoxDirectory)
f2.AddPathList(FileIOPermissionAccess.Write Or FileIOPermissionAccess.Read, DOSBoxDirectory)
My.Computer.FileSystem.WriteAllBytes(DOSBoxDirectory, b, False)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
frmGameDirectories.Show()
End Sub
End Class
I want to get rid of the OpenFileDialog, because it's a huge stain on the program at this point. After that, I need to get the text from the configuration file, and then I need to edit it, and then it needs to be saved. Any help is appreciated. Just so you know, button 2 is what I want to get rid of, and btnSave is where the exceptions come from. Those are the main parts to focus on.
** EDIT **
I fixed the exception, using some general advice from the other post; however, the program isn't editing the file correctly. It's not editing at all.ConfigFile = File.ReadAllText(DOSBoxDirectory & "\dosbox-0.74.conf")
ConfigFile is a string once again. I haven't changed anything in the btnSave Event Procedure EXCEPT that I added the above code to it