| i developing the web site for institute.my web site have the following directory and this folder stores the audios and many file extension file in diffrent directories.like Server.MapPath("~/maintainer/uploaded_files/Uploaded/gallery/audios/") and Server.MapPath("~/maintainer/uploaded_files/Uploaded/syllabus/") i want to delete the specific file when deleting the record from gridview.when i deleting the the record in gridview,the record is deleted but file not deleting file from directory.i using the following source code and c#/asp code in code behind aspx.cs file |
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" CommandArgument='<%#Eval("filename") %>' Text="Delete">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
and in aspx.cs file
public int deleted;
public Boolean flag = false;
String filename;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//String capath = (sender as LinkButton).CommandArgument;
if (!IsPostBack)
{
try
{
if (e.CommandName == "Delete")
{
Label id = (Label)GridView1.FindControl("Label1");
//String dirpath = "~/maintainer/uploaded_files/Uploaded/gallery/audios/";
String dirpath = Server.MapPath("~//maintainer//uploaded_files//Uploaded//gallery//audios//");
SqlDataSource1.DeleteParameters["id"].DefaultValue = id.Text;
DirectoryInfo di = new DirectoryInfo(dirpath);
FileInfo[] fiarr = di.GetFiles();
foreach (FileInfo fi in fiarr)
{
if (fi.Name == e.CommandArgument.ToString())
{
fi.Delete();
flag = true;
}
else
{
flag = false;
}
}
//if (System.IO.File.Exists(Server.MapPath(e.CommandArgument.ToString())))
//{
// System.IO.File.Delete(Server.MapPath(e.CommandArgument.ToString()));
// flag = true;
// int deleted = SqlDataSource1.Delete();
//}
//else
//{ flag = false; }
if (flag == true)
{
deleted = SqlDataSource1.Delete();
if (deleted > 0)
{
Response.Write("<script>alert('Audio File Deleted Successfully')</script>");
Response.Redirect(Request.Url.AbsoluteUri);
}
}
else
{
Response.Write("<script>alert('Audio File Not Deleted')</script>");
}
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
thanks.