asp_net的文件夹操作
- 格式:doc
- 大小:64.50 KB
- 文档页数:8
创建⽂件夹using System.IO;using System.Web;namespace SEC{/**/////// 对⽂件和⽂件夹的操作类///public class FileControl{public FileControl(){}/**/////// 在根⽬录下创建⽂件夹////// 要创建的⽂件路径public void CreateFolder(string FolderPathName){if(FolderPathName.Trim().Length> 0){try{string CreatePath = System.Web.HttpContext.Current.Server.MapPath("/../Images/"+FolderPathName).ToString();if(!Directory.Exists(CreatePath)){Directory.CreateDirectory(CreatePath);}}catch{throw;}}}/**/////// 删除⼀个⽂件夹下⾯的字⽂件夹和⽂件//////public void DeleteChildFolder(string FolderPathName){if(FolderPathName.Trim().Length> 0){try{string CreatePath = System.Web.HttpContext.Current.Server.MapPath (FolderPathName).ToString();if(Directory.Exists(CreatePath)){Directory.Delete(CreatePath,true);}}catch{throw;}}}/**/////// 删除⼀个⽂件//////public void DeleteFile(string FilePathName){try{FileInfo DeleFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath (FilePathName).ToString());DeleFile.Delete();}catch{}}public void CreateFile(string FilePathName){try{//创建⽂件夹string[] strPath= FilePathName.Split('/');CreateFolder(FilePathName.Replace("/" + strPath[strPath.Length-1].ToString(),"")); //创建⽂件夹FileInfo CreateFile =new FileInfo(System.Web.HttpContext.Current.Server.MapPath (FilePathName).ToString()); //创建⽂件if(!CreateFile.Exists){FileStream FS=CreateFile.Create();FS.Close();}}catch{}}/**/////// 删除整个⽂件夹及其字⽂件夹和⽂件//////public void DeleParentFolder(string FolderPathName){try{DirectoryInfo DelFolder = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath (FolderPathName).ToString());if(DelFolder.Exists){DelFolder.Delete();}}catch{}}/**/////// 在⽂件⾥追加内容//////public void ReWriteReadinnerText(string FilePathName,string WriteWord){try{//建⽴⽂件夹和⽂件//CreateFolder(FilePathName);CreateFile(FilePathName);//得到原来⽂件的内容FileStream FileRead=new FileStream(System.Web.HttpContext.Current.Server.MapPath (FilePathName).ToString(),FileMode.Open,FileAccess.ReadWrite);StreamReader FileReadWord=new StreamReader(FileRead,System.Text.Encoding.Default); string OldString = FileReadWord.ReadToEnd().ToString();OldString = OldString + WriteWord;//把新的内容重新写⼊StreamWriter FileWrite=new StreamWriter(FileRead,System.Text.Encoding.Default); FileWrite.Write(WriteWord);//关闭FileWrite.Close();FileReadWord.Close();FileRead.Close();}catch{// throw;}}/**/////// 在⽂件⾥追加内容//////public string ReaderFileData(string FilePathName){try{FileStream FileRead=new FileStream(System.Web.HttpContext.Current.Server.MapPath (FilePathName).ToString(),FileMode.Open,FileAccess.Read);StreamReader FileReadWord=new StreamReader(FileRead,System.Text.Encoding.Default); string TxtString = FileReadWord.ReadToEnd().ToString();//关闭FileReadWord.Close();FileRead.Close();return TxtString;}catch{throw;}}/**/////// 读取⽂件夹的⽂件/////////public DirectoryInfo checkValidSessionPath(string FilePathName){try{DirectoryInfo MainDir = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath (FilePathName));return MainDir;}catch{throw;}}}}。
中常见⽂件类型、扩展名、存放位置及⽤途总结.asax应⽤程序根⽬录。
通常是 Global.asax ⽂件,该⽂件包含从 HttpApplication 类派⽣并表⽰该应⽤程序的代码。
有关更多信息,请参见 Global.asax 语法。
.ascx应⽤程序根⽬录或⼦⽬录。
Web ⽤户控件⽂件,该⽂件定义⾃定义、可重复使⽤的⽤户控件。
有关更多信息,请参见 ⽤户控件。
.ashx应⽤程序根⽬录或⼦⽬录。
⼀般处理程序⽂件,该⽂件包含实现 IHttpHandler 接⼝以处理所有传⼊请求的代码。
有关更多信息,请参见 HTTP 处理程序介绍。
.asmx应⽤程序根⽬录或⼦⽬录。
XML Web services ⽂件,该⽂件包含通过 SOAP ⽅式可⽤于其他 Web 应⽤程序的类和⽅法。
有关更多信息,请参见 XML Web 服务的发布和部署。
.aspx应⽤程序根⽬录或⼦⽬录。
Web 窗体⽂件,该⽂件可包含 Web 控件和其他业务逻辑。
有关更多信息,请参见 ⽹页和 Web 服务器控件。
.axd应⽤程序根⽬录。
跟踪查看器⽂件,通常是 Trace.axd。
有关更多信息,请参见 跟踪。
.browserApp_Browsers ⼦⽬录。
浏览器定义⽂件,⽤于标识客户端浏览器的启⽤功能。
有关更多信息,请参见 Web 服务器控件和浏览器功能。
.cd应⽤程序根⽬录或⼦⽬录。
类关系图⽂件。
有关更多信息,请参见使⽤类关系图。
.compileBin ⼦⽬录。
预编译的 stub(存根)⽂件,该⽂件指向相应的程序集。
可执⾏⽂件类型(.aspx、ascx、.master、主题⽂件)已经过预编译并放在 Bin ⼦⽬录下。
有关更多信息,请参见 ⽹站预编译概述。
.config应⽤程序根⽬录或⼦⽬录。
通常是 Web.config 配置⽂件,该⽂件包含其设置配置各种 功能的 XML 元素。
有关更多信息,请参见 配置⽂件。
.cs、.jsl、.vbApp_Code ⼦⽬录;但如果是 页的代码隐藏⽂件,则与⽹页位于同⼀⽬录。
(C#)WebApi通过⽂件流下载⽂件的实例下载⽂件到本地是很多项⽬开发中需要实现的⼀个很简单的功能。
说简单,是从具体的代码实现上来说的,.NET的⽂件下载⽅式有很多种,本⽰例给⼤家介绍的是 Web Api⽅式返回HttpResponseMessage下载⽂件到本地。
实现的⽅法很简单,其中就是读取服务器的指定路径⽂件流,将其做为返回的HttpResponseMessage的Content。
直接贴出DownloadController控件器的代码:using System;using System.Collections.Generic;using System.IO;using System.Linq;using ;using .Http;using .Http.Headers;using System.Web.Http;namespace DownloadFileFromWebApi.Controllers{[RoutePrefix("download")]public class DownloadController : ApiController{[Route("get_demo_file")]public HttpResponseMessage GetFileFromWebApi(){try{var FilePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/download/EditPlus64_.zip");var stream = new FileStream(FilePath, FileMode.Open);HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);response.Content = new StreamContent(stream);response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {FileName="Wep Api Demo File.zip"};return response;}catch{return new HttpResponseMessage(HttpStatusCode.NoContent);}}}}以上就是本⽂的全部内容,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
实现访问局域网共享目录下文件教程局域网通常是分布在一个有限地理范围内的网络系统,一般所涉及的地理范围只有几公里。
局域网专用性非常强,具有比较稳定和规范的拓扑结构。
这篇文章主要介绍了实现访问局域网共享目录下文件的解决方法,需要的朋友可以参考下方法步骤using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.IO;using System.Security.Principal;using System.Runtime.InteropServices;public partial class _Default : System.Web.UI.Page{public const int LOGON32_LOGON_INTERACTIVE = 2;public const int LOGON32_PROVIDER_DEFAULT = 0;WindowsImpersonationContext impersonationContext;[DllImport("advapi32.dll")]public static extern int LogonUserA(String lpszUserName,String lpszDomain,String lpszPassword,int dwLogonType,int dwLogonProvider,ref IntPtr phToken);[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]public static extern int DuplicateT oken(IntPtr hToken,int impersonationLevel,ref IntPtr hNewToken);[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]public static extern bool RevertT oSelf();[DllImport("kernel32.dll", CharSet = CharSet.Auto)]public static extern bool CloseHandle(IntPtr handle);public void Page_Load(Object s, EventArgs e){if (impersonateValidUser("lucas", "Workgroup", "lcas")){string path = @"//zhehui001/lu";foreach (string f in Directory.GetFiles(path)){Response.Write(f);}undoImpersonation();}else{//Your impersonation failed. Therefore, include a fail-safe mechanism here.}}private bool impersonateValidUser(String userName, String domain, String password){WindowsIdentity tempWindowsIdentity;IntPtr token = IntPtr.Zero;IntPtr tokenDuplicate = IntPtr.Zero;if (RevertToSelf()){if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT, ref token) != 0){if (DuplicateT oken(token, 2, ref tokenDuplicate) != 0){tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);impersonationContext = tempWindowsIdentity.Impersonate();if (impersonationContext != null){CloseHandle(token);CloseHandle(tokenDuplicate);return true;}}}}if (token != IntPtr.Zero)CloseHandle(token);if (tokenDuplicate != IntPtr.Zero)CloseHandle(tokenDuplicate);return false;}private void undoImpersonation(){impersonationContext.Undo();}}补充:局域网、校园网安全维护方法校园网络分为内网和外网,就是说他们可以上学校的内网也可以同时上互联网,大学的学生平时要玩游戏购物,学校本身有自己的服务器需要维护;在大环境下,首先在校园网之间及其互联网接入处,需要设置防火墙设备,防止外部攻击,并且要经常更新抵御外来攻击;由于要保护校园网所有用户的安全,我们要安全加固,除了防火墙还要增加如ips,ids等防病毒入侵检测设备对外部数据进行分析检测,确保校园网的安全;外面做好防护措施,内部同样要做好防护措施,因为有的学生电脑可能带回家或者在外面感染,所以内部核心交换机上要设置vlan隔离,旁挂安全设备对端口进行检测防护;内网可能有ddos攻击或者arp病毒等传播,所以我们要对服务器或者电脑安装杀毒软件,特别是学校服务器系统等,安全正版安全软件,保护重要电脑的安全;对服务器本身我们要安全server版系统,经常修复漏洞及更新安全软件,普通电脑一般都是拨号上网,如果有异常上层设备监测一般不影响其他电脑。
1.1 第一步: 安装iis服务器iis是windows上的internet信息服务器.简单地说,就是建立www网站的服务器软件.和linux下的apache类似.因为一般的用户的电脑都没有装有iis,但是iis也是我们开发必备的软件,虽然它不是开发工具,但是它也是我们必须的.iis的安装文件一般都是放在了windows的安装光盘上,所以你得有你的windows安装光盘,然后从上面选择增加iis这个windows组件就可以了.安装完成后, c:\inetpub\wwwroot的目录就是你的www服务器对应的目录.比如说,http://localhost/xxx就对应c:\inetpub\wwwroot\xxx这个目录.这里的localhost就是本地主机的意思.当然了,如果是别人要访问这台机器,那么就需要把localhost换成本机的ip地址.1.2 第二步: 安装开发工具的开发工具当然就是microsoft的visual 了.我自己的机器上装的是2003版本,一般都会带有visual c++, visual c#, visual 等等工具当然就是我们这里要讲解的基于.net开发web应用程序的工具了开发出来的web应用程序的代码可以是c#也可以是,总之,开发出来的网页的执行代码只要是基于.net就可以了.有些时候,我们的安装iis和的顺序搞反了,先装.net后装iis, 结果就导致我们的iis上没有安装上.net的一些东西(比如),那么解决办法很简单.在.net的安装路径里面运行一个iis注册程序就可以了. 在我的计算机中路径如下:c:\windows\\framework\v1.1.4322, 我们只要在命令提示符执行此程序,即:c:\windows\\framework\v1.1.4322>c:\windows\\framework\v1.1. 4322\aspnet_regiis.exe –i或c:\windows\\framework\v1.1.4322>c:\windows\\framework\v1.1. 4322\aspnet_regiis.exe /i执行之后提示:开始安装 (1.1.4322.0)。
详解配置⽂件Web.configWeb.config⽂件是⼀个XML⽂本⽂件,它⽤来储存 Web 应⽤程序的配置信息(如最常⽤的设置 Web 应⽤程序的⾝份验证⽅式),它可以出现在应⽤程序的每⼀个⽬录中。
当你通过新建⼀个Web应⽤程序后,默认情况下会在根⽬录⾃动创建⼀个默认的Web.config⽂件,包括默认的配置设置,所有的⼦⽬录都继承它的配置设置。
如果你想修改⼦⽬录的配置设置,你可以在该⼦⽬录下新建⼀个Web.config⽂件。
它可以提供除从⽗⽬录继承的配置信息以外的配置信息,也可以重写或修改⽗⽬录中定义的设置。
在运⾏时对Web.config⽂件的修改不需要重启服务就可以⽣效(注:<processModel> 节例外)。
当然Web.config⽂件是可以扩展的。
你可以⾃定义新配置参数并编写配置节处理程序以对它们进⾏处理。
⼀、web.config配置⽂件(默认的配置设置)以下所有的代码都应该位于<configuration><system.web>和</system.web></configuration>之间,出于学习的⽬的下⾯的⽰例都省略了这段XML标记1、<authentication> 节作⽤:配置 ⾝份验证⽀持(为Windows、Forms、PassPort、None四种)。
该元素只能在计算机、站点或应⽤程序级别声明。
<authentication> 元素必需与<authorization> 节配合使⽤。
⽰例:以下⽰例为基于窗体(Forms)的⾝份验证配置站点,当没有登陆的⽤户访问需要⾝份验证的⽹页,⽹页⾃动跳转到登陆⽹页。
<authentication mode="Forms" ><forms loginUrl="logon.aspx" name=".FormsAuthCookie"/></authentication>其中元素loginUrl表⽰登陆⽹页的名称,name表⽰Cookie名称2、<authorization> 节作⽤:控制对 URL 资源的客户端访问(如允许匿名⽤户访问)。
步骤1)先引用ICSharpCode.SharpZipLib.dll;2).cs文件引入using System.IO;using System.Text;using ICSharpCode.SharpZipLib.Checksums;using ICSharpCode.SharpZipLib.Zip;using ICSharpCode.SharpZipLib.GZip;3)代码#region 下列代码为压缩并下载代码ZipOutputStream zos = null;String strBaseDir = "";void dlZipDir(string strPath, string strFileName){MemoryStream ms = null;Response.ContentType = "application/octet-stream";strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");ms = new MemoryStream();zos = new ZipOutputStream(ms);strBaseDir = strPath + "";addZipEntry(strBaseDir);zos.Finish();zos.Close();Response.Clear();Response.BinaryWrite(ms.ToArray());Response.End();}void addZipEntry(string PathStr){DirectoryInfo di = new DirectoryInfo(PathStr);foreach (DirectoryInfo item in di.GetDirectories()){addZipEntry(item.FullName);}foreach (FileInfo item in di.GetFiles()){FileStream fs = File.OpenRead(item.FullName);byte[] buffer = new byte[fs.Length];fs.Read(buffer, 0, buffer.Length);string strEntryName = item.FullName.Replace(strBaseDir, ""); ZipEntry entry = new ZipEntry(strEntryName);zos.PutNextEntry(entry);zos.Write(buffer, 0, buffer.Length);fs.Close();}}PRotected void Button2_Click(object sender, EventArgs e){string userPath ="D:华海实训()各级账号和密码";dlZipDir(userPath, "华海实训()服务账号");}#endregion更多信息请查看spx" target=_blank>IT技术专栏。
《ASP net开发技术》课程标准课程名称:ASP net开发技术课程编码:60415023总学时数:52学时理论学时26学时实践学时26学时适用专业:物联网应用技术一、课程概述1.课程定位《ASP net开发技术》课程是计算机系计算机网络技术专业学生的一门必修专业课,课程性质是技术类专业课,旨在培养学生应用 nET 技术开发Web 应用程序的能力,以培养学生实际开发Web 程序的主要技能和职业素养为主线,重点围绕面向对象程序设计基本技能、C# 语言程序设计能力、Web 网站开发能力等内容培养学生使用 nET 技术开发Web 应用程序的技能。
本课程是计算机系计算机网络技术专业的专业核心课程之一,是培养基于 net 技术Web 程序员的主要支撑课程。
通过课程学习使学生具备适应 Web 程序员岗位的专业技能和职业素养,并掌握一定的理论知识,为就业后的可持续发展奠定基础。
《ASP net开发技术》课程的先修课程有:《网页编程技术》、《C#程序设计》、《数据库技术》等课程,这些课程属于职业准备课程和职业技能训练课程,为学习本课程和职业岗位准备基本技能,后续课程是毕业设计或顶岗实习,属于岗位技能应用课程,与学生毕业后的职业岗位任职要求相一致,前、后续课程衔接得当,符合职业岗位的任职要求和职业发展的需要。
2.课程设计思路本课程以就业为导向,在行业专家的指导下,以Web 应用开发程序员、企业网站建设、企业网站维护管理等职业岗位能力培养为重点,以实际工作任务为引领,以创新能力培养为主线,将课程知识体系整合为9个技能教学模块,在教学过程中注意体现学生设计能力培养的循序渐进性,采用理论教学+实验实训+综合项目实训3层递进相结合的模式来提高教学质量。
实际教学中,按照项目导向、任务驱动教学模式,设计和开发一系列基于工作过程的教学标准和教学资源,实现“教、学、做”一体化的课堂教学。
在教学内容和方法上贯彻“技能培养为主,知识够用为度”的教学思想,旨在培养学生的创新意识,提高岗位实践能力和适应能力。
----实现⽹页中查看offic⽂件,word,ppt等原理很简单,实现⼀个树,先获取⽂件存放⽂件夹地址,然后遍历每个⽂档,填⼊到树节点中去,看下截图效果:具体看下⾯代码:File.cs:public partial class Files : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){if(!this.IsPostBack)this.InitTree();}private void InitTree(){//清除树节点this.trvFiles.Nodes.Clear();//实例化根节点TreeNode node = new TreeNode();node.Text = "简历⽂件";node.Value = "";//获取存放Word PPT⽂件的⽂件夹路径string dicPath = this.Server.MapPath("WordFiles");//实例化⽂件夹对象DirectoryInfo dic = new DirectoryInfo(dicPath);//循环获取Word⽂档⽂件foreach (FileInfo file in dic.GetFiles("*.doc")){//实例化⽂件节点TreeNode fileNode = new TreeNode();//赋值fileNode.Text = ;fileNode.Value = file.FullName;//添加⾄根节点中node.ChildNodes.Add(fileNode);continue;}//循环获取PPT⽂件foreach (FileInfo file in dic.GetFiles("*.ppt")){TreeNode fileNode = new TreeNode();fileNode.Text = ;fileNode.Value = file.FullName;node.ChildNodes.Add(fileNode);continue;}//展开⽂件夹节点node.Expand();this.trvFiles.Nodes.Add(node);}protected void trvFiles_SelectedNodeChanged(object sender, EventArgs e){//判断选中节点不是根节点if (!string.IsNullOrEmpty(this.trvFiles.SelectedNode.Value)){//拼装打开⽂件的JavaScript脚本string js = "window.frames[0].location.href='{0}';";js = string.Format(js, "WordFiles/" + this.trvFiles.SelectedNode.Text);//执⾏脚本即在DocShow.aspx页⾯打开相应节点this.WriteJS(js);}}protected void WriteJS(string msg){string script = string.Format("<script type='text/javascript' language='javascript'>{0}</script>", msg);this.ClientScript.RegisterStartupScript(this.GetType(), "alert", script);}protected void AlertMsg(string msg){string script = string.Format("<script type='text/javascript' language='javascript'>window.alert('{0}');</script>",msg);this.ClientScript.RegisterStartupScript(this.GetType(),"alert",script);}}页⾯布局:<body><form id="form1" runat="server"><div><table style="border: none; padding: none; margin: none; width: 100%; height: 100%;"><tr><td style="width:20%;height:100%;vertical-align:top;"><asp:TreeView ID="trvFiles" runat="server" ImageSet="XPFileExplorer" NodeIndent="15"OnSelectedNodeChanged="trvFiles_SelectedNodeChanged"><ParentNodeStyle Font-Bold="False"/><HoverNodeStyle Font-Underline="True" ForeColor="#6666AA"/><SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"VerticalPadding="0px"/><NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"NodeSpacing="0px" VerticalPadding="2px"/></asp:TreeView></td><td><iframe style="scroll:auto;width:100%;height:800px;border:none;" src="DocShow.aspx"></iframe></td></tr></table></div></form></body>⽹盘下载:。
文件和文件夹操作大全收藏/// <summary>/// 创建文件夹/// </summary>/// <param name="Path"></param>public static void FolderCreate(string Path){// 判断目标目录是否存在如果不存在则新建之if (!Directory.Exists(Path))Directory.CreateDirectory(Path);}#endregion#region 创建目录public static void FileCreate(string Path){FileInfo CreateFile = new FileInfo(Path); //创建文件if (!CreateFile.Exists){FileStream FS = CreateFile.Create();FS.Close();}}#endregion#region 递归删除文件夹目录及文件/***************************************** 函数名称:DeleteFolder* 功能说明:递归删除文件夹目录及文件* 参数:dir:文件夹路径* 调用示列:* string dir = Server.MapPath("test/");* EC.FileObj.DeleteFolder(dir);*****************************************//// <summary>/// 递归删除文件夹目录及文件/// </summary>/// <param name="dir"></param>/// <returns></returns>public static void DeleteFolder(string dir){if (Directory.Exists(dir)) //如果存在这个文件夹删除之{foreach (string d in Directory.GetFileSystemEntries(dir)){if (File.Exists(d))File.Delete(d); //直接删除其中的文件elseDeleteFolder(d); //递归删除子文件夹}Directory.Delete(dir, true); //删除已空文件夹}}#endregion#region 将指定文件夹下面的所有内容copy到目标文件夹下面果目标文件夹为只读属性就会报错。
/***************************************** 函数名称:CopyDir* 功能说明:将指定文件夹下面的所有内容copy到目标文件夹下面果目标文件夹为只读属性就会报错。
* 参数:srcPath:原始路径,aimPath:目标文件夹* 调用示列:* string srcPath = Server.MapPath("test/");* string aimPath = Server.MapPath("test1/");* EC.FileObj.CopyDir(srcPath,aimPath);*****************************************//// <summary>/// 指定文件夹下面的所有内容copy到目标文件夹下面/// </summary>/// <param name="srcPath">原始路径</param>/// <param name="aimPath">目标文件夹</param>public static void CopyDir(string srcPath, string aimPath){try{// 检查目标目录是否以目录分割字符结束如果不是则添加之if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)aimPath += Path.DirectorySeparatorChar;// 判断目标目录是否存在如果不存在则新建之if (!Directory.Exists(aimPath))Directory.CreateDirectory(aimPath);// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组//如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法//string[] fileList = Directory.GetFiles(srcPath);string[] fileList = Directory.GetFileSystemEntries(srcPath);//遍历所有的文件和目录foreach (string file in fileList){//先当作目录处理如果存在这个目录就递归Copy该目录下面的文件if (Directory.Exists(file))CopyDir(file, aimPath + Path.GetFileName(file));//否则直接Copy文件elseFile.Copy(file, aimPath + Path.GetFileName(file), true);}}catch (Exception ee){throw new Exception(ee.ToString());}}#endregion#region 获取指定文件夹下所有子目录及文件(树形)/***************************************** 函数名称:GetFoldAll(string Path)* 功能说明:获取指定文件夹下所有子目录及文件(树形)* 参数:Path:详细路径* 调用示列:* string strDirlist = Server.MapPath("templates");* this.Literal1.Text = EC.FileObj.GetFoldAll(strDirlist);*****************************************//// <summary>/// 获取指定文件夹下所有子目录及文件/// </summary>/// <param name="Path">详细路径</param>public static string GetFoldAll(string Path){string str = "";DirectoryInfo thisOne = new DirectoryInfo(Path);str = ListTreeShow(thisOne, 0, str);return str;}/// <summary>/// 获取指定文件夹下所有子目录及文件函数/// </summary>/// <param name="theDir">指定目录</param>/// <param name="nLevel">默认起始值,调用时,一般为0</param>/// <param name="Rn">用于迭加的传入值,一般为空</param>/// <returns></returns>public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn)//递归目录文件{DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录foreach (DirectoryInfo dirinfo in subDirectories){if (nLevel == 0){Rn += "├";}else{string _s = "";for (int i = 1; i <= nLevel; i++){_s += "│ ";}Rn += _s + "├";}Rn += "<b>" + .ToString() + "</b><br />";FileInfo[] fileInfo = dirinfo.GetFiles(); //目录下的文件foreach (FileInfo fInfo in fileInfo){if (nLevel == 0){Rn += "│ ├";}else{string _f = "";for (int i = 1; i <= nLevel; i++){_f += "│ ";}Rn += _f + "│ ├";}Rn += .ToString() + " <br />";}Rn = ListTreeShow(dirinfo, nLevel + 1, Rn);}return Rn;}/***************************************** 函数名称:GetFoldAll(string Path)* 功能说明:获取指定文件夹下所有子目录及文件(下拉框形)* 参数:Path:详细路径* 调用示列:* string strDirlist = Server.MapPath("templates");* this.Literal2.Text = EC.FileObj.GetFoldAll(strDirlist,"tpl","");*****************************************//// <summary>/// 获取指定文件夹下所有子目录及文件(下拉框形)/// </summary>/// <param name="Path">详细路径</param>///<param name="DropName">下拉列表名称</param>///<param name="tplPath">默认选择模板名称</param>public static string GetFoldAll(string Path,string DropName,string tplPath){string strDrop = "<select name=\"" + DropName + "\" id=\"" + DropName + "\"><option value=\"\">--请选择详细模板--</option>";string str = "";DirectoryInfo thisOne = new DirectoryInfo(Path);str = ListTreeShow(thisOne, 0, str,tplPath);return strDrop+str+"</select>";}/// <summary>/// 获取指定文件夹下所有子目录及文件函数/// </summary>/// <param name="theDir">指定目录</param>/// <param name="nLevel">默认起始值,调用时,一般为0</param>/// <param name="Rn">用于迭加的传入值,一般为空</param>/// <param name="tplPath">默认选择模板名称</param>/// <returns></returns>public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn,stringtplPath)//递归目录文件{DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录foreach (DirectoryInfo dirinfo in subDirectories){Rn += "<option value=\"" + .ToString() + "\"";if (tplPath.ToLower() == .ToString().ToLower()){Rn += " selected ";}Rn += ">";if (nLevel == 0){Rn += "┣";}else{string _s = "";for (int i = 1; i <= nLevel; i++){_s += "│ ";}Rn += _s + "┣";}Rn += "" + .ToString() + "</option>";FileInfo[] fileInfo = dirinfo.GetFiles(); //目录下的文件foreach (FileInfo fInfo in fileInfo){Rn += "<option value=\"" + .ToString()+"/"+.ToString() + "\"";if (tplPath.ToLower() == .ToString().ToLower()){Rn += " selected ";}Rn += ">";if (nLevel == 0){Rn += "│ ├";}else{string _f = "";for (int i = 1; i <= nLevel; i++){_f += "│ ";}Rn += _f + "│ ├";}Rn += .ToString() + "</option>";}Rn = ListTreeShow(dirinfo, nLevel + 1, Rn, tplPath);}return Rn;}#endregion#region 获取文件夹大小/***************************************** 函数名称:GetDirectoryLength(string dirPath)* 功能说明:获取文件夹大小* 参数:dirPath:文件夹详细路径* 调用示列:* string Path = Server.MapPath("templates");* Response.Write(EC.FileObj.GetDirectoryLength(Path)); *****************************************//// <summary>/// 获取文件夹大小/// </summary>/// <param name="dirPath">文件夹路径</param>/// <returns></returns>public static long GetDirectoryLength(string dirPath){if (!Directory.Exists(dirPath))return 0;long len = 0;DirectoryInfo di = new DirectoryInfo(dirPath);foreach (FileInfo fi in di.GetFiles()){len += fi.Length;}DirectoryInfo[] dis = di.GetDirectories();if (dis.Length > 0){for (int i = 0; i < dis.Length; i++){len += GetDirectoryLength(dis[i].FullName);}}return len;}#endregion#region 获取指定文件详细属性/***************************************** 函数名称:GetFileAttibe(string filePath)* 功能说明:获取指定文件详细属性* 参数:filePath:文件详细路径* 调用示列:* string file = Server.MapPath("robots.txt");* Response.Write(EC.FileObj.GetFileAttibe(file));*****************************************//// <summary>/// 获取指定文件详细属性/// </summary>/// <param name="filePath">文件详细路径</param>/// <returns></returns>public static string GetFileAttibe(string filePath){string str = "";System.IO.FileInfo objFI = new System.IO.FileInfo(filePath);str += "详细路径:" + objFI.FullName + "<br>文件名称:" + + "<br>文件长度:" + objFI.Length.ToString() + "字节<br>创建时间" + objFI.CreationTime.ToString() + "<br>最后访问时间:" + stAccessTime.ToString() + "<br>修改时间:" + stWriteTime.ToString() + "<br>所在目录:" + objFI.DirectoryName + "<br>扩展名:" + objFI.Extension;return str;}#endregion}}。