软讯网络 > 编程语言 > .NET > C#.NET > shawl.qiu c# .net FileManager class v1.0(文件管理器)
【标 题】:shawl.qiu c# .net FileManager class v1.0(文件管理器)
【关键字】:
shawl.qiu,c#,.net,FileManager,class,v1.0
【来 源】:http://blog.csdn.net/btbtd/archive/2007/02/25/1513610.aspx
shawl.qiu c# .net FileManager class v1.0(文件管理器)
shawl.qiu c# .net FileManager class v1.0(文件管理器)
目录:
1. 简介
2. 核心代码
3. 页面代码
shawl.qiu
2007-02-25
http://blog.csdn.net/btbtd1. 简介
- CREATED BY STABX, AT 2007-2-24.
- shawl.qiu c# .net FileManager class(文件管理器)
- ---/-------------------------------------------------------------
- version 1.0
- 默认用户名: shawl.qiu
- 默认密码: aaaaaa
- 下载:
- http://files.myopera.com/btbtd/csharp/class/sq_csharp_FileManager_class_v1.0.7z
- 重要提示:
- 站点 web.config SessionState 应该设置为 SQLServer 模式, 如下面的设置:
- <configuration>
- <!--
- <appSettings>
- <add key="sq" value="just a test" />
- </appSettings>-->
- <!--Response.Write(ConfigurationSettings.AppSettings["sq"]); -->
-
- <system.web>
- <httpRuntime maxRequestLength="20480"
- useFullyQualifiedRedirectUrl="true"
- executionTimeout="90"
- />
-
- <!-- <customErrors defaultRedirect="/include/error/generalError.html"
- mode="RemoteOnly" >
- <error statusCode="404" redirect="/include/error/error404.html" />
- </customErrors>-->
-
- <globalization
- requestEncoding="UTF-8"
- responseEncoding="UTF-8"
- fileEncoding="UTF-8"
- />
- <sessionState mode="SQLServer"
- cookieless="true"
- timeout="20"
- sqlConnectionString="data source=127.0.0.1;user id=sqAspDotNetSession;password=AspDotNetSessionPwd"
- >
- </sessionState>
- </system.web>
-
- </configuration>
- 或者设置为允许 子目录的 web.config 设置, 在子目录放置上述内容的 web.config
- 原因为:
- 使用 FileObj.Move() 或 FileInfoObj.MoveTo() 时, 会导致页面重新编译, 从而丢失 Session.
- 目前我只找到 Session 的 SQLServer 选项才能解决该问题.
- 功能摘要:
- 支持 创建 新文件/新目录
-
- 支持编辑文本文件
-
- 支持上传文件
-
- 支持 移动 文件/目录
-
- 支持 删除 文件/目录
-
- 支持 重命名 文件/目录
-
- 支持 下载文件
-
- 支持 验证/不验证(该选项主要为整合程序而设置) 使用权限
-
- 支持自定义管理目录
-
- 支持编辑用户信息(当选择验证选项时)
- 写本程序的动机:
- 主要为整合到我的 sqGallery 画廊程序,
- 我想为我的画廊程序 增加文件编辑功能,
- 不过一想可能使代码非常混乱,
- 就打定主意写一个通用性高的文件管理器,
- 唉, 那知道,
- 这是一个无底坑, 写了这么一个杂乱无章的文件管理器,
- 不过功能还是挺齐全的.
-
- 后续版本主要为 简化/优化 代码.
- © 2007-2008, shawl.qiu All rights reserved.
- author: shawl.qiu
- e-mail: shawl.qiu@gmail.com
- blog: http://blog.csdn.net/btbtd
- 2007-2-25 0:26:15
2. 核心代码
- using System;
- using System.Collections;
- using System.Data;
- using System.IO;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- public delegate void GeneralEventDelegate(Object s, EventArgs e);
- //public delegate void OneArgStringDelegate(String Str);
- /*-----------------------------------------------------------------------------------*\
- * shawl.qiu c# .net FileManager class v1.0
- \*-----------------------------------------------------------------------------------*/
- //---------------------------------------------------------------------begin class FileManager
- public class FileManager
- {
- //-----------------------------------begin event
- public FileManager()
- {
- }
-
- ~FileManager()
- {
- }
- //-----------------------------------end event
-
- //-----------------------------------begin public constant
- //-----------------------begin about
- public const String auSubject = "shawl.qiu c# .net FileManager class";
- public const String auVersion = "v1.0";
- public const String au = "shawl.qiu";
- public const String auEmail = "shawl.qiu@gmail.com";
- public const String auBlog = "http://blog.csdn.net/btbtd";
- public const String auCreateDate = "2007-2-19";
- //-----------------------end about
- //-----------------------------------end public constant
-
- //-----------------------------------begin public variable
- public bool Debug = false;
- public bool Verify = true;
-
- public string ManageRoot = "/test/";
- public string ProgramRoot = "/sqFlMng/";
-
- public string LoginSession = "sqFlMngLogin";
- public string CheckCodeSession = "sqFlMngCheckCode";
- public string FolderListSession = "flMngVsFolderList";
- public string FileListSession = "flMngVsFileList";
-
- public string LoginGotoUrl = "?";
- public string LogoutGotoUrl = "?";
-
- public string EditableExtension = ".txt|.asp|.aspx|.ascx|.html|.htm|.xml|.xsl|.xslt|.xsd|"+
- ".dtd|s.css|.vbs|.js|.svg|.c|.rss|.wml|.w3d";
-
- public string UploadExtension = "7z,rar,zip,mp3,bmp,gif,jpg,jpeg,png,txt,swf";
- public int UploadTotalKb = 5120;
- public int UploadSingleKb = 1024;
-
- public PlaceHolder MainPlaceHolder;
- public PlaceHolder FolderPlaceHolder;
- public PlaceHolder FilePlaceHolder;
- public PlaceHolder EditBoxPlaceHolder;
-
- public Label NavigatorLabel;
- public Label DebugLabel;
- public Label InfoLabel;
- public Label FooterLabel;
-
- public DataList FolderDataList;
- public DataList FileDataList;
-
- public GeneralEventDelegate MoveFileInFolderDelegate;
- public GeneralEventDelegate MoveFileToRootDelegate;
- public GeneralEventDelegate MoveFileToParentDelegate;
- //-----------------------------------end public variable
-
- //-----------------------------------begin private variable
- private string XmlFilePath = "";
- private string CurrentPath = "";
- private string ManageRoot_Phs = "";
- private string SiteRoot = "";
-
- private string FilePathSession = "flMngFileCurPath";
- private string FolderPathSession = "flMngCurPath";
- //-----------------------------------end private variable
-
- //-----------------------------------begin public method
- public void Go()
- {
- if(MainPlaceHolder==null)return;
-
- if(InfoLabel!=null)InfoLabel.Text="";
- if(DebugLabel!=null)DebugLabel.Text="";
- if(NavigatorLabel!=null)NavigatorLabel.Text="";
-
- if(!ProgramRoot.EndsWith("/")) ProgramRoot+="/";
- XmlFilePath = HttpContext.Current.Server.MapPath(ProgramRoot+"user.xml");
-
- string reqPath = HttpContext.Current.Request.QueryString["path"]+"";
- reqPath = HttpContext.Current.Server.UrlDecode(reqPath);
- if(reqPath==""|reqPath.IndexOf(ManageRoot)!=0)
- {
- CurrentPath = HttpContext.Current.Server.MapPath(ManageRoot);
- }
- else
- {
- CurrentPath = HttpContext.Current.Server.MapPath(reqPath);
- }
- ManageRoot_Phs = HttpContext.Current.Server.MapPath(ManageRoot);
- SiteRoot = HttpContext.Current.Server.MapPath("/");
-
- if(Debug&DebugLabel!=null)
- {
- DebugLabel.Text+="\n<li/>SiteRoot: "+SiteRoot;
- DebugLabel.Text+="\n<li/>ManageRoot_Phs: "+ManageRoot_Phs;
- DebugLabel.Text+="\n<li/>CurrentPath: "+CurrentPath;
- DebugLabel.Text+="\n<hr/>";
- } // end if 1
-
- Navigator(reqPath);
-
- if(Verify)
- {
- bool login = CheckPermission();
-
- if(Debug&DebugLabel!=null)
- {
- DebugLabel.Text+="<li/>LoginSession: "+login;
- DebugLabel.Text+="<hr/>";
- }
- switch(HttpContext.Current.Request.QueryString["id"]+"")
- {
- case "logout":
- HttpContext.Current.Session.Abandon();
- GoBack("已登出系统 ", 3, LogoutGotoUrl, InfoLabel);
- return;
- case "editinfo":
- EditBox(MainPlaceHolder, new EventHandler(EditInfo));
- return;
- }
-
- if(Debug&DebugLabel!=null)
- {
- DebugLabel.Text+="\n<li/>XmlFilePath: "+XmlFilePath;
- DebugLabel.Text+="\n<li/>是否已登陆: "+login;
- DebugLabel.Text+="\n<li/>Session[\"sqFlMngLogin\"]: "+
- HttpContext.Current.Session["sqFlMngLogin"];
- DebugLabel.Text+="\n<hr/>";
- } // end if 1
-
- if(login)
- {
- Main();
- }
- else
- {
- LoginBox(MainPlaceHolder, new EventHandler(CheckLogin));
- return;
- }
- }
- else
- {
- Main();
- }// end if
- Footer();
- } // end Go;
- //-----------------------------------end public method
-
- //-----------------------------------begin private method
-
- private void EditInfo(Object s, EventArgs e)
- {
- String Username=HttpContext.Current.Request.Form["Username"]+"";
- String Password=HttpContext.Current.Request.Form["Password"]+"";
- String RePassword=HttpContext.Current.Request.Form["RePassword"]+"";
- String CheckCode=HttpContext.Current.Request.Form["CheckCode"]+"";
- String CheckCodeSession=HttpContext.Current.Session["sqFlMngCheckCode"]+"";
-
- if(Debug&&DebugLabel!=null)
- {
- DebugLabel.Text+="<li/>"+Username;
- DebugLabel.Text+="<li/>"+Password;
- DebugLabel.Text+="<li/>"+CheckCode;
- DebugLabel.Text+="<li/>"+CheckCodeSession;
- DebugLabel.Text+="<hr/>";
- }
- if(CheckCode!=CheckCodeSession)
- {
- Message("验证码错误!", InfoLabel);
- goto Error;
- }
-
- if(Username=="")
- {
- Message("用户名不能为空!", InfoLabel);
- goto Error;
- }
-
- Finished:;
-
- string RootPath=HttpContext.Current.Server.MapPath(ProgramRoot);
- if(!RootPath.EndsWith("\\"))RootPath+="\\";
-
- String FilePath=RootPath+"user.xml";
- DataSet dsDataSet=new DataSet();
- DataTable dtConfig=new DataTable();
-
- dsDataSet.ReadXml(FilePath);
- dtConfig=dsDataSet.Tables["Admin"];
-
- dtConfig.Rows[0]["Username"]=Username;
-
- if(Password!="")
- {
- if(Password!=RePassword)
- {
- Message("密码与确认密码不相同, 操作被终止!", InfoLabel);
- return;
- }
- dtConfig.Rows[0]["Password"]=md5(Password);
- }
-
- dsDataSet.WriteXml(FilePath);
-
- dsDataSet.Dispose();
- dtConfig.Dispose();
-
- HttpContext.Current.Session.Abandon();
- GoBack("数据已更新, 请重新登陆, 3 秒后返回, 还有 ", 3, "?", InfoLabel);
-
- goto End;
-
- Error:;
-
- goto End;
-
- End:;
- }
-
- private void Message(String word, Label InfoLabel)
- {
- if(InfoLabel==null)return;
- InfoLabel.Text="<div style=\"display:table;width:100%;background-color:yellow!important;"+
- "color:black!important;text-align:center!important;\">"+
- word+"</div>";
- }
-
- private void EditBox(PlaceHolder EditBoxPh, EventHandler SubmitEventHandler)
- {
- if(EditBoxPh==null)goto End;
-
- string RootPath=HttpContext.Current.Server.MapPath(ProgramRoot);
- if(!RootPath.EndsWith("\\"))RootPath+="\\";
- DataSet dsDataSet=new DataSet();
- DataTable dtConfig=new DataTable();
-
- dsDataSet.ReadXml(RootPath+"user.xml");
- dtConfig=dsDataSet.Tables["Admin"];
-
-
- Literal Br=new Literal();
- Br.Text="<br/>";
-
- Literal Reset=new Literal();
- Reset.Text=
- " <input type=\"reset\" value=\"reset\" onclick=\"return confirm('现在重置吗?');\" />";
-
- Literal ltUsername=new Literal();
- ltUsername.Text="username: ";
-
- TextBox Username=new TextBox();
- Username.ID="Username";
- Username.Text=dtConfig.Rows[0]["Username"]+"";
-
- Literal ltPassword=new Literal();
- ltPassword.Text="<br/>password: ";
-
- TextBox Password=new TextBox();
- Password.ID="Password";
- Password.TextMode=TextBoxMode.Password;
-
- Literal ltRePassword=new Literal();
- ltRePassword.Text="<br/>re-password: ";
-
- TextBox RePassword=new TextBox();
- RePassword.ID="RePassword";
- RePassword.TextMode=TextBoxMode.Password;
-
- Literal ltPublished=new Literal();
- ltPublished.Text="(不更改密码请保留为空)<br/>Published: ";
- Literal ltCheckCode=new Literal();
- ltCheckCode.Text="<br/>CheckCode: ";
-
- TextBox CheckCode=new TextBox();
- CheckCode.ID="CheckCode";
- CheckCode.Columns=4;
-
- Button smtButton=new Button();
- smtButton.ID="glySubmit";
- smtButton.Text="Edit Now";
- smtButton.Click += SubmitEventHandler;
- smtButton.Attributes["onclick"]="javascript:return confirm('请确认更改?')";
-
- System.Web.UI.WebControls.Image CheckCodeImage=new System.Web.UI.WebControls.Image();
- CheckCodeImage.ImageUrl="cs/checkcode.aspx";
-
- EditBoxPh.Controls.Add(ltUsername);
- EditBoxPh.Controls.Add(Username);
- EditBoxPh.Controls.Add(ltPassword);
- EditBoxPh.Controls.Add(Password);
- EditBoxPh.Controls.Add(ltRePassword);
- EditBoxPh.Controls.Add(RePassword);
- EditBoxPh.Controls.Add(ltCheckCode);
- EditBoxPh.Controls.Add(CheckCode);
- EditBoxPh.Controls.Add(CheckCodeImage);
- EditBoxPh.Controls.Add(Br);
- EditBoxPh.Controls.Add(smtButton);
- EditBoxPh.Controls.Add(Reset);
-
- dtConfig.Dispose();
- dsDataSet.Dispose();
-
- End:;
- }
-
- private void Footer()
- {
- if(FooterLabel==null)return;
-
- FooterLabel.Text=auSubject+" "+auVersion+", Powered by "+au+".";
- FooterLabel.Text+="<br/>© 2007-2008, "+au+" All rights reserved.";
- }
-
- private void Main()
- {
- GetFolderBar(FolderPlaceHolder);
-
- switch(HttpContext.Current.Request.QueryString["id"]+"")
- {
- case "upload":
- Upload();
- return;
- case "editfile":
- EditFileBox(EditBoxPlaceHolder);
- return;
- }
-
- ListDirectorys(CurrentPath, ManageRoot_Phs, FolderDataList);
- ListFile(CurrentPath, ManageRoot_Phs, FileDataList);
- GetFileBar(FilePlaceHolder);
- }
-
- private void EditFileBox(PlaceHolder ph)
- {
- if(ph==null)return;
-
- bool Debug =false;
-
- string filePath = HttpContext.Current.Request.QueryString["filePath"]+"";
- string fileName = Path.GetFileName(filePath);
- string fileExt = Path.GetExtension(fileName);
- string finalPath = HttpContext.Current.Server.MapPath(filePath);
-
- if(Debug&DebugLabel!=null)
- {
- DebugLabel.Text += "<li/>Edit File:";
- DebugLabel.Text += "<li/>filePath: "+filePath;
- DebugLabel.Text += "<li/>fileName: "+fileName;
- DebugLabel.Text += "<li/>fileExt: "+fileExt;
- DebugLabel.Text += "<li/>finalPath: "+finalPath;
- DebugLabel.Text += "<li/>CheckPathIn(filePath, ManageRoot, true): "+
- CheckPathIn(filePath, ManageRoot, true);
- DebugLabel.Text += "<li/>CheckExtension(EditableExtension, fileExt): "+
- CheckExtension(EditableExtension, fileExt);
- }
-
- if(filePath=="")
- {
- GoBack("文件名不能为空, 3 秒后返回, 还有 ", 3, true, InfoLabel);
- return;
- }
-
- if(!CheckPathIn(filePath, ManageRoot, true))
- {
- GoBack("越权, 操作被终止, 3 秒后返回, 还有 ", 3, true, InfoLabel);
- return;
- }
-
- if(fileName.IndexOf(".")<0)
- {
- GoBack("文件名必须有后缀, 3 秒后返回, 还有 ", 3, true, InfoLabel);
- return;
- }
-
- if(!CheckExtension(EditableExtension, fileExt))
- {
- GoBack("不允许编辑的文件类型, 3 秒后返回, 还有 ", 3, true, InfoLabel);
- return;
- }
-
- if(!File.Exists(finalPath))
- {
- GoBack("文件不存在, 操作被终止, 3 秒后返回, 还有 ", 3, false, InfoLabel);
- return;
- }
-
- HtmlInputHidden hih = new HtmlInputHidden();
- hih.Value = finalPath;
- hih.ID = "flMngEditFilePath";
- ph.Controls.Add(hih);
-
- Literal ltl;
-
- ltl = new Literal();
- ltl.Text = "<li/>正在编辑文件: <b>"+filePath+"</b><br/>";
- ph.Controls.Add(ltl);
-
- TextBox tbx = new TextBox();
- tbx.TextMode = TextBoxMode.MultiLine;
- tbx.Columns = 60;
- tbx.Rows = 20;
- tbx.Text = ReadTextFile(finalPath);
- tbx.ID = "flMngEditFileContent";
- ph.Controls.Add(tbx);
-
- ltl = new Literal();
- ltl.Text = "<br/>";
- ph.Controls.Add(ltl);
-
- Button btn = new Button();
- btn.Text = "Submit";
- btn.Attributes["onclick"] = "javascript: return confirm('确定更改?')";
- btn.Click += new EventHandler(EditFileChange);
- ph.Controls.Add(btn);
-
- ltl = new Literal();
- ltl.Text = " <input type='reset' value='reset' onclick=\"return confirm('是否重置?');\"";
- ph.Controls.Add(ltl);
-
- } // end private void EditFileBox
-
- private void EditFileChange(Object s, EventArgs e)
- {
- bool Debug=false;
- string finalPath = HttpContext.Current.Request.Form["flMngEditFilePath"]+"";
- string fileContent = HttpContext.Current.Request.Form["flMngEditFileContent"]+"";
-
- if(Debug&InfoLabel!=null)
- {
- InfoLabel.Text+="<li/>finalPath: "+finalPath;
- InfoLabel.Text+="<li/>finalPath: "+fileContent;
- }
-
- WriteTextFile(finalPath, fileContent);
-
- GoBack("文件更新完毕, 3 秒后返回, 还有 ", 3, true, InfoLabel);
- return;
- }
-
- private void WriteTextFile(string fileName, string content)
- {
- using(StreamWriter sw = new StreamWriter(fileName))
- {
- sw.Write(content);
- }
- } // end private void WriteTextFile
-
- private string ReadTextFile(string filePath)
- {
- if(!File.Exists(filePath))return "false";
- string fileString="";
- using(StreamReader sr = new StreamReader(filePath))
- {
- fileString = sr.ReadToEnd();
- }
- return fileString;
- } // end private string ReadTextFile
-
- private bool CheckExtension(string extList, string extForCheck)
- {
- return Regex.IsMatch(
- extList,
- @extForCheck.Replace(".","\\.")+"\\b",
- RegexOptions.IgnoreCase
- );
- } // end private bool CheckExtension
-
- private bool CheckPathIn(string pathIn, string pathForCheck)
- {
- if(pathIn.IndexOf(pathForCheck)<0)return false;
- return true;
- }
-
- private bool CheckPathIn(string pathIn, string pathForCheck, bool CovPath)
- {
- if(CovPath)
- {
- pathIn = HttpContext.Current.Server.MapPath(pathIn);
- pathForCheck = HttpContext.Current.Server.MapPath(pathForCheck);
- }
-
- return CheckPathIn(pathIn, pathForCheck);
- }
-
- private void GetFileBar(PlaceHolder ph)
- {
- if(ph==null)return;
-
- Literal ltr = new Literal();
- ltr.Text="<h3>文件操作:</h3>";
- ph.Controls.Add(ltr);
-
- LinkButton lbtn;
-
- lbtn = new LinkButton();
- lbtn.Text = "MoveTo Root | ";
- lbtn.Attributes["onclick"] = "javascript:return confirm('确定要移动文件到根目录吗?');";
- if(MoveFileToRootDelegate!=null)
- lbtn.Click+=new EventHandler(MoveFileToRootDelegate);
- ph.Controls.Add(lbtn);
-
- lbtn = new LinkButton();
- lbtn.Text = "MoveTo Parent | ";
- lbtn.Attributes["onclick"] = "javascript:return confirm('确定要移动文件到父目录吗?');";
- if(MoveFileToParentDelegate!=null)
- lbtn.Click+=new EventHandler(MoveFileToParentDelegate);
- ph.Controls.Add(lbtn);
-
- lbtn = new LinkButton();
- lbtn.Text = "Move File In: ";
- lbtn.Attributes["onclick"] = "javascript:return confirm('确定要移动文件吗?');";
- if(MoveFileInFolderDelegate!=null)
- lbtn.Click+=new EventHandler(MoveFileInFolderDelegate);
- ph.Controls.Add(lbtn);
-
- }
-
- private void GetFolderBar(PlaceHolder ph)
- {
- if(ph==null)return;
- Literal ltl;
-
- LinkButton lbtn = new LinkButton();
- lbtn.Text="New Directory";
- lbtn.Click += new EventHandler(CreateNewDirectory);
- lbtn.Attributes["onclick"] = "return confirm('现在创建新文件夹吗?')";
- ph.Controls.Add(lbtn);
-
- ltl = new Literal();
- ltl.Text=" | ";
- ph.Controls.Add(ltl);
-
- LinkButton lbtnNewFile = new LinkButton();
- lbtnNewFile.Text="New File(txtfile)";
- lbtnNewFile.Click += new EventHandler(CreateNewFile);
- lbtnNewFile.Attributes["onclick"] = "return confirm('现在创建新文件吗?')";
- ph.Controls.Add(lbtnNewFile);
-
- ltl = new Literal();
- ltl.Text=": ";
- ph.Controls.Add(ltl);
-
- TextBox tbx = new TextBox();
- tbx.ID = "flMngCreateNewFolderInCurrent";
- ph.Controls.Add(tbx);
-
- string QueryUrl="";
- AutoConvertPagedUrl("id", out QueryUrl);
- ltl = new Literal();
- ltl.Text = " <a href='"+QueryUrl+"upload'>upload</a>";
-
- ph.Controls.Add(ltl);
- }
-
- private void ListFile(string path, string pathForCheck, DataList dl)
- {
- if(path.IndexOf(pathForCheck)<0|dl==null)return;
-
- DirectoryInfo di = new DirectoryInfo(path);
- FileInfo[] fiArray = di.GetFiles();
-
- int TotalFile = fiArray.Length;
-
- if(TotalFile==0)
- {
- InfoLabel.Text+="<h2 class='algc'>当前目录没有文件.</h2>";
- HttpContext.Current.Session[FileListSession] = null;
- return;
- }
-
- if(Debug&DebugLabel!=null)
- {
- DebugLabel.Text+="<li/>TotalFiles: "+TotalFile;
-
- DebugLabel.Text+="<hr/>";
- }
-
- if(HttpContext.Current.Session[FileListSession]==null|
- HttpContext.Current.Session[FilePathSession]+""!=path)
- {
- DataTable dt = new DataTable();
- DataRow dr;
-
- CreateDataColumn(dt, "FileName", typeof(string));
- CreateDataColumn(dt, "FileRelativePath", typeof(string));
- CreateDataColumn(dt, "FilePhysicalPath", typeof(string));
-
- for(int i=0, j=fiArray.Length; i<j; i++)
- {
- string FilePath = fiArray[i].FullName+"";
- ConvertPathToRelative(ref FilePath);;
-
- dr = dt.NewRow();
- dr[0]=fiArray[i].Name;
- dr[1]=FilePath;
- dr[2]=fiArray[i].FullName;
- dt.Rows.Add(dr);
- }
- dl.DataSource = dt.DefaultView;
- dl.DataBind();
- HttpContext.Current.Session[FileListSession] = dt;
- HttpContext.Current.Session[FilePathSession] = path;
- }
- else
- {
- HttpContext.Current.Session[FilePathSession] = path;
- }
- } // end private void ListFile
-
- private void ListDirectorys(string path, string pathForCheck, DataList dl)
- {
- if(path.IndexOf(pathForCheck)<0|dl==null)return;
-
- if(HttpContext.Current.Session[FolderListSession]==null|
- HttpContext.Current.Session[FolderPathSession]+""!=path)
- {
- if(Debug&DebugLabel!=null)
- {
- DebugLabel.Text+="<li/>Session[\"FolderPathSession\"]: "+
- HttpContext.Current.Session[FolderPathSession];
-
- DebugLabel.Text+="<hr/>";
- }
-
- DirectoryInfo di = new DirectoryInfo(path);
- DirectoryInfo[] diArray = di.GetDirectories();
-
- if(diArray.Length==0)
- {
- InfoLabel.Text+="<h2 class='algc'>当前目录没有子目录.</h2>";
- HttpContext.Current.Session[FolderListSession] = null;
- //return;
- }
-
- DataTable dt = new DataTable();
- DataRow dr;
-
- CreateDataColumn(dt, "FolderName", typeof(string));
- CreateDataColumn(dt, "FolderRelativePath", typeof(string));
- CreateDataColumn(dt, "FolderPhysicalPath", typeof(string));
-
- for(int i=0, j=diArray.Length; i<j; i++)
- {
- string FolderPath = diArray[i].FullName+"";
- ConvertPathToRelative(ref FolderPath, true);
-
- dr = dt.NewRow();
- dr[0]=diArray[i].Name;
- dr[1]=FolderPath;
- dr[2]=diArray[i].FullName;
- dt.Rows.Add(dr);
- }
- dl.DataSource = dt.DefaultView;
- dl.DataBind();
- HttpContext.Current.Session[FolderListSession] = dt;
- HttpContext.Current.Session[FolderPathSession] = path;
- }
- else
- {
- HttpContext.Current.Session[FolderPathSession] = path;
- }
- }
-
- private void Upload()
- {
- if(EditBoxPlaceHolder==null)return;
- string QueryUrl="";
- AutoConvertPagedUrl("id", out QueryUrl);
- string uploadPath = HttpContext.Current.Request.QueryString["path"]+"";
- if(uploadPath=="")uploadPath = ManageRoot;
- upload up=new upload();
- up.ext=UploadExtension;
- up.debug=false;
- up.item=5; // 声明显示多少个上传框
- up.path=uploadPath; // 上传目录;
- up.goback_url=QueryUrl+"upload"; // 上传完毕后返回的 URL
- up.goback_second=5; // 上传后返回间隔
- up.interval=5; // 每次上传间隔, 单位秒
- up.autorename=true; // 自动重命名;
-
- up.totalsize = UploadTotalKb;
- up.singlesize = UploadSingleKb;
-
- // 声明 处理 所有 返回路径 的方法
- //up.ReturnFilePath=new OneArgStringDelegate(GetPath);
-
- up.UploadPh=EditBoxPlaceHolder; // 声明显示上传控件的占位符
- up.UploadBox(); // 显示上传操作控件
-
- up=null;
- }
-
- private void AutoConvertPagedUrl(String QueryId, out String QueryUrl)
- {
- QueryUrl=System.Web.HttpContext.Current.Request.Url+"";
-
- if(QueryUrl.IndexOf(@"?")==-1)
- {
- QueryUrl+=@"?";
- }
- QueryUrl=Regex.Replace(QueryUrl,@"\b"+@QueryId+@"\=[^&]+","", RegexOptions.IgnoreCase);
- QueryUrl+="&"+QueryId+"=";
- QueryUrl=Regex.Replace(QueryUrl,@"\?\&",@"?", RegexOptions.IgnoreCase);
- QueryUrl=Regex.Replace(QueryUrl,@"\&+",@"&", RegexOptions.IgnoreCase);
-
- string sReferrer = System.Web.HttpContext.Current.Request.UrlReferrer+"";
- if(sReferrer.IndexOf("(")>-1&&sReferrer.IndexOf(")")>-1)
- {
- sReferrer = Regex.Replace(sReferrer, @".*(\(.*\)).*", "$1");
- QueryUrl = Regex.Replace(
- QueryUrl, @"(http\:\/\/.*?\/)", "$1"+sReferrer+"/", RegexOptions.IgnoreCase);
- }
- } // end private void AutoConvertPagedUrl
- //-----------------------------------end private method
-
- private void CreateNewFile(Object s, EventArgs e)
- {
- Control ctlPh = FolderPlaceHolder.FindControl("flMngCreateNewFolderInCurrent");
- if(ctlPh!=null)
- {
- TextBox tbx = (TextBox)ctlPh;
- if(tbx.Text!="")
- {
- string finalFileName = CurrentPath+tbx.Text;
-
- if(Debug&DebugLabel!=null)
- {
- DebugLabel.Text+="<li/>new file string:"+tbx.Text;
- DebugLabel.Text+="<li/>finalFileName:"+finalFileName;
-
- DebugLabel.Text+="<hr/>";
- }
-
- if(tbx.Text.IndexOf(".")<0)
- {
- GoBack("新文件必须有扩展名, 操作被取消, 3 秒反返回, 还有 ", 3,
- HttpContext.Current.Request.UrlReferrer+"", InfoLabel);
- return;
- }
-
- if(File.Exists(finalFileName))
- {
- GoBack("文件已存在, 操作被取消, 3 秒反返回, 还有 ", 3,
- HttpContext.Current.Request.UrlReferrer+"", InfoLabel);
- return;
- }
- else
- {
- using(StreamWriter sw = new StreamWriter(finalFileName))
- {
-
- }
- HttpContext.Current.Session[FileListSession] = null;
- GoBack("新文件已创建, 3 秒反返回, 还有 ", 3,
- HttpContext.Current.Request.UrlReferrer+"", InfoLabel);
- return;
- }
- }
- }
- }
-
- private void CreateNewDirectory(Object s, EventArgs e)
- {
- Control ctlPh = FolderPlaceHolder.FindControl("flMngCreateNewFolderInCurrent");
- if(ctlPh!=null)
- {
- TextBox tbx = (TextBox)ctlPh;
- if(tbx.Text!="")
- {
- string newFolder = CurrentPath+tbx.Text;
- if(!Directory.Exists(newFolder))
- {
- Directory.CreateDirectory(newFolder);
- HttpContext.Current.Session[FolderListSession] = null;
- GoBack("新文件夹已创建, 3 秒反返回, 还有 ", 3,
- HttpContext.Current.Request.UrlReferrer+"", InfoLabel);
- return;
- }
- else
- {
- GoBack("文件夹已存在, 操作被取消, 3 秒反返回, 还有 ", 3,
- HttpContext.Current.Request.UrlReferrer+"", InfoLabel);
- return;
- }
- }
- }
- }
-
- private StringBuilder GetPath(String PathQuery, String LinkString)
- {
- PathQuery=System.Web.HttpContext.Current.Server.UrlDecode(PathQuery);
- if(!PathQuery.EndsWith("/"))PathQuery+="/";
- StringBuilder sb=new StringBuilder();
- String[] PathTemp=PathQuery.Split('/');
- for(Int32 i=0, j=PathTemp.Length; i<j; i++)
- {
- if(PathTemp[i]!="")
- {
- sb.Append(" -> ");
- if(i==j-2)
- {
- sb.Append("[");
- }
- sb.Append("<a href=\""+LinkString);
- sb.Append("/");
- for(Int32 k=0; k<=i; k++)
- {
- if(PathTemp[k]!="")
- {
- sb.Append(System.Web.HttpContext.Current.Server.UrlEncode(PathTemp[k]));
- sb.Append("/");
- }
- }
- sb.Append("\">");
- sb.Append(PathTemp[i]);
- sb.Append("</a>");
- if(i==j-2)
- {
- sb.Append("]");
- }
- }
- }
- return sb;
- } // end private StringBuilder GetPath
-
- protected void CreateDataColumn(DataTable dt, String fieldName, Type tp)
- {
- dt.Columns.Add(new DataColumn(fieldName, tp));
- }
-
- protected void CreateDataColumn(DataTable dt, String fieldName)
- {
- dt.Columns.Add(new DataColumn(fieldName, typeof(int)));
- }
-
- private void ConvertPathToRelative(ref string FolderPath)
- {
- string SiteRoot= HttpContext.Current.Server.MapPath("/");
-
- FolderPath = FolderPath.Replace(SiteRoot, "").Replace(@"\", "/");
- if(!FolderPath.StartsWith("//"))FolderPath="/"+FolderPath;
- FolderPath = HttpContext.Current.Server.UrlPathEncode(FolderPath);
- }
-
- private void ConvertPathToRelative(ref string FolderPath, bool plusSlash)
- {
- ConvertPathToRelative(ref FolderPath);
-
- if(plusSlash)
- if(!FolderPath.EndsWith("//"))FolderPath+="/";
- }
-
- private void Navigator(string curPath)
- {
- if(NavigatorLabel==null)return;
-
- if(Debug&DebugLabel!=null)
- {
- DebugLabel.Text+="\n<li/>NavigatorLable ok. ";
- DebugLabel.Text+="\n<hr/>";
- }
-
- NavigatorLabel.Text+="\n<div class='algr flMngNavigator'>\n";
-
- bool login = CheckPermission();
-
- if(!Verify|login)
- {
- bool mode=false;
- if(login) mode=true;
-
- NavigatorMethod(curPath, mode);
- }
-
- NavigatorLabel.Text+="\n</div>";
- } // end public void Navigator
-
- private void NavigatorMethod(string curPath, bool loginMode)
- {
- NavigatorLabel.Text+=
- "<div class='fltl algl'>"+
- "<a href='?path="+ManageRoot+"'>sqFlMng Root</a> "+
- GetPath(curPath, "?path=")+
- "</div> ";
-
- if(loginMode)
- {
- NavigatorLabel.Text+=" <a href='?id=editinfo'>EditInfo</a>";
- NavigatorLabel.Text+=" <a href='?id=logout'>logout</a>";
- }
- }
-
- private bool CheckPermission()
- {
- DataTable dt = ReadDataSetTable(XmlFilePath, "Admin");
- if(dt==null)
- return false;
-
- string login = HttpContext.Current.Session[LoginSession]+"";
-
- if(login=="") return false;
- return true;
- } // end private bool CheckPermission
-
- private DataTable ReadDataSetTable(string filePath, string tableName)
- {
- DataSet ds = new DataSet();
- ds.ReadXml(filePath);
- return ds.Tables[tableName];
- } // end private DataTable ReadDataSetTable
-
- private void LoginBox(PlaceHolder EditBoxPh, EventHandler SubmitEventHandler)
- {
- if(EditBoxPh==null)goto End;
-
- Literal Br=new Literal();
- Br.Text="<br/>";
-
- Literal ltUsername=new Literal();
- ltUsername.Text="Username: ";
-
- Literal ltPassword=new Literal();
- ltPassword.Text="<br/>Password: ";
-
- Literal ltCheckCode=new Literal();
- ltCheckCode.Text="<br/>CheckCode: ";
-
- TextBox Username=new TextBox();
- Username.ID="Username";
-
- TextBox CheckCode=new TextBox();
- CheckCode.ID="CheckCode";
- CheckCode.Columns=4;
-
- TextBox Password=new TextBox();
- Password.TextMode=TextBoxMode.Password;
- Password.ID="Password";
-
- Button smtButton=