Click here to Skip to main content
15,879,239 members
Articles / Web Development / XHTML
Article

Powerful File manager

Rate me:
Please Sign up or sign in to vote.
4.13/5 (12 votes)
27 Jun 2008CPOL4 min read 55K   3.7K   65   7
A Powerful File manager in pure asp.net
Image 1

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

C#
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:

C#
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

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

Now create new Table row

C#
TableRow tr = new TableRow();

So, create new Table Cells for command button bar

C#
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

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

Now we must create new row for title of each cells

C#
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

C#
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

C#
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.

C#
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.

C#
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)


Written By
Austria Austria
I am Mohsen Ahmadian,
I was born in Tehran (capital city of Iran), I live in Vienna
I am a freelancer and a web developer
I Love Codeproject and C++, Java, C#, PHP
if you found my articles are good, buy a coffee for me
https://www.buymeacoffee.com/mohsenahmadian

Comments and Discussions

 
Questionسوال در مورد MVC Pin
HoseinQuest30-Jun-11 2:30
HoseinQuest30-Jun-11 2:30 
Questionlist file another site Pin
Hamid reza taebi29-Jun-11 23:19
Hamid reza taebi29-Jun-11 23:19 
AnswerRe: list file another site Pin
Mohsen Ahmadian4-Jul-11 8:34
Mohsen Ahmadian4-Jul-11 8:34 
GeneralMy vote of 5 Pin
mrhesy25-Nov-10 3:02
mrhesy25-Nov-10 3:02 
GeneralVery Nice Article Pin
vamshikt511-Aug-10 20:33
vamshikt511-Aug-10 20:33 
Generalnice librtary Pin
mahan11011011011-Jun-08 18:46
mahan11011011011-Jun-08 18:46 
GeneralRe: nice librtary Pin
Mohsen Ahmadian27-Jun-08 1:14
Mohsen Ahmadian27-Jun-08 1:14 

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

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