• 0

[Visual Basic] help


Question

How can you delete a certain file in a special directory such as the AppData folder?

I'm trying to make it so that when a user clicks on a button, it will delete a certain file in the AppData directory.

For example:

When a user clicks the button, it will delete "test.doc" from "AppData\Test"

I know it may sound confusing, but i hope you can understand what I'm trying to do. :)

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

What i understand is you know the path of the AppData folder

so you can do this

one of the following things

To delete a text file

My.Computer.FileSystem.DeleteFile("C:\test.doc")

To delete a text file and ask the user to confirm that the file should be deleted

My.Computer.FileSystem.DeleteFile("C:\test.doc", _
		FileIO.UIOption.AllDialogs, FileIO.RecycleOption.DeletePermanently, FileIO.UICancelOption.DoNothing)

To delete a text file and send it to the Recycle Bin

My.Computer.FileSystem.DeleteFile("C:\test.doc", _
FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)

More info on :

http://msdn.microsoft.com/en-us/library/tdx72k4b(VS.80).aspx

Hope it helps :cool:

Link to comment
Share on other sites

  • 0

Try this

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		Dim File = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Test\test.doc"

		If My.Computer.FileSystem.FileExists(File) Then
			' Place Code To Delete File
			MsgBox("Confirm File" & vbCrLf & File)
		Else
			' If Needed 
			MsgBox("Missing File" & vbCrLf & File)
		End If
	End Sub
Link to comment
Share on other sites

  • 0

You really should be able to use the above posts to solve your problem. Did you read any of the code that people posted? None of it actually does everything for you; you needed to piece things together. Anyway, here's the final code for the lazy (with a little bit of error-checking for those unexpected moments):

Imports System.IO

Public Class Form1

	'Defines the file to be deleted
	Dim sFileToDelete As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\test\test.doc"

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		'First, check to make sure the file exists on the filesystem
		If File.Exists(sFileToDelete) Then

			'Use Try...Catch block to catch possible errors
			Try

				'Attempt to delete the file and show a confirmation box if it is successful
				File.Delete(sFileToDelete)

				'Confirm that the file was deleted
				If Not File.Exists(sFileToDelete) Then
					MessageBox.Show("The file '" & sFileToDelete & "' was deleted")
				End If

				'Catch 3 common file access errors
			Catch exNotFound As FileNotFoundException
				MessageBox.Show("The file '" & sFileToDelete & "' was not found")
			Catch exAccess As UnauthorizedAccessException
				MessageBox.Show("The file '" & sFileToDelete & "' could not be accessed")
			Catch exIO As IOException
				MessageBox.Show("The file '" & sFileToDelete & "' is being used by another process")

				'Catch any other generic error
			Catch ex As Exception
				MessageBox.Show("Error: " & ex.Message)
			End Try

		Else

			'Message if the file is not found
			MessageBox.Show("The file '" & sFileToDelete & "' was not found")
		End If

	End Sub
End Class

Link to comment
Share on other sites

  • 0
You really should be able to use the above posts to solve your problem. Did you read any of the code that people posted? None of it actually does everything for you; you needed to piece things together. Anyway, here's the final code for the lazy (with a little bit of error-checking for those unexpected moments):

That worked great, thanks to everyone that helped me out! :D

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.