asp.net建立檔案夾

來源:互聯網
上載者:User
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

("http://www.cnblogs.com/../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;
}
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.