Create a folder and delete a folder

Source: Internet
Author: User

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;
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.