博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Asp.net对文件夹和文件的操作类
阅读量:6613 次
发布时间:2019-06-24

本文共 3263 字,大约阅读时间需要 10 分钟。

using
 System; 
None.gif
using
 System.IO; 
None.gif
using
 System.Web; 
None.gif
None.gif
namespace
 SEC 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// 
InBlock.gif
/// 对文件和文件夹的操作类 
ExpandedSubBlockEnd.gif
/// 
InBlock.gifpublic class FileControl 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
public FileControl() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// 
InBlock.gif
/// 在根目录下创建文件夹 
InBlock.gif
/// 
ExpandedSubBlockEnd.gif
/// 要创建的文件路径 
InBlock.gifpublic void CreateFolder(string FolderPathName) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if(FolderPathName.Trim().Length> 0
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
string CreatePath = System.Web.HttpContext.Current.Server.MapPath 
InBlock.gif
InBlock.gif(
"../../../Images/"+FolderPathName).ToString(); 
InBlock.gif
if(!Directory.Exists(CreatePath)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifDirectory.CreateDirectory(CreatePath); 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
throw
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// 
InBlock.gif
/// 删除一个文件夹下面的字文件夹和文件 
InBlock.gif
/// 
ExpandedSubBlockEnd.gif
/// 
InBlock.gifpublic void DeleteChildFolder(string FolderPathName) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if(FolderPathName.Trim().Length> 0
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
string CreatePath = System.Web.HttpContext.Current.Server.MapPath 
InBlock.gif
InBlock.gif(FolderPathName).ToString(); 
InBlock.gif
if(Directory.Exists(CreatePath)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifDirectory.Delete(CreatePath,
true); 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
throw
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// 
InBlock.gif
/// 删除一个文件 
InBlock.gif
/// 
ExpandedSubBlockEnd.gif
/// 
InBlock.gifpublic void DeleteFile(string FilePathName) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifFileInfo DeleFile 
= new FileInfo(System.Web.HttpContext.Current.Server.MapPath 
InBlock.gif
InBlock.gif(FilePathName).ToString()); 
InBlock.gifDeleFile.Delete(); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
public void CreateFile(string FilePathName) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
//创建文件夹 
InBlock.gif
string[] strPath= FilePathName.Split('/'); 
InBlock.gifCreateFolder(FilePathName.Replace(
"/" + strPath[strPath.Length-1].ToString(),"")); //创建文件 
InBlock.gif
InBlock.gif夹 
InBlock.gifFileInfo CreateFile 
=new FileInfo(System.Web.HttpContext.Current.Server.MapPath 
InBlock.gif
InBlock.gif(FilePathName).ToString()); 
//创建文件 
InBlock.gif
if(!CreateFile.Exists) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifFileStream FS
=CreateFile.Create(); 
InBlock.gifFS.Close(); 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// 
InBlock.gif
/// 删除整个文件夹及其字文件夹和文件 
InBlock.gif
/// 
ExpandedSubBlockEnd.gif
/// 
InBlock.gifpublic void DeleParentFolder(string FolderPathName) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifDirectoryInfo DelFolder 
= new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath 
InBlock.gif
InBlock.gif(FolderPathName).ToString()); 
InBlock.gif
if(DelFolder.Exists) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifDelFolder.Delete(); 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// 
InBlock.gif
/// 在文件里追加内容 
InBlock.gif
/// 
ExpandedSubBlockEnd.gif
/// 
InBlock.gifpublic void ReWriteReadinnerText(string FilePathName,string WriteWord) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
//建立文件夹和文件 
InBlock.gif
//CreateFolder(FilePathName); 
InBlock.gif
CreateFile(FilePathName); 
InBlock.gif
//得到原来文件的内容 
InBlock.gif
FileStream FileRead=new FileStream(System.Web.HttpContext.Current.Server.MapPath 
InBlock.gif
InBlock.gif(FilePathName).ToString(),FileMode.Open,FileAccess.ReadWrite); 
InBlock.gifStreamReader FileReadWord
=new StreamReader(FileRead,System.Text.Encoding.Default); 
InBlock.gif
string OldString = FileReadWord.ReadToEnd().ToString(); 
InBlock.gifOldString 
= OldString + WriteWord; 
InBlock.gif
//把新的内容重新写入 
InBlock.gif
StreamWriter FileWrite=new StreamWriter(FileRead,System.Text.Encoding.Default); 
InBlock.gifFileWrite.Write(WriteWord); 
InBlock.gif
//关闭 
InBlock.gif
FileWrite.Close(); 
InBlock.gifFileReadWord.Close(); 
InBlock.gifFileRead.Close(); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
// throw; 
ExpandedSubBlockEnd.gif
}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// 
InBlock.gif
/// 在文件里追加内容 
InBlock.gif
/// 
ExpandedSubBlockEnd.gif
/// 
InBlock.gifpublic string ReaderFileData(string FilePathName) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifFileStream FileRead
=new FileStream(System.Web.HttpContext.Current.Server.MapPath 
InBlock.gif
InBlock.gif(FilePathName).ToString(),FileMode.Open,FileAccess.Read); 
InBlock.gifStreamReader FileReadWord
=new StreamReader(FileRead,System.Text.Encoding.Default); 
InBlock.gif
string TxtString = FileReadWord.ReadToEnd().ToString(); 
InBlock.gif
//关闭 
InBlock.gif
FileReadWord.Close(); 
InBlock.gifFileRead.Close(); 
InBlock.gif
return TxtString; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
throw
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// 
InBlock.gif
/// 读取文件夹的文件 
InBlock.gif
/// 
InBlock.gif
/// 
ExpandedSubBlockEnd.gif
/// 
InBlock.gifpublic DirectoryInfo checkValidSessionPath(string FilePathName) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifDirectoryInfo MainDir 
= new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath 
InBlock.gif
InBlock.gif(FilePathName)); 
InBlock.gif
return MainDir; 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
throw
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 

转载于:https://www.cnblogs.com/zhangchenliang/archive/2007/05/06/736754.html

你可能感兴趣的文章
Myeclipse中打开接口实现类的快捷键
查看>>
使用JdbcTemplate和JdbcDaoSupport
查看>>
Glibc 和 uClibc
查看>>
Mysql学习第三课-分析二进制日志进行增量备份和还原
查看>>
HDU 6073 - Matching In Multiplication | 2017 Multi-University Training Contest 4
查看>>
如何检测域名是否被微信屏蔽 微信域名检测接口API是如何实现
查看>>
POJ1611-The Suspects
查看>>
Linux下安装Python-3.3.2【转】
查看>>
LeetCode OJ:Merge Two Sorted Lists(合并两个链表)
查看>>
功能测试
查看>>
【BZOJ 1901】Dynamic Rankings
查看>>
PAT (Advanced Level) 1028. List Sorting (25)
查看>>
【转】聚集索引和非聚集索引的区别
查看>>
Github-Client(ANDROID)开源之旅(二) ------ 浅析ActionBarSherkLock
查看>>
eclipse中如何去除警告:Class is a raw type. References to generic type Class<T> should be parameterized...
查看>>
k sum(lintcode)
查看>>
Android 控件属性
查看>>
React-Native 之 GD (十六)首页筛选功能
查看>>
SSISDB5:使用TSQL脚本执行Package
查看>>
asp.net后台进程做定时任务
查看>>