问题描述
我有点困难,试图获得一个按钮来创建一个目录,然后将.txt添加到该新文件夹
用户点击保存按钮,我的应用程序,根据用户放入textBox的名称/标题在C盘上创建一个目录。这很好。
我的问题是我希望应用程序也在该文件夹中创建一个与该文件夹同名的.txt文件。
当用户添加名称并点击保存按钮时,我用它来创建目录。
我需要添加什么才能使它同时创建一个文本文件时间?
问候
我尝试过:
Hi, i am having a bit of a hard time trying to get a button to create a directory and then add a .txt to that new folder
When the user hits the save button, my app, creates a directory on the C drive based on the Name/Title the user put into the textBox. This works fine.
My problem is that i want the app to also create a .txt file in that new folder with the same name as that folder.
This what i am using to create the Directory when the user adds a name and hits the save button.
What do i need to add to make it create a text file at the same time?
Regards
What I have tried:
Public Class FormEdit
Private Sub ButtonTitleSave_Click(sender As Object, e As EventArgs) Handles ButtonTitleSave.Click
Dim path As String = ("C:\SpotLocker\" & TextBoxTitle.Text)
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
PanelTitle.Hide()
PanelURL.Show()
End Sub
推荐答案
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
' create file here; e.g. "C:\directory\name\name.txt"
File.Create(path + "\\" + TextBoxTitle.Text + ".txt")
这将创建文件并打开该文件的流 - 关闭它,在 .Close() >创建()功能。请注意,我在If块中创建了文件,仅在目录不存在时创建文件 - 如果您的目录存在,由于代码错误,请删除目录并再次运行代码。
[]
This would create the file and open the stream to that file — to close that, do .Close()
on the Create()
function. Note that, I created the file inside the If block, to only create the file when the directory did not exist — if your directory exists, due to faulty code, remove the directory and run the code again.
How to: Create a File in Visual Basic | Microsoft Docs[^]
这篇关于单击并创建文件夹和文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!