How to delete file from folder in vb.net ?

Ranjeet_8
Posted by Ranjeet_8 under VB.NET category on | Points: 40 | Views : 1295
when the user click on button then if the file exists then it will be deleted.
// HTML

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Delete" onclick="Button1_Click" />
</div>
</form>
</body>
</html>


// VB Code

Imports System.IO
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim TheFile As New FileInfo((MapPath(".") & "\Your Folder Location\") + TextBox1.Text & ".Doc")
If TheFile.Exists Then
File.Delete((MapPath(".") & "\Your Folder Location\") + TextBox1.Text & ".Doc")
End If
End Sub

Comments or Responses

Login to post response