1、就是列出服务器的应用程序目录列表吗?如果这样,可以:
/****************************************************************************
/ Scan the current folder, add all the child directories and files
/ For each item add the links to navigate the folder structure, the icons,
/ and other information such as the creation date, size etc.
/***************************************************************************/
private void FillFoldersAndFilesTable()
{
string location; DirectoryInfo parentDir;
DirectoryInfo[] childDirs;
FileInfo[] childFiles;
try
{
parentDir = new DirectoryInfo(Server.MapPath(folderPath));
// get all the child directories and files
childDirs = parentDir.GetDirectories();
childFiles = parentDir.GetFiles();
}
catch (Exception exc)
{
StatusMessage.Text = exc.Message;
StatusMessage.Visible = true;
return;
} TableRow rowItem;
TableCell cellItemIcon;
TableCell cellItemLink;
TableCell cellSize;
TableCell cellDownload;
TableCell cellAttributes;
TableCell cellOperations;
TableCell cellCreated;
TableCell cellLastModified;
HyperLink linkRename;
HyperLink linkItem;
HyperLink linkDownload;
CheckBox checkItem;
HyperLink linkSetAttribs;
Label labelAttributes;
System.Web.UI.WebControls.Image imgItemIcon; Style styleFolderRow = new Style();
styleFolderRow.CssClass = FolderStyle.Text;
Style styleFileRow = new Style();
styleFileRow.CssClass = FileStyle.Text;
Style styleLink = new Style();
styleLink.CssClass = "GridLink"; // if the current folder is not the site root, add the FolderUp icon and link
if (folderPath != Request.ApplicationPath.ToString())
{
rowItem = new TableRow();
cellItemIcon = new TableCell();
cellItemLink = new TableCell();
linkItem = new HyperLink();
imgItemIcon = new System.Web.UI.WebControls.Image();
// create the cell with the FolderUp icon
imgItemIcon.ImageUrl = "./Images/ParentFolder.gif";
cellItemIcon.Controls.Add(imgItemIcon);
cellItemIcon.HorizontalAlign = HorizontalAlign.Right;
// add the link that points to the parent directory
linkItem.Text = " ...";
int lastSlashIndex = folderPath.LastIndexOf("/");
location = folderPath.Substring(0,lastSlashIndex);
if (location.Length==0)
location = Request.ApplicationPath.ToString();
linkItem.NavigateUrl = "BrowseFiles.aspx?Folder=" + location;
linkItem.ApplyStyle(styleLink);
cellItemLink.Controls.Add(linkItem); // add the cells to the new row
rowItem.Cells.Add(cellItemIcon);
rowItem.Cells.Add(cellItemLink);
rowItem.Cells.Add(new TableCell());
rowItem.Cells.Add(new TableCell());
rowItem.Cells.Add(new TableCell());
rowItem.Cells.Add(new TableCell());
rowItem.Cells.Add(new TableCell());
rowItem.Cells.Add(new TableCell());
// add the row to the table
rowItem.ApplyStyle(styleFolderRow);
FoldersAndFiles.Rows.Add(rowItem);
} // add all the child directories first
foreach (DirectoryInfo childDir in childDirs)
{
rowItem = new TableRow();
cellItemLink = new TableCell();
linkItem = new HyperLink();
cellItemIcon = new TableCell();
checkItem = new CheckBox();
imgItemIcon = new System.Web.UI.WebControls.Image();
cellSize = new TableCell();
cellOperations = new TableCell();
linkRename = new HyperLink();
cellCreated = new TableCell();
cellLastModified = new TableCell();
cellAttributes = new TableCell();
labelAttributes = new Label();
linkSetAttribs = new HyperLink();
// create the link that points to this sub-directory
linkItem.Text = childDir.Name;
location = folderPath;
if (location.EndsWith("/"))
location += childDir.Name;
else
location += "/" + childDir.Name;
linkItem.NavigateUrl = "BrowseFiles.aspx?Folder=" + location;
linkItem.ApplyStyle(styleLink);
cellItemLink.Controls.Add(linkItem);
// set the cell that displays the item's size
cellSize.Text = FormatSize(GetDirectorySize(childDir.FullName)) + " ";
cellSize.HorizontalAlign = HorizontalAlign.Right; // set the created and last modified date
cellCreated.Text = String.Format("{0:MM/dd/yy hh:mm tt}", childDir.CreationTime);
cellLastModified.Text = String.Format("{0:MM/dd/yy hh:mm tt}", childDir.LastWriteTime);
/****************************************************************************
/ Scan the current folder, add all the child directories and files
/ For each item add the links to navigate the folder structure, the icons,
/ and other information such as the creation date, size etc.
/***************************************************************************/
private void FillFoldersAndFilesTable()
{
string location; DirectoryInfo parentDir;
DirectoryInfo[] childDirs;
FileInfo[] childFiles;
try
{
parentDir = new DirectoryInfo(Server.MapPath(folderPath));
// get all the child directories and files
childDirs = parentDir.GetDirectories();
childFiles = parentDir.GetFiles();
}
catch (Exception exc)
{
StatusMessage.Text = exc.Message;
StatusMessage.Visible = true;
return;
} TableRow rowItem;
TableCell cellItemIcon;
TableCell cellItemLink;
TableCell cellSize;
TableCell cellDownload;
TableCell cellAttributes;
TableCell cellOperations;
TableCell cellCreated;
TableCell cellLastModified;
HyperLink linkRename;
HyperLink linkItem;
HyperLink linkDownload;
CheckBox checkItem;
HyperLink linkSetAttribs;
Label labelAttributes;
System.Web.UI.WebControls.Image imgItemIcon; Style styleFolderRow = new Style();
styleFolderRow.CssClass = FolderStyle.Text;
Style styleFileRow = new Style();
styleFileRow.CssClass = FileStyle.Text;
Style styleLink = new Style();
styleLink.CssClass = "GridLink"; // if the current folder is not the site root, add the FolderUp icon and link
if (folderPath != Request.ApplicationPath.ToString())
{
rowItem = new TableRow();
cellItemIcon = new TableCell();
cellItemLink = new TableCell();
linkItem = new HyperLink();
imgItemIcon = new System.Web.UI.WebControls.Image();
// create the cell with the FolderUp icon
imgItemIcon.ImageUrl = "./Images/ParentFolder.gif";
cellItemIcon.Controls.Add(imgItemIcon);
cellItemIcon.HorizontalAlign = HorizontalAlign.Right;
// add the link that points to the parent directory
linkItem.Text = " ...";
int lastSlashIndex = folderPath.LastIndexOf("/");
location = folderPath.Substring(0,lastSlashIndex);
if (location.Length==0)
location = Request.ApplicationPath.ToString();
linkItem.NavigateUrl = "BrowseFiles.aspx?Folder=" + location;
linkItem.ApplyStyle(styleLink);
cellItemLink.Controls.Add(linkItem); // add the cells to the new row
rowItem.Cells.Add(cellItemIcon);
rowItem.Cells.Add(cellItemLink);
rowItem.Cells.Add(new TableCell());
rowItem.Cells.Add(new TableCell());
rowItem.Cells.Add(new TableCell());
rowItem.Cells.Add(new TableCell());
rowItem.Cells.Add(new TableCell());
rowItem.Cells.Add(new TableCell());
// add the row to the table
rowItem.ApplyStyle(styleFolderRow);
FoldersAndFiles.Rows.Add(rowItem);
} // add all the child directories first
foreach (DirectoryInfo childDir in childDirs)
{
rowItem = new TableRow();
cellItemLink = new TableCell();
linkItem = new HyperLink();
cellItemIcon = new TableCell();
checkItem = new CheckBox();
imgItemIcon = new System.Web.UI.WebControls.Image();
cellSize = new TableCell();
cellOperations = new TableCell();
linkRename = new HyperLink();
cellCreated = new TableCell();
cellLastModified = new TableCell();
cellAttributes = new TableCell();
labelAttributes = new Label();
linkSetAttribs = new HyperLink();
// create the link that points to this sub-directory
linkItem.Text = childDir.Name;
location = folderPath;
if (location.EndsWith("/"))
location += childDir.Name;
else
location += "/" + childDir.Name;
linkItem.NavigateUrl = "BrowseFiles.aspx?Folder=" + location;
linkItem.ApplyStyle(styleLink);
cellItemLink.Controls.Add(linkItem);
// set the cell that displays the item's size
cellSize.Text = FormatSize(GetDirectorySize(childDir.FullName)) + " ";
cellSize.HorizontalAlign = HorizontalAlign.Right; // set the created and last modified date
cellCreated.Text = String.Format("{0:MM/dd/yy hh:mm tt}", childDir.CreationTime);
cellLastModified.Text = String.Format("{0:MM/dd/yy hh:mm tt}", childDir.LastWriteTime);
解决方案 »
- 将Stream转为为Flash文件.怎么转换
- DataGrid EditCommandColumn问题
- 缓存优化问题
- 水晶报表的总页数显示问题!急!!!
- 急问:asp.net实现,利用Cache防止同一帐号重复登录的问题
- 怎样使用C#将unicode的编码转换为gb2312??
- 关于静态变量的问题,请教:
- 有关frameset嵌套的问题
- 问在DataGrid中如何控制列的颜色?
- 新手问:有没有类似Response.redirect("……")的命令能将此url定位在一个新页面中
- 为何我用"\w{1,20}"表达式后不能输入汉字?如何要想验证可能出现汉字的信息?在线等!
- 关于身份验证的问题,请各位高人指点(在线)
// add the checkbox, and store the directory path as an attribute
// in addition set an attribute that states that this item is not a file (this info
// will be used later by the procedures that copy/move the selected items)
checkItem.Attributes["Path"] = location;
checkItem.Attributes["IsFile"] = "false";
cellItemIcon.Controls.Add(checkItem); // add the icon with the closed folder
imgItemIcon.ImageUrl = "./Images/ClosedFolder.gif";
cellItemIcon.Controls.Add(imgItemIcon);
cellItemIcon.HorizontalAlign = HorizontalAlign.Right; // add the Rename link. The 'D' before the file location will be used to determine
// if the path identifies a file or a directory
linkRename.Text = "<img src=\"./Images/Rename.gif\" border=\"0\" height=\"16\" width=\"16\" Alt=\"更名\">";
linkRename.NavigateUrl = "javascript:Rename('D" + location + "');";
cellOperations.Controls.Add(linkRename); // set the attributes cell: the description label and the icon link to modify them
labelAttributes.Text = GetAttributesDescription(childDir.Attributes);
labelAttributes.Font.Name = "Courier";
linkSetAttribs.Text = "<img src=\"./Images/Edit.gif\" border=\"0\" height=\"16\" width=\"16\" Alt=\"编辑属性\">";
linkSetAttribs.NavigateUrl = "javascript:SetAttributes('" + location + "');";
cellAttributes.Controls.Add(labelAttributes);
cellAttributes.Controls.Add(linkSetAttribs); // add the cells to the new row
rowItem.Cells.Add(cellItemIcon);
rowItem.Cells.Add(cellItemLink);
rowItem.Cells.Add(new TableCell());
rowItem.Cells.Add(cellOperations);
rowItem.Cells.Add(cellAttributes);
rowItem.Cells.Add(cellSize);
rowItem.Cells.Add(cellCreated);
rowItem.Cells.Add(cellLastModified);
// add the new row to the table
rowItem.ApplyStyle(styleFolderRow);
FoldersAndFiles.Rows.Add(rowItem);
} // now add each child file
foreach (FileInfo childFile in childFiles)
{
rowItem = new TableRow();
cellItemLink = new TableCell();
linkItem = new HyperLink();
cellItemIcon = new TableCell();
checkItem = new CheckBox();
imgItemIcon = new System.Web.UI.WebControls.Image();
cellDownload = new TableCell();
linkDownload = new HyperLink();
cellSize = new TableCell();
cellOperations = new TableCell();
linkRename = new HyperLink();
cellCreated = new TableCell();
cellLastModified = new TableCell();
cellAttributes = new TableCell();
labelAttributes = new Label();
linkSetAttribs = new HyperLink();
int extIndex;
// create and add the link that points to this file, and open it in a new window
linkItem.Text = childFile.Name;
location = folderPath;
if (location.EndsWith("/"))
location += childFile.Name;
else
location += "/" + childFile.Name;
linkItem.NavigateUrl = location;
linkItem.Target = "_blank";
linkItem.ApplyStyle(styleLink);
cellItemLink.Controls.Add(linkItem); // add the link to download the file
linkDownload.Text = "<img src=\"./Images/Download.gif\" border=\"0\" height=\"16\" width=\"16\" Alt=\"下载\">";
linkDownload.NavigateUrl = "Download.aspx?File=" + location;
cellDownload.Controls.Add(linkDownload); // set the cell that displays the item's size
cellSize.Text = FormatSize(childFile.Length) + " ";
cellSize.HorizontalAlign = HorizontalAlign.Right;
// set the created and last modified date
cellCreated.Text = String.Format("{0:MM/dd/yy hh:mm tt}", childFile.CreationTime);
cellLastModified.Text = String.Format("{0:MM/dd/yy hh:mm tt}", childFile.LastWriteTime); // add the Rename link. The 'F' before the file location will be used to determine
// if the path identifies a file or a directory
linkRename.Text = "<img src=\"./Images/Rename.gif\" border=\"0\" height=\"16\" width=\"16\" Alt=\"更名\">";
linkRename.NavigateUrl = "javascript:Rename('F" + location + "');";
cellOperations.Controls.Add(linkRename);
// if this file is detected as a text file (basing on its extensions)
// and the file is not read only or a system file, add an image link to edit it
extIndex = Array.IndexOf(textExtensions, childFile.Extension.ToLower());
if (extIndex > -1 &&
(childFile.Attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly &&
(childFile.Attributes & FileAttributes.System) != FileAttributes.System)
{
HyperLink linkEditFile = new HyperLink();
linkEditFile.Text = "<img src=\"./Images/Write.gif\" height=\"16\" width=\"16\" border=\"0\" alt=\"编辑文件\">";
linkEditFile.NavigateUrl = "EditFile.aspx?File=" + location;
cellOperations.Controls.Add(linkEditFile);
} // search the extension in the array, and in case show the respective icon
// if not found add a general icon for unknown files
extIndex = Array.IndexOf(extensions, childFile.Extension.ToLower());
if (extIndex > -1)
imgItemIcon.ImageUrl = "./Images/" + childFile.Extension.Substring(1) + ".gif";
else
imgItemIcon.ImageUrl = "./Images/unknown.gif";
// set the checkbox and add the item's path and type (file) as
// it's done above for child directories
checkItem.Attributes["Path"] = location;
checkItem.Attributes["IsFile"] = "true";
cellItemIcon.Controls.Add(checkItem);
cellItemIcon.Controls.Add(imgItemIcon);
cellItemIcon.HorizontalAlign = HorizontalAlign.Right; // set the attributes cell: the description label and the icon link to modify them
labelAttributes.Text = GetAttributesDescription(childFile.Attributes);
labelAttributes.Font.Name = "Courier";
linkSetAttribs.Text = "<img src=\"./Images/Edit.gif\" border=\"0\" height=\"16\" width=\"16\" Alt=\"编辑属性\">";
linkSetAttribs.NavigateUrl = "javascript:SetAttributes('" + location + "');";
cellAttributes.Controls.Add(labelAttributes);
cellAttributes.Controls.Add(linkSetAttribs); // add the cells to the new row
rowItem.Cells.Add(cellItemIcon);
rowItem.Cells.Add(cellItemLink);
rowItem.Cells.Add(cellDownload);
rowItem.Cells.Add(cellOperations);
rowItem.Cells.Add(cellAttributes);
rowItem.Cells.Add(cellSize);
rowItem.Cells.Add(cellCreated);
rowItem.Cells.Add(cellLastModified); // add the new row to the table
rowItem.ApplyStyle(styleFileRow);
FoldersAndFiles.Rows.Add(rowItem);
}
}
<asp:Image id="Image" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageUrl") %>' Width="192" Visible='<%# DataBinder.Eval(Container.DataItem, "Visible") %>' >
</asp:Image>