Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 20-Nov-17 3:39am
Comments
Richard MacCutchan 20-Nov-17 9:21am    
Don't use the C: root to create new directories and files. However, you just need to add the code to write a new text file. StreamWriter class is probably a good choice.
[no name] 20-Nov-17 9:42am    
Hi Richard,
Why shouldn't i use the C: root? I mean it works so far, is there a better option?
Yes i know i need to add the code for the text file, but that's the problem. I can do it with a new button, but don't know how to add it to the existing button so that it knows to give the file the same name as the folder.
I'll go and play around with it some more :D
Richard Deeming 20-Nov-17 11:32am    
Two obvious reasons to avoid the root of C: - permissions, and clutter.

Since Windows Vista, regular users don't have permission to create files or folders in the root of C:, so they won't be able to install or use your application without administrator approval.

And by putting your files in the root, you're cluttering up your users' file system with files that only make sense to your application.

Unless you have a really good reason not to, you should store your application's files in an application-specific folder under the AppData path.

Where should I store my data?[^]
[no name] 20-Nov-17 14:09pm    
Thanks for the heads up, i guess going for AppData isn't a bad thing. As for permissions and vista, and i apologize to any vista users that fell for buying (i did) or are still using it, but i am only aiming at windows 10 users. It's not a real choice as my app is to work alongside someone else's app that ONLY runs on win10 machines.
Thanks again
Richard Deeming 20-Nov-17 14:41pm    
When I said "since Vista", I meant any version from Vista onwards. So that includes Windows 10. :)

1 solution

Okay, once you have that, you can just add a new file and write it with the same name.
VB
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")
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[^]
 
Share this answer
 
Comments
[no name] 20-Nov-17 10:23am    
Afzaal Ahmad Zeeshan

Thank you very much, that did the job.

Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900