Hello there!

This forums has helped me a lot to learn programming now I need your help again. I'm try to figure out a way to create a folder based on users input in Textbox on desktop.

Public Class Form1

    Dim username As TextBox

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim USERPROFILE As String
        USERPROFILE = Environ("HOMEPATH")
        Dim oShell : oShell = CreateObject("WScript.shell")
        IO.Directory.CreateDirectory(USERPROFILE & "\Desktop\"username)
    End Sub
End Class

It giving me errors. Can someone correct me and give a sample code for this?

Any help in appreciated.

Dani AI

Generated

A few practical additions to the thread (building on 's and 's points) that avoid the common pitfalls and make the solution robust and user-friendly.

Prefer using the framework helpers rather than manual string concatenation or raw environment calls. Create the folder from a deliberate action (for example a Button.Click or an AcceptButton on the form) instead of the TextBox.TextChanged event so a directory is not created on every keystroke. Sanitize the textbox value for invalid filename characters and reserved Windows names, then combine paths with Path.Combine and wrap the create call in a Try/Catch to handle permissions or invalid-path exceptions.

Imports System.IO

Private Sub CreateFolderFromTextbox()
    Dim folderName = TextBox1.Text.Trim()
    If String.IsNullOrEmpty(folderName) Then Return

    ' Replace invalid filename characters
    For Each ch In Path.GetInvalidFileNameChars()
        folderName = folderName.Replace(ch, "_"c)
    Next

    ' Avoid reserved names like CON, PRN, COM1..COM9, LPT1..LPT9
    Dim reserved = New HashSet(Of String)(StringComparer.OrdinalIgnoreCase) From {"CON","PRN","AUX","NUL"}
    For i = 1 To 9
        reserved.Add("COM" & i)
        reserved.Add("LPT" & i)
    Next
    If reserved.Contains(folderName) Then folderName = "_" & folderName

    Dim desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
    Dim newPath = Path.Combine(desktopPath, folderName)
    Try
        Directory.CreateDirectory(newPath)
        MessageBox.Show("Folder created: " & newPath)
    Catch ex As Exception
        MessageBox.Show("Could not create folder: " & ex.Message)
    End Try
End Sub

Troubleshooting checklist: confirm the path returned by Environment.GetFolderPath is valid on the machine, inspect exception messages for permission or path-length errors, and use a debounced approach (Timer or explicit button) if automatic creation based on typing is still desired.

Recommended Answers

All 6 Replies

What are the errors?

Nothing actually. It's not even compiling. Error at this line IO.Directory.CreateDirectory(USERPROFILE & "\Desktop\"username)

You need a symbol to concatenate the "\Desktop\" and username. Also, you should be referencing the Text property of the TextBox username, not the TextBox itself.

commented: Thanks +0

Could you show me an example?

IO.Directory.CreateDirectory(USERPROFILE & "\Desktop\"username) 

I see an error. There is no & between "\Desktop\" and username

IO.Directory.CreateDirectory(USERPROFILE & "\Desktop\" & username)

You should also place all the calls outsite that event handler. You will be taking a memory hit. The event will fire everytime the text changes. So, as you type a directory, it will be creating a directory for every typed character.

Example:

MyDirectory typed in will create these directories:
M
My
MYD
MYDi
...

commented: Thanks +0

Thanks. It works fine now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.