Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 20th, 2006, 10:04 AM
Authorized User
 
Join Date: Mar 2005
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default Deleting files using a listbox control

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.";
            }
        }
    }
  #2 (permalink)  
Old April 20th, 2006, 10:43 AM
Authorized User
 
Join Date: Mar 2005
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got it working with this code

Code:
            string deletefile = Request.Form["dirList"];
            Response.Write(deletefile);

            if (deletefile != "")
            {
                deletefile = Server.MapPath("./HtmlStore/") + deletefile;
                File.Delete(deletefile);
                this.dirList.Items.Remove(deletefile);
            }
            else
            {
                lblError.Visible = true;
                lblError.Text = "No file was selected to delete.";
            }
            BindData();


However I only noticed that it was deleted by looking in Widnows Explorer. The items on the list itself does not disappear.



Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting selected row in ListBox YoungFools Excel VBA 1 April 19th, 2007 09:14 AM
Creating, Writing to and Deleting Text Files goels Access VBA 7 January 30th, 2007 11:51 AM
Listbox of files mreider C# 0 February 23rd, 2006 07:13 AM
deleting files as session expires jaisonkmathews General .NET 4 October 18th, 2004 12:07 AM
Deleting directories and files ... DennisTh VB.NET 2002/2003 Basics 5 June 12th, 2003 02:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.