asp.net file Operations base class (read, delete, bulk copy, delete, write, get folder size, file attributes, Traverse directory) _ Practical Tips

Source: Internet
Author: User
Tags readfile

Copy Code code as follows:

Using System;
Using System.IO;
Using System.Text;
Using System.Data;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Namespace EC
{
<summary>
File Action Class
</summary>
public class Fileobj:idisposable
{
private bool _alreadydispose = false;
#region Constructors
Public Fileobj ()
{
//
TODO: Add constructor logic here
//
}
~fileobj ()
{
Dispose ();;
}
protected virtual void Dispose (bool isdisposing)
{
if (_alreadydispose) return;
if (isdisposing)
//{
if (XML!= null)
// {
XML = NULL;
// }
//}
_alreadydispose = true;
}
#endregion
#region IDisposable Members
public void Dispose ()
{
Dispose (TRUE);
Gc. SuppressFinalize (this);
}
#endregion
#region Get file suffix name
/****************************************
* Function Name: GETPOSTFIXSTR
* Function Description: Get file suffix name
* Parameter: FileName: file name
* Call Columns:
* String filename = "aaa.aspx";
* string s = EC. FILEOBJ.GETPOSTFIXSTR (filename);
*****************************************/
<summary>
Take suffix name
</summary>
<param name= "filename" > FileName </param>
<returns>.gif|. HTML format </returns>
public static string Getpostfixstr (string filename)
{
int start = filename. LastIndexOf (".");
int length = filename. Length;
string postfix = filename. Substring (start, Length-start);
return postfix;
}
#endregion
#region Write a file
/****************************************
* Function Name: WriteFile
* Function Description: Write the file, will cover up the previous content
* Parameter: Path: File path, Strings: text content
* Call Columns:
* String Path = Server.MapPath ("default2.aspx");
* String Strings = "This is what I write";
* EC. Fileobj.writefile (path,strings);
*****************************************/
<summary>
Write a file
</summary>
<param name= "path" > File path </param>
<param name= "Strings" > File content </param>
public static void WriteFile (String Path, String Strings)
{
if (! System.IO.File.Exists (Path))
{
System.IO.FileStream f = System.IO.File.Create (Path);
F.close ();
}
System.IO.StreamWriter F2 = new System.IO.StreamWriter (Path, False, System.Text.Encoding.GetEncoding ("gb2312"));
F2. Write (Strings);
F2. Close ();
F2. Dispose ();
}
#endregion
#region Read files
/****************************************
* Function Name: ReadFile
* Function Description: Read text content
* Parameters: Path: File paths
* Call Columns:
* String Path = Server.MapPath ("default2.aspx");
* string s = EC. Fileobj.readfile (Path);
*****************************************/
<summary>
Read files
</summary>
<param name= "path" > File path </param>
<returns></returns>
public static string ReadFile (String Path)
{
string s = "";
if (! System.IO.File.Exists (Path))
s = "There is no corresponding directory";
Else
{
StreamReader F2 = new StreamReader (Path, System.Text.Encoding.GetEncoding ("gb2312"));
s = F2. ReadToEnd ();
F2. Close ();
F2. Dispose ();
}
return s;
}
#endregion
#region Append Files
/****************************************
* Function Name: Fileadd
* Function Description: Append file contents
* Parameter: Path: File path, strings: Content
* Call Columns:
* String Path = Server.MapPath ("default2.aspx");
* String Strings = "new additions";
* EC. Fileobj.fileadd (Path, Strings);
*****************************************/
<summary>
Append files
</summary>
<param name= "path" > File path </param>
<param name= "Strings" > Content </param>
public static void Fileadd (String Path, String strings)
{
StreamWriter sw = File.appendtext (Path);
Sw. Write (strings);
Sw. Flush ();
Sw. Close ();
}
#endregion
#region Copy Files
/****************************************
* Function Name: filecoppy
* Function Description: Copy file
* Parameter: Orignfile: Original file, NewFile: New file path
* Call Columns:
* String orignfile = Server.MapPath ("default2.aspx");
* String NewFile = Server.MapPath ("default3.aspx");
* EC. Fileobj.filecoppy (Orignfile, NewFile);
*****************************************/
<summary>
Copy files
</summary>
<param name= "Orignfile" > Original file </param>
<param name= "NewFile" > New file path </param>
public static void Filecoppy (String orignfile, String NewFile)
{
File.Copy (Orignfile, NewFile, true);
}
#endregion
#region Delete Files
/****************************************
* Function Name: Filedel
* Function Description: Delete file
* Parameters: Path: File paths
* Call Columns:
* String Path = Server.MapPath ("default3.aspx");
* EC. Fileobj.filedel (Path);
*****************************************/
<summary>
deleting files
</summary>
<param name= "path" > Path </param>
public static void Filedel (String Path)
{
File.delete (Path);
}
#endregion
Moving Files #region
/****************************************
* Function Name: Filemove
* Function Description: Move file
* Parameters: Orignfile: Original path, NewFile: New file path
* Call Columns:
* String orignfile = Server.MapPath (".. /description. txt ");
* String NewFile = Server.MapPath (".. /.. /description. txt ");
* EC. Fileobj.filemove (Orignfile, NewFile);
*****************************************/
<summary>
Moving files
</summary>
<param name= "Orignfile" > Original path </param>
<param name= "NewFile" > New path </param>
public static void Filemove (String orignfile, String NewFile)
{
File.move (Orignfile, NewFile);
}
#endregion
#region Create a directory under the current directory
/****************************************
* Function Name: foldercreate
* Function Description: Create a table of contents in the current directory
* Parameters: Orignfolder: Current directory, Newfloder: New directory
* Call Columns:
* String orignfolder = Server.MapPath ("test/");
* String newfloder = "new";
* EC. Fileobj.foldercreate (Orignfolder, Newfloder);
*****************************************/
<summary>
Create a table of contents in the current directory
</summary>
<param name= "Orignfolder" > current directory </param>
<param name= "Newfloder" > New directory </param>
public static void FolderCreate (String orignfolder, String newfloder)
{
Directory.setcurrentdirectory (Orignfolder);
Directory.CreateDirectory (Newfloder);
}
#endregion
#region Recursively Delete folder directories and files
/****************************************
* Function Name: DeleteFolder
* Function Description: Delete folder directory and file recursively
* Parameters: Dir: Folder path
* Call Columns:
* String dir = Server.MapPath ("test/");
* EC. Fileobj.deletefolder (dir);
*****************************************/
<summary>
Recursively delete folder directories and files
</summary>
<param name= "dir" ></param>
<returns></returns>
public static void DeleteFolder (String dir)
{
if (Directory.Exists (dir))//If there is a deletion of this folder
{
foreach (String D in Directory.getfilesystementries (dir))
{
if (file.exists (d))
File.delete (d); Delete files directly from them
Else
DeleteFolder (d); recursively deletes subfolders
}
Directory.delete (dir); Delete an empty folder
}
}
#endregion
#region Copy all content under the specified folder to the destination folder The result is an error if the destination folder is read-only.
/****************************************
* Function Name: copydir
* Function Description: Copy all content under the specified folder to the destination folder The destination folder is read-only and the error will be.
* Parameters: Srcpath: Original path, Aimpath: Destination Folder
* Call Columns:
* String srcpath = Server.MapPath ("test/");
* String aimpath = Server.MapPath ("test1/");
* EC. Fileobj.copydir (Srcpath,aimpath);
*****************************************/
<summary>
Specifies that all content under the folder is copy to the target folder
</summary>
<param name= "Srcpath" > Original path </param>
<param name= "Aimpath" > Target folder </param>
public static void Copydir (String srcpath, String aimpath)
{
Try
{
Checks whether the destination directory ends with a directory split character if it is not added
if (Aimpath[aimpath.length-1]!= Path.directoryseparatorchar)
Aimpath + = Path.directoryseparatorchar;
Determine if the target directory exists if it does not exist the new
if (! Directory.Exists (Aimpath))
Directory.CreateDirectory (Aimpath);
Gets a list of files in the source directory that contains an array of files and directory paths
If you point to the file below the copy destination file and not the directory, use the following method
string[] filelist = Directory.GetFiles (Srcpath);
string[] filelist = directory.getfilesystementries (Srcpath);
Iterate through all the files and directories
foreach (string file in FileList)
{
First as a directory processing if there is this directory recursively copy the file below the directory
if (directory.exists (file))
Copydir (file, Aimpath + path.getfilename (file));
Otherwise direct copy file
Else
File.Copy (file, Aimpath + path.getfilename (file), true);
}
}
catch (Exception ee)
{
throw new Exception (EE. ToString ());
}
}
#endregion
}
}

Related Article

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.