Click here to Skip to main content
Click here to Skip to main content
Go to top

Powerful File manager

, 27 Jun 2008
Rate this:
Please Sign up or sign in to vote.
A Powerful File manager in pure asp.net

Introduction

In the web application some times you need to manage your files or allow your users to manage their files. In this time you need a file manager and creating a powerful solution is time consuming, but you don't have enough time. This was the story of my work when I needed it. I think if I write a powerful file manager that is simple to use and portable, did my best.

Requirements

First step of any project get my requirements I need a file manager has this features:

  • Functional
  1. Create new directory
  2. Create new html/text file
  3. Edit html/text files
  4. Upload file
  5. Upload and unzip files in to destination directory
  6. Create Archive in server (in zip format)
  7. Show list of Files/Directory
  8. Show Attribute of Files
  9. Delete files
  10. Delete Directory
  • nonfunctional
  1. Pure ASP.net (without any configuration)
  2. Skin able (change with CSS)
  3. User Friendly
  4. Security
  5. Easy to install

Using PFM (Power File Manager)

Using power file manager (in short PFM) is very simple to install. In first step copy files and directories in your web application then copy bin files to your bin directory next add link of PFM in your app.

Note:
  1. The default directory to manage in PFM is "other file" but you can change it in source code (root path).
  2. If you want to share the PFM with your Users for their directories you can manage root path with your app.

Now your app has one PFM.

Change Skin

To change skin of PFM you have many customizations

  1. Change CSS in file "PFMStyle.css"
  2. Change images in Softimages

Edit/Create Html File

PFM use MCE_tiny Editor to Create/Edit Html File. This is a powerful editor and you can change it with any Html Editor compatible with asp.net but this is not a limitation.

How to Manage Users to Use (Security)

This is simple on PFM because PFM has simple structure and you can control it by few changes in code for examples if you need change user permission to avoid deleting file, you can hide delete file in command line and so one.

This is very simple but PFM on this Article doesn't have any control on user and its sample of PFM. For making it to your interest you should change it.

Warning for multi users Use:

If you want to use it for multi users you have to change selection method. as a suggestion you can use cookie.

Create/Open Archive

To create or Open Archive I use "ICSharpCode.SharpZipLib" Library. This API is cool and is simple to work with ZIP Archive (Under GNU).

Code representation

Table Solution

My solution to show a list of files use an ASP Table, but if you know about asp table this is very simple .for this attention I wrote three classes to wrap and use easily

public class MyTableCell:TableCell //Describe new Table Cell  
public class MyLinkButton:Label  //Describe Link button (text + image)
public class MyImageButton:Label  //Describe Image button

Now all things are ready to create a list of files and directories:

        string p = Server.MapPath(startpath);
        if (!Directory.Exists(p))
        {
            MultiView1.SetActiveView(View2);
            return;
        }
        string[] fns = Directory.GetFiles(p);
        string[] dns = Directory.GetDirectories(p);
        string uplevel = startpath;
        if (uplevel.LastIndexOf("\\") > 0)
            uplevel = uplevel.Substring(0, uplevel.LastIndexOf("\\"));
        Table1.Rows.Clear();
        Table1.CssClass = "ms-list8-main";
        TableRow tr = new TableRow();
        MyTableCell head = new MyTableCell("ms-list8-top", new MyImageButton("softimages/folder_home.png", "Default.aspx?goto=listview", "Root")
            , new MyImageButton("softimages/back.png", "Default.aspx?goto=listview&start=" + uplevel, "Back")
            , new MyImageButton("softimages/top.png", "Default.aspx?goto=Upload&start=" + startpath, "Upload File")
            , new MyImageButton("softimages/newfolder.png", "Default.aspx?goto=newFolder&start=" + startpath, "New Folder")
            , new MyImageButton("softimages/mimetype/html.png", "Default.aspx?goto=addnew2&start=" + startpath, "New Text File")
            , new MyImageButton("softimages/ark.png", "Default.aspx?goto=zip&start=" + startpath, "Archive")
            , new MyImageButton("softimages/reloadark.png", "Default.aspx?goto=ReloadZip&start=" + startpath, "Clear Archive")            
            , new MyImageButton("softimages/delark.png", "Default.aspx?goto=DeletArch&start=" + startpath, "Delete Archive"));
        head.ColumnSpan = 5;
        tr.Cells.Add(head);
        Table1.Rows.Add(tr);
        tr = new TableRow();
        tr.Cells.AddRange(new TableCell[] {new MyTableCell("File Name","ms-list8-tl"),new MyTableCell("Size","ms-list8-top")
            ,new MyTableCell("Creation Time","ms-list8-top"),
         new MyTableCell("Last write time","ms-list8-top") ,new MyTableCell("Commands","ms-list8-top")});
        Table1.Rows.Add(tr);
        PrintList(dns, false);
        PrintList(fns, true);
        tr = new TableRow();
        MyTableCell mtc = new MyTableCell(" ", "ms-list8-bottom");
        mtc.ColumnSpan = 5;
        tr.Cells.Add(mtc);
        Table1.Rows.Add(tr);
        MultiView1.SetActiveView(View2);

At first we clear all table cells

        Table1.Rows.Clear();
        Table1.CssClass = "ms-list8-main";

Now create new Table row

TableRow tr = new TableRow();

So, create new Table Cells for command button bar

MyTableCell head = new MyTableCell("ms-list8-top", new MyImageButton("softimages/folder_home.png",
           "Default.aspx?goto=listview", "Root"), new MyImageButton("softimages/back.png",
           "Default.aspx?goto=listview&start=" + uplevel, "Back"),new MyImageButton("softimages/top.png",
           "Default.aspx?goto=Upload&start=" + startpath, "Upload File"),
           new MyImageButton("softimages/newfolder.png", "Default.aspx?goto=newFolder&start=" +
           startpath,"New Folder"), new MyImageButton("softimages/mimetype/html.png", 
           "Default.aspx?goto=addnew2&start=" + startpath, "New Text File")
            , new MyImageButton("softimages/ark.png", "Default.aspx?goto=zip&start=" + startpath, "Archive")
            , new MyImageButton("softimages/reloadark.png", "Default.aspx?goto=ReloadZip&start=" + 
             startpath, "Clear Archive"), new MyImageButton("softimages/delark.png", 
            "Default.aspx?goto=DeletArch&start=" + startpath, "Delete Archive"));
         head.ColumnSpan = 5;

Set column span to 5 to balance the table. After this add head to table row and table

   tr.Cells.Add(head);
   Table1.Rows.Add(tr);

Now we must create new row for title of each cells

tr = new TableRow();
        tr.Cells.AddRange(new TableCell[] {new MyTableCell("File Name","ms-list8-tl"),new
                           MyTableCell("Size","ms-list8-top"),new MyTableCell("Creation Time",
                                       "ms-list8-top"),new MyTableCell("Last write time","ms-list8-top") 
                          ,new MyTableCell("Commands","ms-list8-top")});
        Table1.Rows.Add(tr);

In this time we have Table with command bar and title bar and can add list of directory's and files

   PrintList(dns, false);  // dns is list of directories name 
   PrintList(fns, true);   // fns is list of files name

Print list is a procedure with two parameters to show list in table

   private void PrintList(string[] fns, bool isfiles)

Other source code of create list is very easy and don't need any more comment.

Multi view Solution

In this project we have many pages to create, delete, rename and etc…. On the other hand we must create project in one page because it must be portable, in this case my idea to solve this problem is using Multiview solution.
Multiview is an ASP.net tools to create many pages in one page.

Query Handling Solution

How to handle views or user commands, when we use multi view?
To answer this question we have two choices
Event handling Or Query Handling (QH)
Event Handling is not very flexible on the other hand QH is very flexible.
QH is an old, but very powerful solution.
So, answer is QH.

if (!IsPostBack)
        {
            string w = Request["goto"];
            if (w == null)
                ViewList();
            else
            {
                switch (w)
                {
                    case "addnew":
                        MultiView1.SetActiveView(View1);
                        Label4.Text = "Create Text File";                        
                        TextBox1.ReadOnly = false;
                        Button7.Visible = false;
                        break;
                    case "addnew2":
                        MultiView1.SetActiveView(View1);
                        Label4.Text = "Create Text File";                        
                        TextBox1.ReadOnly = false;
                        Button7.Visible = true;
                        break;
                    case "listview":
                        ViewList();
                        break;
                    case "edit":
                        editThisfile();
                        break;
                    case "ren":
                        renamethisfile();
                        break;
                    case "del":
                        deleteThisfile();
                        break;
                    case "info":
                        information();
                        break;
                    case "link":
                        showlink();
                        break;
                    case "newFolder":
                        MultiView1.SetActiveView(View7);
                        break;
                    case "Upload":
                        MultiView1.SetActiveView(View8);
                        break;
                    case "zip":
                        CreateZip();
                        break;
                    case "AddZip":
                        AddtoZip();
                        break;
                    case "RemoveZip":
                        RemoveZip();
                        break;
                    case "ReloadZip":
                        ReloadZip();
                        break;
                    case "DeletArch":
                        DeletArch();
                        break;
                    default:
                        break;
                }
            }
        }

Monitoring Errors

If you have a great project you will have huge errors! To solve this problem you must monitoring your errors and save them when users work with your application. Dr. Watson was a person who wrote any events. So now we have a class with this title.
DrWatson (our class) just writes thrown exceptions in a text file called "DrWatson.txt" with date/time and source of throw.

catch (Exception ex)
        {
            DrWatson.SaveError(ex);
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

mohsen ahmadian

Iran (Islamic Republic Of) Iran (Islamic Republic Of)
I am mohsen ahmadian
I live in kermanshah(city of iran)
I Love Codeproject and C++,java,C#

Comments and Discussions

 
Generalnice librtary Pinmembermahan11011011011-Jun-08 18:46 
GeneralRe: nice librtary Pinmembermohsen ahmadian27-Jun-08 1:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 27 Jun 2008
Article Copyright 2008 by mohsen ahmadian
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid