Visual studio visual basic写入文件
当我想用VisualBasic写一个文件时,它说他不能,因为它已经在另一个过程中了,但是其他任何东西都没有。 她是我的密码Visual studio visual basic写入文件,visual-studio,streamwriter,Visual Studio,Streamwriter,当我想用VisualBasic写一个文件时,它说他不能,因为它已经在另一个过程中了,但是其他任何东西都没有。 她是我的密码 Imports System.IO Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Button1.Visible = False Label1.Visible = False
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Visible = False
Label1.Visible = False
pathlocation.Visible = True
Button2.Visible = True
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
pathlocation.Visible = False
Button2.Visible = False
End Sub
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim path As String
Dim modsl As String = "/modshop"
Dim config As String = "/config"
Dim configfile As String = "/config.txt"
Dim configwriter As System.IO.StreamWriter
path = pathlocation.Text
path = path + modsl
My.Computer.FileSystem.CreateDirectory(path)
config = path + config
My.Computer.FileSystem.CreateDirectory(config)
configfile = config + configfile
File.Create(configfile)
configwriter = File.AppendText(configfile)
configwriter.Write(path)
configwriter.Close()
Button2.Visible = False
End Sub
Private Sub pathlocation_TextChanged(sender As Object, e As EventArgs) Handles pathlocation.TextChanged
End Sub
End Class
File.Create创建文件并返回文件流,因此下面的File.AppendText会查找您自己已经打开的文件,并失败 考虑到这一点以及该方法在传递的路径中构建了所有缺失的目录,您可以大大简化代码
Dim path = System.IO.Path.Combine(pathlocation.Text, "modshop")
Dim fullPath = System.IO.Path.Combine(path, "config")
Directory.CreateDirectory(fullPath)
Dim configFile = System.IO.Path.Combine(fullPath, "config.txt")
File.WriteAllText(configFile, path)
按照我使用的代码逻辑创建或覆盖配置文件(如果存在)。如果要附加到现有文件而不覆盖其内容,请使用他给出的错误:严重性代码描述项目文件行抑制状态错误BC30980无法从包含“path”的表达式推断“path”类型。modshop F:\modshop\modshop\Form1.vb 21 active,该问题是由System.IO中的类Path和名为Path的变量引起的。您可以更改该变量名(例如更改为filePath)或将完整名称空间System.IO添加到Path类中。使用最后一个选项更新答案可以帮助它工作,但是如果我想使用writer=newstreamwriter(configFile)writer.WriteLine(path)逐行写入,该怎么办