The following file action class, you can delete directories and not empty directories Oh, you can also create files to write files, delete files previously recursive operation directory.
Using System.IO;
Using System.Web;
Namespace sec
{
/**////
Action classes for files and folders
///
public class Filecontrol
{
Public Filecontrol ()
{
}
/**////
Create a folder under the root directory
///
The file path to create
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
}
}
}
/**////
Delete the word folders and files under a folder
///
///
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
}
}
}
/**////
Delete a file
///
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
{
Create a folder
String[] strpath= filepathname.split ('/');
CreateFolder (Filepathname.replace ("/" + strpath[strpath.length-1].tostring (), "")); Create a folder
FileInfo CreateFile =new FileInfo (System.web.httpcontext.current.server.mappath (filepathname). ToString ()); Create a file
if (!createfile.exists)
{
FileStream fs=createfile.create ();
Fs.close ();
}
}
Catch
{
}
}
/**////
Delete an entire folder and its Word folders and files
public void Deleparentfolder (string folderpathname)
{
Try
{
DirectoryInfo delfolder = new DirectoryInfo (System.web.httpcontext.current.server.mappath (folderpathname). ToString ());
if (delfolder.exists)
{
Delfolder.delete ();
}
}
Catch
{
}
}
/**////
Append content to File
public void Rewritereadinnertext (String filepathname,string writeword)
{
Try
{
Creating folders and Files
CreateFolder (filepathname);
CreateFile (filepathname);
Get the contents of the original file
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;
Re-write new content to
StreamWriter Filewrite=new StreamWriter (Fileread,system.text.encoding.default);
Filewrite.write (Writeword);
Shut down
Filewrite.close ();
Filereadword.close ();
Fileread.close ();
}
Catch
{
Throw
}
}
/**////
Append content to File
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 ();
Shut down
Filereadword.close ();
Fileread.close ();
return txtstring;
}
Catch
{
Throw
}
}
/**////
Read files for a folder
Public DirectoryInfo Checkvalidsessionpath (string filepathname)
{
Try
{
DirectoryInfo maindir = new DirectoryInfo (System.web.httpcontext.current.server.mappath (filepathname));
return maindir;
}
Catch
{
Throw
}
}
}
}