Member Avatar for Member #854131

I would like to do a coding to delete the txt.file that i had created before using visual studio..can anyone teach me the coding?thank you.

Dani AI

Generated

Short expert summary and practical fixes for the VS2010 VB.NET thread (original poster , replies from and ). The two common UI flows in the thread are: 1) an explorer-style control that returns a full path, and 2) an OpenFileDialog. If the Delete button appears to "do nothing", the problem is almost always one of wiring, path/permission, or an exception being swallowed.

Quick checklist to diagnose the button that does nothing:

  • Confirm the button Click handler is actually attached (double-click the button in the designer to generate a handler with Handles btnDelete.Click).
  • If using OpenFileDialog, check the dialog result before reading FileName so no empty path is used.
  • Dump the chosen path to Output/MessageBox to verify it is the expected full path.
  • Watch for exceptions: a locked file, read-only attribute, or lack of filesystem permission will prevent deletion.
  • Remember Recycle Bin behavior: some APIs permanently delete; others can send to Recycle Bin. Network or removable volumes may not use Recycle Bin.

Example: move the selected file to the Recycle Bin and show clear error messages (VB.NET). This uses the VisualBasic FileSystem helper and explicit exception handling.

Imports Microsoft.VisualBasic.FileIO

' inside a properly wired Click handler:
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    Dim path As String = OpenFileDialog1.FileName
    Try
        FileSystem.DeleteFile(path, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin)
        MessageBox.Show("Moved to Recycle Bin")
    Catch ex As IOException
        MessageBox.Show("File in use: " & ex.Message)
    Catch ex As UnauthorizedAccessException
        MessageBox.Show("Permission denied: " & ex.Message)
    Catch ex As Exception
        MessageBox.Show("Delete failed: " & ex.Message)
    End Try
End If

Notes: use FileSystem.DeleteFile to send to the Recycle Bin; use System.IO.File.Delete when a permanent delete is required. Add debug output and explicit catches so failures become visible rather than silent. This resolves the usual causes raised in the thread and complements and suggestions.

Recommended Answers

All 14 Replies

Have a look at this link with some sample code Here.

Member Avatar for Member #854131

thank you for the reply.but i mean is using visual studio(basic)to delete the file.and also i want to delete file that will choose by user,not the fix one txt file.thank you.

Are you using VB6 or VB.Net?

Member Avatar for Member #854131

i am using visual studio 2010 doing the visual basic.

Download the sample application .

This will load all files explorer style. Now use the code in the link I supplied earlier on to delete a selected file.:)

Member Avatar for Member #854131

Thank you.but how can i join the both coding?the sample application gt so many form n coding inside..and is it the sample use for searching the folder?if i use open file dialog can delete the file?

imports system.io

file.delete("pathtofile.txt")

Open file dialog will not work. With the link above, using explorer style, the user can load files and by clicking on a file, the path is returned. This way you can now delete the file as in -

Dim FileToDelete As String

FileToDelete = "C:\Users\Owner\Documents\testDelete.txt"

If System.IO.File.Exists(FileToDelete) = True Then

System.IO.File.Delete(FileToDelete)
MsgBox("File Deleted")

End If

if i understand what you are asking you can deleter the file with using an open file dialog..

OpenFileDialog1.ShowDialog()

Dim FilePath as String = OpenFileDialog1.Filename

File.Delete(FilePath)

Just use AndreW's Code In The Middle To Be Sure That The File Exists.

Member Avatar for Member #854131

Open file dialog will not work. With the link above, using explorer style, the user can load files and by clicking on a file, the path is returned. This way you can now delete the file as in -

Dim FileToDelete As String

FileToDelete = "C:\Users\Owner\Documents\testDelete.txt"

If System.IO.File.Exists(FileToDelete) = True Then

System.IO.File.Delete(FileToDelete)
MsgBox("File Deleted")

End If

thank you.i directly copy ur coding inside my project, but the button delete didnt work anything.

Member Avatar for Member #854131

if i understand what you are asking you can deleter the file with using an open file dialog..

OpenFileDialog1.ShowDialog()

Dim FilePath as String = OpenFileDialog1.Filename

File.Delete(FilePath)

Just use AndreW's Code In The Middle To Be Sure That The File Exists.

thank you.it really work.but can the file i want to delete will go to recycle bin?

Yes, it will be added to the recycle bin.

If you want to clear the recycle bin, try the following -

Private Declare Function SHEmptyRecycleBin Lib "shell32.dll" Alias
"SHEmptyRecycleBinA" (ByVal hWnd As Int32, ByVal pszRootPath As String,
ByVal dwFlags As Int32) As Int32
Private Declare Function SHUpdateRecycleBinIcon Lib "shell32.dll" () As
Int32

Private Const SHERB_NOCONFIRMATION = &H1
Private Const SHERB_NOPROGRESSUI = &H2
Private Const SHERB_NOSOUND = &H4

#Region "Empty Recycle Bin (SUB)"

Private Sub EmptyRecycleBin()
SHEmptyRecycleBin(Me.Handle.ToInt32, vbNullString, SHERB_NOCONFIRMATION +
SHERB_NOSOUND)
SHUpdateRecycleBinIcon()
End Sub

#End Region
Member Avatar for Member #854131

Yes, it will be added to the recycle bin.

If you want to clear the recycle bin, try the following -

Private Declare Function SHEmptyRecycleBin Lib "shell32.dll" Alias
"SHEmptyRecycleBinA" (ByVal hWnd As Int32, ByVal pszRootPath As String,
ByVal dwFlags As Int32) As Int32
Private Declare Function SHUpdateRecycleBinIcon Lib "shell32.dll" () As
Int32

Private Const SHERB_NOCONFIRMATION = &H1
Private Const SHERB_NOPROGRESSUI = &H2
Private Const SHERB_NOSOUND = &H4

#Region "Empty Recycle Bin (SUB)"

Private Sub EmptyRecycleBin()
SHEmptyRecycleBin(Me.Handle.ToInt32, vbNullString, SHERB_NOCONFIRMATION +
SHERB_NOSOUND)
SHUpdateRecycleBinIcon()
End Sub

#End Region

nope.the coding u given before this post is straightly delete the file.cant find it in recycle bin..thank you.

commented: For not giving up on trying until it works. +4

It was a pleasure. Please mark as solved if there are no more questions regarding the deletion of a text file, thanks.:)

I gave you some rep points for trying until it works, well done!

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.