Tuesday, January 26, 2010

Delete File From The Server

dr1.Close();
String strQuery = “delete from tbl_test where id=’” + Request.Params.Get(“id”) + “‘”;
cmd1 = new SqlCommand(strQuery, conn);
cmd1.CommandType = CommandType.Text;
cmd1.Connection = conn;
cmd1.Parameters.AddWithValue(“state”, Request.Params.Get(“state”));
try
{
cmd1.ExecuteNonQuery();
cmd1.ExecuteReader();
DeleteFile(“File”);
}
finally
{
conn.Close();
lblmsg.Visible = true;
lblmsg.Text = “Selected Data is successfully deleted.”;
BindData();
}
private void DeleteFile(string strFileName1)
{//Delete file from the server
strFileName1 = filename1;
string strFileName2 = filename2;
string str1 = Server.MapPath(“../upload/image/”) + strFileName1;
string str2 = Server.MapPath(“../upload/image1/”) + strFileName2;
if (str1.Trim().Length > 0 && str2.Trim().Length > 0)
{
FileInfo fi1 = new FileInfo(str1);
FileInfo fi2 = new FileInfo(str2);
if (fi1.Exists && fi2.Exists)//if file exists delete it
{
fi1.Delete();
fi2.Delete();
}
}
}