所以我在使这个程序运行时遇到了很多麻烦。其唯一目的是编辑DOSBox模拟器的配置文件。现在,我正在尝试使其可以使位于AppData \ Local目录中的DOSBox配置文件编辑它,然后保存它。我可以很容易地解决我目前遇到的这个问题,在保存文件的标题后出现错误;但是,当我做我得到另一个错误。它沿着未能关闭流的方向前进。 idk,它实际上并没有在Visual Studio中弹出。它真正做的是保存到我正在修改的文件中。我以前从来没有见过。我确实在一点上工作,在没有错误的情况下正确保存,但没有正确保存设置。正在被另一个进程使用的vb.net文件
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事件程序中的任何东西除了我将上面的代码添加到它之外
可能重复[IOException:进程无法访问该文件的'文件路径',因为它正在被另一个进程使用](https://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access -the-file-file-path-because-it-being-being) –
我很肯定,如果你制作一个MCVE,你会自己找到解决方案。 –
要明确,MCVE是一个最小,完整和可验证的示例。 (请参阅 https://stackoverflow.com/help/mcve) – Mr47