我无法理解为什么说该文件已打开。错误大概是一半。
Dim worldWriter As New System.IO.StreamWriter(cmbworldsave) (this is the line that i am having trouble with)
Imports System.IO
Imports System.Text
Public Class frmindex
'Variables
Dim vcmbworld As ComboBox
Dim addworld As String
Dim root As String
Dim hellworld As String
Dim pvp As String
Dim whitelist As String
Dim spawnmonsters As String
Dim onlinemode As String
Dim spawnanimals As String
Dim bit As String
Private Sub frmindex_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If cmbserverapplication.Text = "Jar File" Then
lblram.Visible = True
txbram.Visible = True
Else
lblram.Visible = False
txbram.Visible = False
End If
'Set Default Root
root = CurDir()
'finds load files
txbroot.Text = root
'Checks if save folder exists if not creates one
If (Not System.IO.Directory.Exists(root + "\setting")) Then
System.IO.Directory.CreateDirectory(root + "\setting")
End If
'loads world settings
If File.Exists(root + "\setting\world.txt") Then
For Each line As String In File.ReadLines(root + "\setting\world.txt")
If line.Length <> 0 Then
cmbworld.Items.Add(line)
End If
Next line
End If
'loads ip
If File.Exists(root + "\setting\ip.txt") Then
For Each line As String In File.ReadLines(root + "\setting\ip.txt")
If line.Length <> 0 Then
cmbip.Items.Add(line)
End If
Next line
End If
'loads levelseed
If File.Exists(root + "\setting\levelseed.txt") Then
For Each line As String In File.ReadLines(root + "\setting\levelseed.txt")
If line.Length <> 0 Then
cmblevelseed.Items.Add(line)
End If
Next line
End If
txbseverport.Text = My.Settings.sseverport
txbmaxplayers.Text = My.Settings.smaxplayers
txbspawnprotection.Text = My.Settings.sspawnprotection
txbroot.Text = My.Settings.sroot
cmbworld.Text = My.Settings.sworld
cmbip.Text = My.Settings.sseverip
cmblevelseed.Text = My.Settings.slevelseed
cmbserverapplication.Text = My.Settings.sseverapplication
txbram.text = My.Settings.sram
hellworld = My.Settings.shellworld
pvp = My.Settings.spvp
whitelist = My.Settings.swhitelist
spawnmonsters = My.Settings.sspawnmonsters
onlinemode = My.Settings.sonlinemode
spawnanimals = My.Settings.sspawnanimals
cbxhellworld.Checked = hellworld
cbxpvp.Checked = pvp
cbxwhitelist.Checked = whitelist
cbxspawnmonsters.Checked = spawnmonsters
cbxonlinemode.Checked = onlinemode
cbxspawnanimals.Checked = spawnanimals
End Sub
Private Sub btnrootbrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnrootbwse.Click
' Creates a FolderBrowserDialog object
Dim fbdroot As New FolderBrowserDialog
'Dialog
With fbdroot
' Desktop is the root folder in the dialog.
.RootFolder = Environment.SpecialFolder.Desktop
' Select the C:\Windows directory on entry.
.SelectedPath = CurDir()
' Prompt the user with a custom message.
.Description = "Please Select Root Of Sever"
root = .SelectedPath
If .ShowDialog = DialogResult.OK Then
root = .SelectedPath
txbroot.Text = root
End If
End With
End Sub
Private Sub btnbwsworld_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnworld.Click
If Not cmbworld.Text = "" Then
cmbworld.Items.Add(cmbworld.Text)
cmbworld.Text = ""
End If
End Sub
Private Sub btnaddip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddip.Click
If Not cmbip.Text = "" Then
cmbip.Items.Add(cmbip.Text)
cmbip.Text = ""
End If
End Sub
Private Sub btnlaunch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlaunch.Click
If System.IO.Directory.Exists(root + "\setting") = True Then
Else
System.IO.Directory.CreateDirectory(root + "\setting")
End If
Dim sbcmbworld As New StringBuilder()
For Each item As Object In cmbworld.Items
sbcmbworld.AppendFormat("{0} {1}", item, Environment.NewLine)
Next
Dim cmbworldsave As String = root + "\setting\world.txt"
If System.IO.File.Exists(cmbworldsave) = True Then
Else
System.IO.File.Create(root + "\setting\world.txt")
End If
Dim worldWriter As New System.IO.StreamWriter(cmbworldsave) **<---- ERROR HERE!**
worldWriter.Write(sbcmbworld.ToString()) 'Use the stringbuilder here
worldWriter.WriteLine()
worldWriter.Close()
Dim sbcmbip As New StringBuilder()
For Each item As Object In cmbworld.Items
sbcmbip.AppendFormat("{0} {1}", item, Environment.NewLine)
Next
Dim cmbipsave As String = root + "\setting\ip.txt"
If System.IO.File.Exists(cmbworldsave) = True Then
Else
System.IO.File.Create(root + "\setting\ip.txt")
End If
Dim ipwriter As New System.IO.StreamWriter(cmbipsave)
ipwriter.Write(sbcmbip.ToString()) 'Use the stringbuilder here
ipwriter.WriteLine()
ipwriter.Close()
Dim sbcmblevelseed As New StringBuilder()
For Each item As Object In cmbworld.Items
sbcmbip.AppendFormat("{0} {1}", item, Environment.NewLine)
Next
Dim cmblevelseedsave As String = root + "\setting\levelseed.txt"
If System.IO.File.Exists(cmbworldsave) = True Then
Else
System.IO.File.Create(root + "\setting\levelseed.txt")
End If
Dim levelseedWriter As New System.IO.StreamWriter(cmbipsave)
levelseedWriter.Write(sbcmblevelseed.ToString()) 'Use the stringbuilder here
levelseedWriter.WriteLine()
levelseedWriter.Close()
If cbxhellworld.Checked Then
hellworld = "true"
Else
hellworld = "false"
End If
If cbxpvp.Checked Then
pvp = "true"
Else
pvp = "false"
End If
If cbxwhitelist.Checked Then
whitelist = "true"
Else
whitelist = "false"
End If
If cbxspawnmonsters.Checked Then
spawnmonsters = "true"
Else
spawnmonsters = "false"
End If
If cbxonlinemode.Checked Then
onlinemode = "true"
Else
onlinemode = "false"
End If
If cbxspawnanimals.Checked Then
spawnanimals = "true"
Else
spawnanimals = "false"
End If
My.Settings.sseverport = txbseverport.Text
My.Settings.smaxplayers = txbmaxplayers.Text
My.Settings.sspawnprotection = txbspawnprotection.Text
My.Settings.sroot = txbroot.Text
My.Settings.sworld = cmbworld.Text
My.Settings.sseverip = cmbip.Text
My.Settings.slevelseed = cmblevelseed.Text
My.Settings.sseverapplication = cmbserverapplication.Text
My.Settings.sram = txbram.text
My.Settings.shellworld = hellworld
My.Settings.spvp = pvp
My.Settings.swhitelist = whitelist
My.Settings.sspawnmonsters = spawnmonsters
My.Settings.sonlinemode = onlinemode
My.Settings.sspawnanimals = spawnanimals
My.Settings.Save()
If System.IO.File.Exists(root + "\server.properties") = True Then
Else
System.IO.File.Create(root + "\server.properties")
End If
Dim propertyWriter As New System.IO.StreamWriter(root + "\server.properties")
propertyWriter.Write("level-name=" + cmbworld.Text)
propertyWriter.WriteLine("")
propertyWriter.Write("hellworld=" + hellworld)
propertyWriter.Write("spawn-monsters=" + spawnmonsters)
propertyWriter.WriteLine("")
propertyWriter.Write("online-mode=" + onlinemode)
propertyWriter.WriteLine("")
propertyWriter.Write("max-players=" + txbmaxplayers.Text)
propertyWriter.WriteLine("")
propertyWriter.Write("server-ip=" + cmbip.Text)
propertyWriter.WriteLine("")
propertyWriter.Write("pvp=" + pvp)
propertyWriter.WriteLine("")
propertyWriter.Write("level-speed=" + cmblevelseed.Text)
propertyWriter.WriteLine("")
propertyWriter.Write("sever-port=" + txbseverport.Text)
propertyWriter.WriteLine("")
propertyWriter.Write("spawn-monsters=" + spawnmonsters)
propertyWriter.WriteLine("")
propertyWriter.Close()
Dim serverpath As String
If cmbserverapplication.Text = "Jar File" Then
If System.IO.File.Exists(root + "\minecraft_server.jar") = True Then
If Environment.Is64BitOperatingSystem = True Then
bit = "64"
Else
bit = "32"
End If
serverpath = root + "\minecraft_server.jar"
Dim javapath As String
Dim launchcode As String
If bit = 64 Then
javapath = "C:\Program Files (x86)\Java\jre6\bin\java"
Else
javapath = "C:\Program Files\Java\jre6\bin\java"
End If
launchcode = "-Xmx" + txbram.Text + "M -Xms" + txbram.Text + "M -jar " + serverpath + " nogui"
Dim pi As New ProcessStartInfo(javapath)
pi.Arguments = launchcode
Process.Start(pi)
Else
MsgBox("The server file does not exist. Please make sure that you have select the right file type and that it has not been renamed.")
End If
Else
If System.IO.File.Exists(root + "\minecraft_server.exe") = True Then
serverpath = root + "\minecraft_server.exe"
Dim startInfo As System.Diagnostics.ProcessStartInfo
Dim pStart As New System.Diagnostics.Process
startInfo = New System.Diagnostics.ProcessStartInfo(serverpath)
pStart.StartInfo = startInfo
pStart.Start()
Else
MsgBox("The server file does not exist. Please make sure that you have select the right file type and that it has not been renamed.")
End If
End If
End Sub
Private Sub btnremoveworld_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnremoveworld.Click
cmbworld.Items.RemoveAt(cmbworld.SelectedIndex)
End Sub
Private Sub btnremoveip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnremoveip.Click
cmbip.Items.RemoveAt(cmbip.SelectedIndex)
End Sub
Private Sub btnaddlevelseed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddlevelseed.Click
cmblevelseed.Items.Add(cmblevelseed.Text)
cmblevelseed.Text = ""
End Sub
Private Sub btnremovelevelseed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnremovelevelseed.Click
cmblevelseed.Items.RemoveAt(cmblevelseed.SelectedIndex)
End Sub
Private Sub cmbserverapplication_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbserverapplication.SelectedIndexChanged
If cmbserverapplication.Text = "Jar File" Then
lblram.Visible = True
txbram.Visible = True
Else
lblram.Visible = False
txbram.Visible = False
End If
End Sub
End Class
答案 0 :(得分:3)
它说该文件正在使用中,因为File.Create返回了您需要关闭/处理的流对象。
返回值
输入:System.IO.FileStream
FileStream,提供对路径中指定的文件的读/写访问权。
至少,改变对方法的每次调用,例如这两个:
System.IO.File.Create(root + "\setting\world.txt")
System.IO.File.Create(root + "\setting\levelseed.txt")
对此:
System.IO.File.Create(root + "\setting\world.txt").Dispose()
System.IO.File.Create(root + "\setting\levelseed.txt").Dispose()
或者,您可以使用File类File.WriteAllText的其他方法:
System.IO.File.WriteAllText(root + "\setting\world.txt", "")
System.IO.File.WriteAllText(root + "\setting\levelseed.txt", "")