[原创]Asp.net对文件夹和文件的操作类

参考了一些资料,写了一个文件操作类和大家分享,有问题的地方请大家多多指教 .

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
// using System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.IO;
None.gif
None.gif
namespace  REC
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// FileControl 的摘要说明
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class FileControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public FileControl()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 判断是否存在指定文件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="Path"></param>
ExpandedSubBlockEnd.gif        
/// <param name="FileName"></param>

InBlock.gif        public void IsCreateFile(string Path)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(Directory.Exists(Path))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{}
InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                FileInfo CreateFile 
= new FileInfo(Path);         //创建文件 
InBlock.gif
                if (!CreateFile.Exists)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    FileStream FS 
= CreateFile.Create();
InBlock.gif                    FS.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// 在根目录下创建文件夹 
InBlock.gif        
/// </summary> 
ExpandedSubBlockEnd.gif        
/// <param name="FolderPath">要创建的文件路径</param> 

InBlock.gif        public 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("../../../Images/" + FolderPathName).ToString();
InBlock.gif                    
if (!Directory.Exists(CreatePath))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Directory.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        
/**//// <summary> 
InBlock.gif        
/// 删除一个文件夹下面的字文件夹和文件 
InBlock.gif        
/// </summary> 
ExpandedSubBlockEnd.gif        
/// <param name="FolderPathName"></param> 

InBlock.gif        public 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(FolderPathName).ToString();
InBlock.gif                    
if (Directory.Exists(CreatePath))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Directory.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        
/**//// <summary> 
InBlock.gif        
/// 删除一个文件 
InBlock.gif        
/// </summary> 
ExpandedSubBlockEnd.gif        
/// <param name="FilePathName"></param> 

InBlock.gif        public void DeleteFile(string FilePathName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                FileInfo DeleFile 
= new FileInfo(System.Web.HttpContext.Current.Server.MapPath(FilePathName).ToString());
InBlock.gif                DeleFile.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.gif                CreateFolder(FilePathName.Replace(
"/" + strPath[strPath.Length - 1].ToString(), "")); //创建文件夹 
InBlock.gif
                FileInfo CreateFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(FilePathName).ToString());         //创建文件 
InBlock.gif
                if (!CreateFile.Exists)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    FileStream FS 
= CreateFile.Create();
InBlock.gif                    FS.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// 删除整个文件夹及其字文件夹和文件 
InBlock.gif        
/// </summary> 
ExpandedSubBlockEnd.gif        
/// <param name="FolderPathName"></param> 

InBlock.gif        public void DeleParentFolder(string FolderPathName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DirectoryInfo DelFolder 
= new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath(FolderPathName).ToString());
InBlock.gif                
if (DelFolder.Exists)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DelFolder.Delete();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// 在文件里追加内容 
InBlock.gif        
/// </summary> 
ExpandedSubBlockEnd.gif        
/// <param name="FilePathName"></param> 

InBlock.gif        public 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(FilePathName).ToString(), FileMode.Open, FileAccess.ReadWrite);
InBlock.gif                StreamReader FileReadWord 
= new StreamReader(FileRead, System.Text.Encoding.Default);
InBlock.gif                
string OldString = FileReadWord.ReadToEnd().ToString();
InBlock.gif                OldString 
= OldString + WriteWord;
InBlock.gif                
//把新的内容重新写入 
InBlock.gif
                StreamWriter FileWrite = new StreamWriter(FileRead, System.Text.Encoding.Default);
InBlock.gif                FileWrite.Write(WriteWord);
InBlock.gif                
//关闭 
InBlock.gif
                FileWrite.Close();
InBlock.gif                FileReadWord.Close();
InBlock.gif                FileRead.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//    throw; 
ExpandedSubBlockEnd.gif
            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// 在文件里追加内容 
InBlock.gif        
/// </summary> 
ExpandedSubBlockEnd.gif        
/// <param name="FilePathName"></param> 

InBlock.gif        public string ReaderFileData(string FilePathName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                FileStream FileRead 
= new FileStream(System.Web.HttpContext.Current.Server.MapPath(FilePathName).ToString(), FileMode.Open, FileAccess.Read);
InBlock.gif                StreamReader FileReadWord 
= new StreamReader(FileRead, System.Text.Encoding.Default);
InBlock.gif                
string TxtString = FileReadWord.ReadToEnd().ToString();
InBlock.gif                
//关闭 
InBlock.gif
                FileReadWord.Close();
InBlock.gif                FileRead.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        
/**//// <summary> 
InBlock.gif        
/// 读取文件夹的文件 
InBlock.gif        
/// </summary> 
InBlock.gif        
/// <param name="FilePathName"></param> 
ExpandedSubBlockEnd.gif        
/// <returns></returns> 

InBlock.gif        public DirectoryInfo checkValidSessionPath(string FilePathName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DirectoryInfo MainDir 
= new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath(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}

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值