I am trying to create a small file manager system for a Church group. Files are stored in a html directory called "htmlStore". I am able to upload files however when I try to delete a file from the filelist displayed in a listbox nothing happens. I have checked permissions and that is as it should be. Any help would be appreciated.
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
namespace CIS
{
/// <summary>
/// Summary description for fileMan.
/// </summary>
public class fileMan : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnUpload;
protected System.Web.UI.HtmlControls.HtmlInputFile MyFile;
protected System.Web.UI.WebControls.ListBox dirList;
protected System.Web.UI.WebControls.Button bnDelete;
protected System.Web.UI.WebControls.Label lblText;
protected System.Web.UI.WebControls.Label lblError;
protected System.Web.UI.WebControls.Label lblMessage;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
DirectoryInfo htmldir = new DirectoryInfo(ConfigurationSettings.AppSettings["HTMLStore"].Trim());
FileInfo [] files = htmldir.GetFiles();
foreach (Object item in files)
{
dirList.Items.Add(item.ToString());
}
dirList.SelectedIndex = 0;
//bnAddItem.Attributes.Add("OnClick","return btnModify_OnClick()");
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
this.bnDelete.Click += new System.EventHandler(this.bnDelete_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void btnUpload_Click(object sender, System.EventArgs e)
{
string strFileName = MyFile.PostedFile.FileName;
strFileName = System.IO.Path.GetFileName(strFileName);
MyFile.PostedFile.SaveAs(Server.MapPath("./HtmlStore/") + strFileName);
lblMessage.Text = "Your file: " + strFileName + " has been uploaded successfully !";
}
private void bnDelete_Click(object sender, System.EventArgs e)
{
string deletefile = Request.Form["dirList"];
Response.Write(deletefile);
if (deletefile != "")
{
this.dirList.Items.Remove(Server.MapPath("./HtmlStore/") + deletefile);
}
else
{
lblError.Visible = true;
lblError.Text = "No file was selected to delete.";
}
}
}