.NET Forum / Languages / Visual J# / May 2005
How to create a Directory listing?
|
|
Thread rating:  |
Christian-Josef Schrattenthaler - 19 May 2005 16:36 GMT Hi!
I want to build a dynamic Download-Page:
If the userer goes to updates.aspx, the ASP-Pages should look for files in a special directory, and then list them with a hyperlink to make a download.
And I need to read some Information from the filename of the files:
Filename: "2005-05-19_2.9a.zip".
This means: Datum: "2005-05-19" Version: "2.9a" Plattform: "Windows"
I looked for information on Internet, but I didn't find anything useful. Can anyone thell me how I can do this with VJ# under ASP.NET, or can anyone thell me some documentation for getting info about?
Kind greetings, christian.
Lars-Inge Tønnessen [VJ# MVP] - 19 May 2005 21:04 GMT Hi Christian,
Here is an example I have made for you. The example uses a table to display the content of a folder on the web server. You will find code to extract the info you wanted in the code too. I have also added a cool switching background color in the dir listing.
The files are stored in the directory: "C:\Inetpub\wwwroot\Files" on the Web Server.
This is the "WebForm1.aspx.jsl" file:
package JDirListing;
import System.Collections.*; import System.ComponentModel.*; import System.Data.*; import System.Drawing.*; import System.Web.*; import System.Web.SessionState.*; import System.Web.UI.*; import System.Web.UI.WebControls.*; import System.Web.UI.HtmlControls.*;
/** * Summary description for WebForm1. */ public class WebForm1 extends System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Table Table2;
private void Page_Load(Object sender, System.EventArgs e) { // Put user code to initialize the page here this.ListDir( ); }
#region Web Form Designer generated code protected void OnInit(System.EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. Donot remove this. // InitializeComponent(); super.OnInit(e); }
/** * Required method for Designer support - do not modify * the contents of this method with the code editor. */ private void InitializeComponent() { this.add_Load( new System.EventHandler(this.Page_Load) );
} #endregion
private void ListDir() { // Get the dir info. You should not hardcode this line into the source, but instead // use a key-value entry in the web.config xml file. System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo( "C:\\Inetpub\\wwwroot\\Files" ); // All files in the directory. System.IO.FileInfo[] files = dirInfo.GetFiles();
// Header info. System.Web.UI.WebControls.TableRow headerRow = new System.Web.UI.WebControls.TableRow(); this.Table2.get_Rows().Add( headerRow );
// Download System.Web.UI.WebControls.TableCell tc1 = new System.Web.UI.WebControls.TableCell(); tc1.set_Text( "Get the file" ); tc1.set_BackColor( System.Drawing.Color.get_Blue() ); tc1.set_ForeColor( System.Drawing.Color.get_White() ); headerRow.get_Cells().Add( tc1 );
// Filename System.Web.UI.WebControls.TableCell tc2 = new System.Web.UI.WebControls.TableCell(); tc2.set_Text( "Filename" ); tc2.set_BackColor( System.Drawing.Color.get_Blue() ); tc2.set_ForeColor( System.Drawing.Color.get_White() ); headerRow.get_Cells().Add( tc2 );
// Size System.Web.UI.WebControls.TableCell tc3 = new System.Web.UI.WebControls.TableCell(); tc3.set_Text( "Size in Kb" ); tc3.set_BackColor( System.Drawing.Color.get_Blue() ); tc3.set_ForeColor( System.Drawing.Color.get_White() ); headerRow.get_Cells().Add( tc3 );
// Date System.Web.UI.WebControls.TableCell tc4 = new System.Web.UI.WebControls.TableCell(); tc4.set_Text( "Date" ); tc4.set_BackColor( System.Drawing.Color.get_Blue() ); tc4.set_ForeColor( System.Drawing.Color.get_White() ); headerRow.get_Cells().Add( tc4 );
// Version System.Web.UI.WebControls.TableCell tc5 = new System.Web.UI.WebControls.TableCell(); tc5.set_Text( "Version" ); tc5.set_BackColor( System.Drawing.Color.get_Blue() ); tc5.set_ForeColor( System.Drawing.Color.get_White() ); headerRow.get_Cells().Add( tc5 );
// Platform System.Web.UI.WebControls.TableCell tc6 = new System.Web.UI.WebControls.TableCell(); tc6.set_Text( "Platform" ); tc6.set_BackColor( System.Drawing.Color.get_Blue() ); tc6.set_ForeColor( System.Drawing.Color.get_White() ); headerRow.get_Cells().Add( tc6 );
// List the files. boolean bgColorSwitch = false; for ( int counter = 0; counter < files.length; counter++ ) { // Set the bakground color to a cool switching effect. System.Drawing.Color bgColor = System.Drawing.Color.get_White(); if ( bgColorSwitch == false ) { bgColor = System.Drawing.Color.get_White(); bgColorSwitch = true; } else { bgColor = System.Drawing.Color.get_LightBlue(); bgColorSwitch = false; }
// Make a new table row and add it to the screen. System.Web.UI.WebControls.TableRow newRow = new System.Web.UI.WebControls.TableRow(); this.Table2.get_Rows().Add( newRow );
// Download link System.Web.UI.WebControls.TableCell link = new System.Web.UI.WebControls.TableCell();
// You should not hardcode this link into the source, but insted use a key-value in the web.config xml file link.set_Text( "<a href=\"http://localhost/Files/" + files[counter].get_Name() + "\">Click here to download</a>" ); link.set_BackColor( bgColor ); newRow.get_Cells().Add( link );
// Filename System.Web.UI.WebControls.TableCell fname = new System.Web.UI.WebControls.TableCell(); fname.set_Text( files[counter].get_Name() ); fname.set_BackColor( bgColor ); newRow.get_Cells().Add( fname );
// Size System.Web.UI.WebControls.TableCell fsize = new System.Web.UI.WebControls.TableCell(); fsize.set_Text( "" + (files[counter].get_Length() / 1024) + " Kb" ); fsize.set_BackColor( bgColor ); newRow.get_Cells().Add( fsize );
// Date System.Web.UI.WebControls.TableCell date = new System.Web.UI.WebControls.TableCell(); date.set_Text( files[counter].get_Name().Split(new char[]{'_'})[0] ); date.set_BackColor( bgColor ); newRow.get_Cells().Add( date );
// Version System.Web.UI.WebControls.TableCell version = new System.Web.UI.WebControls.TableCell(); String version_string = files[counter].get_Name().Split(new char[]{'_'})[1]; version.set_Text( version_string.Remove(version_string.lastIndexOf("."), 4) ); version.set_BackColor( bgColor ); newRow.get_Cells().Add( version );
// Platform System.Web.UI.WebControls.TableCell Platform = new System.Web.UI.WebControls.TableCell(); Platform.set_Text( "Windows" ); Platform.set_BackColor( bgColor ); newRow.get_Cells().Add( Platform ); } } }
This is the "WebForm1.aspx" file in HTML-view in Visual Studio.
<%@ Page language="VJ#" Codebehind="WebForm1.aspx.jsl" AutoEventWireup="false" Inherits="JDirListing.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>Dir listing J#</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="VJ#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body> <form id="Form1" method="post" runat="server"> <TABLE id="Table1" cellSpacing="0" cellPadding="0" width="100%" border="0"> <TR> <TD> <asp:Label id="Label1" runat="server" Font-Size="Large">Directory listing with J#</asp:Label></TD> </TR> <TR> <TD> </TD> </TR> <TR> <TD> <asp:Table id="Table2" runat="server"></asp:Table></TD> </TR> <TR> <TD></TD> </TR> </TABLE> </form> </body> </HTML>
Any questions about the code is welcome. :o)
Best Regards, Lars-Inge Tønnessen
Christian-Josef Schrattenthaler - 23 May 2005 09:00 GMT Hi Lars-Inge!
Thank you for your help!
I am not a programmer, but I am going to leran Java (Sun Java, not Microsoft Java). I did only the first steps, so I needed a long time to understand some things, also the ASP.NET system. Now I know a little bit more, but only a little bit...
I have the ASP.NET Web Matrix programm from Microsoft to create ASP.NET programms, and Eclipse to create Java programms. I have not the Microsoft Visual Studio tools.
I think, I understand your splitting system. There is an *.aspx file and an *.aspx.jsl file. The aspx.jsl is a compiled VJ#.NET file. and the aspx file uses the code of the compiled aspx.jsl file.
But I have no Microsoft Visual Studio tool, so I can't create jsl files. Is it possible, to use Eclipse files insted of VJ# files?
But there is another question: why do you split the files?
Kind greetings, christian.
Lars-Inge Tønnessen [VJ# MVP] - 24 May 2005 18:18 GMT > I have the ASP.NET Web Matrix programm from Microsoft You should get Visual Studio if you want to do any serious ASP.NET programming. Web Matrix is just a "toy" compared to VS. If you are a student you can buy a student fiendly priced version. Or you can download the express edition if your a home enthusiast or a student. The student version of Visual Studio is in reality the "Professional" edition.
http://lab.msdn.microsoft.com/express/vwd/default.aspx
> and an *.aspx.jsl file. The aspx.jsl is a compiled VJ#.NET file. Yes, a dll library.
> But there is another question: why do you split the files? Visual Studio did that to the files. An other very important thing is that I don't have to ship the source code to a customer. I don't have to show the "competition" how I made the site. It's more difficult to edit my site without me knowing it.
Here is the example written without Visual Studio
The "default.aspx" file.
<%@ Page language="VJ#" %>
<Script Runat="Server">
private void Page_Load(Object sender, System.EventArgs e) { if ( !this.get_IsPostBack() ) { this.Table2.get_Rows().Clear(); this.ListDir( ); } }
protected void OnInit(System.EventArgs e) { InitializeComponent(); super.OnInit(e); }
private void InitializeComponent() { this.add_Load( new System.EventHandler(this.Page_Load) );
}
private void ListDir() { // Get the dir info. You should not hardcode this line into the source, but instead // use a key-value entry in the web.config xml file. System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo( "C:\\Inetpub\\wwwroot\\Files" ); // All files in the directory. System.IO.FileInfo[] files = dirInfo.GetFiles();
// Header info. System.Web.UI.WebControls.TableRow headerRow = new System.Web.UI.WebControls.TableRow(); this.Table2.get_Rows().Add( headerRow );
// Download System.Web.UI.WebControls.TableCell tc1 = new System.Web.UI.WebControls.TableCell(); tc1.set_Text( "Get the file" ); tc1.set_BackColor( System.Drawing.Color.get_Blue() ); tc1.set_ForeColor( System.Drawing.Color.get_White() ); headerRow.get_Cells().Add( tc1 );
// Filename System.Web.UI.WebControls.TableCell tc2 = new System.Web.UI.WebControls.TableCell(); tc2.set_Text( "Filename" ); tc2.set_BackColor( System.Drawing.Color.get_Blue() ); tc2.set_ForeColor( System.Drawing.Color.get_White() ); headerRow.get_Cells().Add( tc2 );
// Size System.Web.UI.WebControls.TableCell tc3 = new System.Web.UI.WebControls.TableCell(); tc3.set_Text( "Size in Kb" ); tc3.set_BackColor( System.Drawing.Color.get_Blue() ); tc3.set_ForeColor( System.Drawing.Color.get_White() ); headerRow.get_Cells().Add( tc3 );
// Date System.Web.UI.WebControls.TableCell tc4 = new System.Web.UI.WebControls.TableCell(); tc4.set_Text( "Date" ); tc4.set_BackColor( System.Drawing.Color.get_Blue() ); tc4.set_ForeColor( System.Drawing.Color.get_White() ); headerRow.get_Cells().Add( tc4 );
// Version System.Web.UI.WebControls.TableCell tc5 = new System.Web.UI.WebControls.TableCell(); tc5.set_Text( "Version" ); tc5.set_BackColor( System.Drawing.Color.get_Blue() ); tc5.set_ForeColor( System.Drawing.Color.get_White() ); headerRow.get_Cells().Add( tc5 );
// Platform System.Web.UI.WebControls.TableCell tc6 = new System.Web.UI.WebControls.TableCell(); tc6.set_Text( "Platform" ); tc6.set_BackColor( System.Drawing.Color.get_Blue() ); tc6.set_ForeColor( System.Drawing.Color.get_White() ); headerRow.get_Cells().Add( tc6 );
// List the files. boolean bgColorSwitch = false; for ( int counter = 0; counter < files.length; counter++ ) { // Set the bakground color to a cool switching effect. System.Drawing.Color bgColor = System.Drawing.Color.get_White(); if ( bgColorSwitch == false ) { bgColor = System.Drawing.Color.get_White(); bgColorSwitch = true; } else { bgColor = System.Drawing.Color.get_LightBlue(); bgColorSwitch = false; }
// Make a new table row and add it to the screen. System.Web.UI.WebControls.TableRow newRow = new System.Web.UI.WebControls.TableRow(); this.Table2.get_Rows().Add( newRow );
// Download link System.Web.UI.WebControls.TableCell link = new System.Web.UI.WebControls.TableCell();
link.set_Text( "<a href=\"http://localhost/Files/" + files[counter].get_Name() +
"\">Click here to download</a>" ); link.set_BackColor( bgColor ); newRow.get_Cells().Add( link );
// Filename System.Web.UI.WebControls.TableCell fname = new System.Web.UI.WebControls.TableCell(); fname.set_Text( files[counter].get_Name() ); fname.set_BackColor( bgColor ); newRow.get_Cells().Add( fname );
// Size System.Web.UI.WebControls.TableCell fsize = new System.Web.UI.WebControls.TableCell(); fsize.set_Text( "" + (files[counter].get_Length() / 1024) + " Kb" ); fsize.set_BackColor( bgColor ); newRow.get_Cells().Add( fsize );
// Date System.Web.UI.WebControls.TableCell date = new System.Web.UI.WebControls.TableCell(); date.set_Text( files[counter].get_Name().Split(new char[]{'_'})[0] ); date.set_BackColor( bgColor ); newRow.get_Cells().Add( date );
// Version System.Web.UI.WebControls.TableCell version = new System.Web.UI.WebControls.TableCell(); String version_string = files[counter].get_Name().Split(new char[]{'_'})[1]; version.set_Text( version_string.Remove(version_string.lastIndexOf("."), 4) ); version.set_BackColor( bgColor ); newRow.get_Cells().Add( version );
// Platform System.Web.UI.WebControls.TableCell Platform = new
System.Web.UI.WebControls.TableCell(); Platform.set_Text( "Windows" ); Platform.set_BackColor( bgColor ); newRow.get_Cells().Add( Platform ); } }
</Script>
<HTML> <HEAD> <title>Dir listing J#</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="VJ#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body> <form id="Form1" method="post" runat="server"> <TABLE id="Table1" cellSpacing="0" cellPadding="0" width="100%" border="0"> <TR> <TD> <asp:Label id="Label1" runat="server"
Font-Size="Large">Directory listing with J#</asp:Label></TD> </TR> <TR> <TD> </TD> </TR> <TR> <TD> <asp:Table id="Table2" runat="server"></asp:Table></TD> </TR> <TR> <TD></TD> </TR> </TABLE> </form> </body> </HTML>
Regards, Lars-Inge Tønnessen
Christian-Josef Schrattenthaler - 24 May 2005 19:50 GMT Hi Lars-Inge!
> You should get Visual Studio if you want to do any serious ASP.NET > programming. Web Matrix is just a "toy" compared to VS. If you are a > student you can buy a student fiendly priced version. Or you can download > the express edition if your a home enthusiast or a student. The student > version of Visual Studio is in reality the "Professional" edition. > http://lab.msdn.microsoft.com/express/vwd/default.aspx I think I have a Visual Studio 2003 Enterprise Architect Version. But I uninstalled it from my Computer, because I want to learn Java, and so I only need Eclipse and the JDK, which are both free. Because I get the information, thtat the VJ.NET tool from Micsosoft is nocht 100% kompatible with the true Java from Sun. And - if if learnd programming in Java - should create programms which runs on every plattform, this means the original Java standard.
> Here is the example written without Visual Studio Ups, thank you, but I did this allready, and I modyfied it a little bit, and it works greatly. Thank you for your help!
Kind greetings, christian.
Lars-Inge Tønnessen [VJ# MVP] - 24 May 2005 20:41 GMT Hi Christian,
> I think I have a Visual Studio 2003 Enterprise Architect Version. But I > uninstalled it from my Computer, because I want to learn Java, You don't have to uninstall Visual Studio to write or compile Sun Java applications.
> Because I get the information, thtat the VJ.NET tool from Micsosoft is > nocht 100% kompatible with the true Java from Sun. It should compile most code written for JDK 1.1.4 and equivalent to the level of many of the classes in the JDK 1.2 java.util package. Do you have any code that will not compile with J#, that you think should?
(J# will not compile Java bytecode to run in Java virtual machines.)
Regards, Lars-Inge Tønnessen
Christian-Josef Schrattenthaler - 25 May 2005 08:39 GMT Hi Lars-Inge!
> You don't have to uninstall Visual Studio to write or compile Sun > Java applications. I know. But I thought, why use 2 or 3 GB of my harddisk for nothing...
> It should compile most code written for JDK 1.1.4 and equivalent to > the level of many of the classes in the JDK 1.2 java.util package. Do > you have any code that will not compile with J#, that you think > should? > > (J# will not compile Java bytecode to run in Java virtual machines.) I didn't try VJ#.NET because of the information. And you see, there is no update for the actual JDK, and you wrote VJ# can not compible bytecode for JVM. So I think, there is no sense to use it...
Now I am downloading the beta2 of Web Developer... And then I take a look.
Question: I don't understand the VJ# philosophy. Why a Microsoft-Java and not a tool for programming the original Sun-Java (Standard). Why do YOU use VJ#?
Greetings, christian.
Christian-Josef Schrattenthaler - 25 May 2005 15:38 GMT Hi Lars-Inge!
I downloaded and installed Visual Web Developer 2005 Beta2. And you are right, It's better than the ASP.NET Web Matrix!
I changed my complete System to VWD and deleted all other tools, excluded Eclipse and JDK (I need them for learning Java).
Harm thath MS doesn't really support it's VJ#.NET! If you look for any support, any code, or any samples... there is nothing. Also in den MSDN are so many examples for VB.NET and C#.NET, but for VJ# there is nothing :-(
But I have an other problem. I use the VJ# tool of the VWD, but it does not create two files? There is also only one aspx file? What must I do to get two files? One for the HTML and one for the VJ# code.
Greetings, Christian.
Lars-Inge Tønnessen [VJ# MVP] - 26 May 2005 21:30 GMT You can find J# samples here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vjsample/htm l/vjsamtechnologysamples.asp
For the 2005 (express) beta edition, samples can be found on this page:
http://msdn2.microsoft.com/library/0c109h88(en-us,vs.80).aspx
> What must I do to get two files? One for the HTML and one for the VJ# > code. You should get two files. It's called "code behind". Open a completely new web project. Drag a new button on to the screen, double click the button. You should now get an empty button handler method in the J# source file.
The compiled code goes into the "\bin\yourlibrary.dll" file.
Regards, Lars-Inge Tønnessen
Christian-Josef Schrattenthaler - 27 May 2005 23:08 GMT Hi Lasr-Inge!
> You should get two files. It's called "code behind". Open a completely new > web project. Drag a new button on to the screen, double click the button. > You should now get an empty button handler method in the J# source file. > > The compiled code goes into the "\bin\yourlibrary.dll" file. This doesn't work. I get only one file! I tried your sample manually, but I got an error. If I did correctly understand the documentation, I have to use"CodeFile" and not "Codebehind"? And I have to "precompile" the file? But I dont knwo where I can do this.
Greetings, christian.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|