所以我在使这个程序工作时遇到了很多麻烦。其唯一目的是编辑DOSBox模拟器的配置文件。现在,我正在尝试制作它,以便我可以获取位于AppData \ Local目录中的DOSBox配置文件,编辑它,然后保存它。我可以很容易地解决我当前的这个问题,在保存文件后出现错误,这是一个标题;但是,当我这样做时,我得到另一个错误。它有点像未能关闭流。 idk,它实际上并没有在Visual Studio中弹出。它真正做的是保存到我试图修改的文件中。我以前从没见过。我确实让它在某一点工作,它没有错误地正确保存,但它没有正确保存设置。
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
我想摆脱OpenFileDialog,因为此时它对程序来说是一个巨大的污点。之后,我需要从配置文件中获取文本,然后我需要编辑它,然后需要保存它。任何帮助表示赞赏。你知道,按钮2是我想要摆脱的,而btnSave是异常的来源。这些是要关注的主要部分。
**编辑**
我使用其他帖子的一些一般性建议修复了异常;但是,程序无法正确编辑文件。根本没有编辑。ConfigFile = File.ReadAllText(DOSBoxDirectory & "\dosbox-0.74.conf")
ConfigFile再次是一个字符串。我没有改变btnSave事件程序中的任何内容除了我将上面的代码添加到它
答案 0 :(得分:0)
以防其他人遇到我的初始问题,解决方案是使用File.ReadAllText(路径)。