Using system;
Using system. IO;
Using system. Web;
Namespace Sec
{
/**////
/// Operation class for files and folders
///
Public class filecontrol
{
Public filecontrol ()
{
}
/**////
/// Create a folder under the root directory
///
/// Path of the file to be created
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;
}
}
}
/**////
/// 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 an object
///
///
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 file
Clip
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 the 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 the file
///
///
Public void rewritereadinnertext (string filepathname, string writeword)
{
Try
{
// Create folders and files
// Createfolder (filepathname );
Createfile (filepathname );
// Obtain the content 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;
// Write new content again
Streamwriter filewrite = new streamwriter (fileread, system. Text. encoding. Default );
Filewrite. Write (writeword );
// Close
Filewrite. Close ();
Filereadword. Close ();
Fileread. Close ();
}
Catch
{
// Throw;
}
}
/**////
/// Append content to the 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 ();
// Close
Filereadword. Close ();
Fileread. Close ();
Return txtstring;
}
Catch
{
Throw;
}
}
/**////
/// Read the file in the folder
///
///
///
Public directoryinfo checkvalidsessionpath (string filepathname)
{
Try
{
Directoryinfo maindir = new directoryinfo (system. Web. httpcontext. Current. server. mappath
(Filepathname ));
Return maindir;
}
Catch
{
Throw;
}
}
}
}