One folder operation class

Source: Internet
Author: User

// Yang Chun
// 2008-7-10
// Delete all files in the specified directory to process the cached files generated by the pie chart column.

Using system;
Using system. Data;
Using system. IO;
Using system. Data. sqlclient;

/// <Summary>
/// Summary of deletefile
/// </Summary>
Namespace Yangchun
{
Public class deletefile
{
Public deletefile ()
{
//
// Todo: add the constructor logic here
//
}

/// <Summary>
/// Delete an object based on the file path
/// </Summary>
/// <Param name = "path"> file name (PATH) </param>
/// <Returns> Successful </returns>
Public static bool deletesiglefile (string path)
{
Fileinfo file = new fileinfo (PATH );
Try
{
If (file. exists)
{
File. Delete ();
Return true;
}
Else
{
Return false;
}
}
Catch (exception ex)
{
Console. Write (ex. Message );
Return false;
}
}

/// <Summary>
/// Delete all objects in the specified directory and Directory
/// </Summary>
/// <Param name = "path"> directory path </param>
/// <Returns> Successful </returns>
Public static bool deletedirectory (string path)
{
Fileinfo info = new fileinfo (PATH );
If (info. exists)
{
Return deletesiglefile (PATH );
}
Else
{
Directoryinfo directory = new directoryinfo (PATH );
Try
{
If (directory. exists! = True)
{
Return false;
}
If (directory. getfiles (). Length <1)
{
Directory. Delete ();
Return true;
}
Else
{
Foreach (fileinfo file in directory. getfiles ())
{
String filepath = file. fullname;
If (deletesiglefile (filepath )! = True)
{
Return false;
}
}
Directory. Delete ();
Return true;
}
}
Catch (exception ex)
{
Console. Write (ex. Message );
Return false;
}
}
}

/// <Summary>
/// Delete all files in the directory according to the path (excluding the Directory)
/// </Summary>
/// <Param name = "path"> path </param>
/// <Returns> whether the deletion is successful </returns>
Public static bool deleteallfileindirectory (string path)
{
Fileinfo file = new fileinfo (PATH );
If (file. exists)
{
Return deletesiglefile (PATH );
}
Else
{
Directoryinfo directory = new directoryinfo (PATH );
Try
{
If (directory. exists! = True)
{
Return false;
}
If (directory. getfiles (). Length <1)
{
Return true;
}
Else
{
Foreach (fileinfo info in directory. getfiles ())
{
String filepath = info. fullname;
If (deletesiglefile (filepath )! = True)
{
Return false;
}
}
Return true;
}
}
Catch (exception ex)
{
Console. Write (ex. Message );
Return false;
}
}

}

///


// get the folder size
///
/// path
// size
Public static long getdirectorylength (string path)
{< br> long length = 0;
If (directory. exists (PATH)
{< br> directoryinfo directory = new directoryinfo (PATH);

Foreach (fileinfo file in directory. getfiles ())
{
Length + = file. length;
}
Directoryinfo [] directoryinfo = directory. getdirectories ();
If (directoryinfo. length> 0)
{
For (INT I = 0; I <directoryinfo. length; I ++)
{
Length = getdirectorylength (directoryinfo [I]. fullname );
}
}
Return length;
}
Else
{
Return length;
}
}

/// <Summary>
/// Obtain the number of records in the table based on the table name
/// </Summary>
/// <Param name = "tablename"> table name </param>
/// <Returns> Number of record rows </returns>
Public static int gettablerecordcount (string tablename)
{
Int result = 0;
String SQL = "select count (0) as tablecount from" + tablename;
Sqlconnection connection = new sqlconnection (DAL. dbhelpersql. getconnectionstring ());
Sqlcommand cmd = new sqlcommand (SQL, connection );
Try
{
Connection. open ();
Result = (INT) cmd. executescalar ();
Return result;
}
Catch (exception ex)
{
Console. Write (ex. Message );
Return 0;
}
Finally
{
Cmd. Dispose ();
Connection. Close ();
}

}

/// <Summary>
/// Delete all records of the table based on the table name
/// </Summary>
/// <Param name = "tablename"> table name </param>
/// <Returns> whether the operation is successful </returns>
Public static bool deletetablerecord (string tablename)
{
String SQL = "delete from" + tablename;
Sqlconnection connection = new sqlconnection (DAL. dbhelpersql. getconnectionstring ());
Sqlcommand cmd = new sqlcommand (SQL, connection );
Try
{
Connection. open ();
If (CMD. executenonquery ()> 0)
{
Return true;
}
Else
{
Return false;
}
}
Catch (exception ex)
{
Console. Write (ex. Message );
Return false;
}
Finally
{
Cmd. Dispose ();
Connection. Close ();
}
}
}
}

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.