Visual Basic 写入文件
visual basic writing into files
当我想用 Visual Basic 写入一个文件时,它说他不能因为它已经在另一个过程中但是没有其他东西可以操作它。
她是我的代号。
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创建文件,returns一个FileStream,因此下面File.AppendText找到自己已经打开的文件,失败
考虑到这一点以及 Directory.CreateDirectory 方法在传递的路径中构建所有缺失目录这一事实,您可以简化很多代码
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)
按照我使用的代码的逻辑File.WriteAllText that creates or overwrite the configFile if it exists. If you want to append to an existing file without overwriting its contents then use File.AppendAllText
当我想用 Visual Basic 写入一个文件时,它说他不能因为它已经在另一个过程中但是没有其他东西可以操作它。 她是我的代号。
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创建文件,returns一个FileStream,因此下面File.AppendText找到自己已经打开的文件,失败
考虑到这一点以及 Directory.CreateDirectory 方法在传递的路径中构建所有缺失目录这一事实,您可以简化很多代码
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)
按照我使用的代码的逻辑File.WriteAllText that creates or overwrite the configFile if it exists. If you want to append to an existing file without overwriting its contents then use File.AppendAllText