how to delete file in VB2008?!?!

How do i identify and delete a file in vb2008?!

Page 1 of 1

8 Replies - 12261 Views - Last Post: 15 April 2009 - 12:26 PM Rate Topic: -----

#1 NickBudi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-April 09

how to delete file in VB2008?!?!

Posted 14 April 2009 - 06:46 PM

How do you identify and delete a file in Visual Basic 2008??

I know how to do it in vb2005, but the method doesn't work in VB2008.

I'm basically trying to say:
If (filepath) exist, then delete (filepath)
(thats in english, not in code lol)


Anyone know how to do that in VB2008?


TY SO MUCH in Advance B)

Is This A Good Question/Topic? 0
  • +

Replies To: how to delete file in VB2008?!?!

#2 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: how to delete file in VB2008?!?!

Posted 14 April 2009 - 10:29 PM

use this in vb 2008
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Try
			Dim iPath As String
			iPath = "C:\test.txt"
			If File.Exists(iPath) Then
				File.Delete(iPath)
			End If
		Catch ex As Exception
			MsgBox(ex.Message)
		End Try
	End Sub


Was This Post Helpful? 0
  • +
  • -

#3 NickBudi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-April 09

Re: how to delete file in VB2008?!?!

Posted 15 April 2009 - 08:25 AM

View Postnoorahmad, on 14 Apr, 2009 - 09:29 PM, said:

use this in vb 2008
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Try
			Dim iPath As String
			iPath = "C:\test.txt"
			If File.Exists(iPath) Then
				File.Delete(iPath)
			End If
		Catch ex As Exception
			MsgBox(ex.Message)
		End Try
	End Sub



I am aware of this method, but in vb2008, i cannot run because "File" is not defined, i have used this method before in vb2005, but it doesnt work in vb2008
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8964
  • View blog
  • Posts: 33,624
  • Joined: 12-June 08

Re: how to delete file in VB2008?!?!

Posted 15 April 2009 - 08:34 AM

View PostNickBudi, on 15 Apr, 2009 - 09:25 AM, said:

View Postnoorahmad, on 14 Apr, 2009 - 09:29 PM, said:

use this in vb 2008
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Try
			Dim iPath As String
			iPath = "C:\test.txt"
			If File.Exists(iPath) Then
				File.Delete(iPath)
			End If
		Catch ex As Exception
			MsgBox(ex.Message)
		End Try
	End Sub



I am aware of this method, but in vb2008, i cannot run because "File" is not defined, i have used this method before in vb2005, but it doesnt work in vb2008


*sigh* File is still there. The class has not been depreciated.

Either add a reference to System.IO or write out your File as System.IO.File. what ever..

This post has been edited by modi123_1: 15 April 2009 - 08:34 AM

Was This Post Helpful? 0
  • +
  • -

#5 kasbaba  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 133
  • Joined: 03-November 08

Re: how to delete file in VB2008?!?!

Posted 15 April 2009 - 08:34 AM

View PostNickBudi, on 15 Apr, 2009 - 07:25 AM, said:

I am aware of this method, but in vb2008, i cannot run because "File" is not defined, i have used this method before in vb2005, but it doesnt work in vb2008


Hi,

You would need to use IO for file operations.

Simply type :
Imports System.IO
outside your class on the top.

to Use it:

		If IO.File.Exists("C:\TestFile.txt") Then
			IO.File.Delete("C:\TestFile.txt")
			MsgBox("File Deleted !")
		Else
			MsgBox("Could not find file to Delete")
		End If


Similarly to delete Folders/Directories use:

		If IO.Directory.Exists("C:\Myfolder") Then
			IO.Directory.Delete("C:\Myfolder")
			MsgBox("Folder Deleted !")
		Else
			MsgBox("Folder does not Exist")
		End If


hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 NickBudi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-April 09

Re: how to delete file in VB2008?!?!

Posted 15 April 2009 - 09:10 AM

Dim strUsername As String = SystemInformation.UserName
		Dim strPath As String = "C:\Users\" & strUsername & "\Desktop\ijji.lnk"
		If IO.File.Exists(strPath) Then
			IO.File.Delete(strPath)
		Else
			txt1.Text = "N/A"
		End If


This is what im trying to do in vb 2008 Kasaba. At the very very top of my code,above the class, i put
Imports System.IO


And everytime i run it, txt1 says "N/A"

I positive that ijji.lnk (internet link) is on my desktop, im looking at it right now lol

any other ideas? maybe this command wont delete .lnk's?
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8964
  • View blog
  • Posts: 33,624
  • Joined: 12-June 08

Re: how to delete file in VB2008?!?!

Posted 15 April 2009 - 09:36 AM

What's your OS?

"C:\Users\" & strUsername & "\Desktop\ijji.lnk"

Doesn't look like an XP folder structure.
Was This Post Helpful? 0
  • +
  • -

#8 kasbaba  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 133
  • Joined: 03-November 08

Re: how to delete file in VB2008?!?!

Posted 15 April 2009 - 11:14 AM

View PostNickBudi, on 15 Apr, 2009 - 08:10 AM, said:

Dim strUsername As String = SystemInformation.UserName
		Dim strPath As String = "C:\Users\" & strUsername & "\Desktop\ijji.lnk"
		If IO.File.Exists(strPath) Then
			IO.File.Delete(strPath)
		Else
			txt1.Text = "N/A"
		End If


Hi,

I wonder why it is not working if the Path is corrent.

To add to what modi123_1 said in the post earlier, Debug the code..try taking your mouse cursor over strPath & see what value it contains.

Check if the path is exactly where your ijji.lnk file is

Also..to access your local desktop..instead of writing:
Dim strPath As String = "C:\Users\" & strUsername & "\Desktop\ijji.lnk"

try writing:
Dim strPath As String = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\ijji.lnk"


hope this helps.
Was This Post Helpful? 0
  • +
  • -

#9 NickBudi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-April 09

Re: how to delete file in VB2008?!?!

Posted 15 April 2009 - 12:26 PM

View Postkasbaba, on 15 Apr, 2009 - 10:14 AM, said:

View PostNickBudi, on 15 Apr, 2009 - 08:10 AM, said:

Dim strUsername As String = SystemInformation.UserName
		Dim strPath As String = "C:\Users\" & strUsername & "\Desktop\ijji.lnk"
		If IO.File.Exists(strPath) Then
			IO.File.Delete(strPath)
		Else
			txt1.Text = "N/A"
		End If


Hi,

I wonder why it is not working if the Path is corrent.

To add to what modi123_1 said in the post earlier, Debug the code..try taking your mouse cursor over strPath & see what value it contains.

Check if the path is exactly where your ijji.lnk file is

Also..to access your local desktop..instead of writing:
Dim strPath As String = "C:\Users\" & strUsername & "\Desktop\ijji.lnk"

try writing:
Dim strPath As String = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\ijji.lnk"


hope this helps.


WORKS! thank you so much guys for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1