2017-06-22 23 views
-2

このプログラムを動作させるには多くの問題があります。その唯一の目的は、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は例外がどこから来るかです。それらは注目すべき主要な部分です。

** EDIT **私は他のポストからのいくつかの一般的なアドバイスを使用して、例外を固定

。ただし、プログラムがファイルを正しく編集していません。それはまったく編集していません。 ConfigFile = File.ReadAllText(DOSBoxDirectory & "\dosbox-0.74.conf") ConfigFileはもう一度文字列です。私はbtnSaveイベントプロシージャで何も変更していない上記のコードを追加した以外は

+0

[IOException:このプロセスは、別のプロセスで使用されているため、ファイル 'ファイルパス'にアクセスできません。](https://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access -the-file-file-path-being-being-being) –

+0

私はあなたがMCVEを作れば、あなたは自分で解決策を見つけることができると確信しています。 –

+0

明確にするために、MCVEは、最小限で完全かつ検証可能な例です。 ( https://stackoverflow.com/help/mcveを参照してください) – Mr47

答えて

0

他の誰かが私の最初の問題に遭遇した場合、解決策はFile.ReadAllText(path)を使用することでした。

関連する問題