Common Operation file DLL class

Source: Internet
Author: User

/// <Summary>
/// All operations on files
/// </Summary>
Public class FileOption
{

/// <Summary>
/// Whether the deletion is successful
/// </Summary>
/// <Param name = "FileFullPath"> Delete the entire file path </param>
/// <Returns> whether the deletion is successful </returns>
Public bool DeleteFile (string FileFullPath)
{
Try
{
If (File. Exists (FileFullPath) = true)
{
If (File. GetAttributes (FileFullPath) = FileAttributes. Normal)
{
File. Delete (FileFullPath );
}
Else
{
File. SetAttributes (FileFullPath, FileAttributes. Normal );
File. Delete (FileFullPath );
}
}
Return true;
}
Catch
{
Return false;


}
}

/// <Summary>
/// Get the file name
/// </Summary>
/// <Param name = "FileFullPath"> </param>
/// <Returns> </returns>
Public string GetFileName (string FileFullPath)
{
If (File. Exists (FileFullPath ))
{

FileInfo file = new FileInfo (FileFullPath );
Return file. Name;
}
Else
{
Return null;
}
}

/// <Summary>
/// Get the file
/// </Summary>
/// <Param name = "FileFullPath"> </param>
/// <Param name = "IncludeExtention"> </param>
/// <Returns> </returns>
Public string GetFileName (string FileFullPath, bool IncludeExtention)
{
If (File. Exists (FileFullPath ))
{
FileInfo fileinfo = new FileInfo (FileFullPath );

If (IncludeExtention)
{
Return fileinfo. Name;
}
Else
{
Return fileinfo. Name. Replace (fileinfo. Extension ,"");
}
}
Else
{
Return null;
}
}

/// <Summary>
/// Get the extension
/// </Summary>
/// <Param name = "FileFullPath"> </param>
/// <Returns> </returns>
Public String GetFileExtent (string FileFullPath)
{
If (File. Exists (FileFullPath ))
{

FileInfo file = new FileInfo (FileFullPath );
Return file. Extension;
}
Else
{
Return null;
}
}

/// <Summary>
/// Whether to open the file
/// </Summary>
/// <Param name = "FileFullPath"> </param>
/// <Returns> </returns>
Public bool OpentFile (string FileFullPath)
{
If (File. Exists (FileFullPath ))
{
System. Diagnostics. Process. Start (FileFullPath );
Return true;
}
Else
{
Return false;
}
}

/// <Summary>
/// Get the file size
/// </Summary>
/// <Param name = "FileFullPath"> </param>
/// <Returns> </returns>
Public string GetFileSize (string FileFullPath)
{
If (File. Exists (FileFullPath ))
{
FileInfo fileInfo = new FileInfo (FileFullPath );
Long fileLength = fileInfo. Length;
If (fileLength> 1024*1024*1024)
{
Return Math. Round (fileLength + 0.0)/(1024*1024*1024), 2). ToString () + "G ";
}
Else if (fileLength & gt; 1024*1024)
{
Return Math. Round (fileLength + 0.0)/(1024*1024), 2). ToString () + "m ";
}
Else
{
Return Math. Round (fileLength + 0.0)/(1024), 2). ToString () + "kb ";
}

}
Else
{
Return "0 ";
}
}

 

/// <Summary>
/// Convert the object to a byte
/// </Summary>
/// <Param name = "FileFullPath"> </param>
/// <Returns> </returns>
Public byte [] FileToStreanByte (string FileFullPath)
{
Byte [] fileData = null;
If (File. Exists (FileFullPath ))
{
FileStream fs = new FileStream (FileFullPath, FileMode. OpenOrCreate );
FileData = new byte [fs. Length];
Fs. Read (fileData, 0, fileData. Length );
Fs. Close ();
}
Return fileData;
}

/// <Summary>
///
/// </Summary>
/// <Param name = "FileFullPath"> </param>
/// <Param name = "Bytesfile"> </param>
/// <Returns> </returns>
Public bool ByteToFileStrean (string FileFullPath, byte [] Bytesfile)
{
Try
{
If (File. Exists (FileFullPath ))
{
File. Delete (FileFullPath );
}
FileStream fs = File. Create (FileFullPath );
Fs. Write (Bytesfile, 0, Convert. ToInt32 (fs. Length ));
Fs. Close ();
Return true;
}
Catch
{

Return false;
}
}

/// <Summary>
/// Serialization
/// </Summary>
/// <Param name = "FileFullPath"> </param>
/// <Returns> </returns>
Public bool SerializeXmlFile (string FileFullPath)
{
Try
{
System. Data. DataSet DS = new System. Data. DataSet ();
DS. WriteXml (FileFullPath );
FileStream FS = new FileStream (FileFullPath + ". TMP", FileMode. OpenOrCreate );
System. Runtime. Serialization. Formatters. Binary. BinaryFormatter BF = new System. Runtime. Serialization. Formatters. Binary. BinaryFormatter ();
BF. Serialize (FS, DS );
FS. Close ();
DeleteFile (FileFullPath );
File. Move (FileFullPath + ". TMP", FileFullPath );
Return true;
}
Catch
{

Return false;
}


}

/// <Summary>
/// Reverse Sequence XML
/// </Summary>
/// <Param name = "FileFullPath"> </param>
/// <Returns> </returns>
Public bool deserialexmlfile (string FileFullPath)
{
Try
{
System. Data. DataSet ds = new System. Data. DataSet ();
FileStream FS = new FileStream (FileFullPath, FileMode. OpenOrCreate );
System. Runtime. Serialization. Formatters. Binary. BinaryFormatter BF = new System. Runtime. Serialization. Formatters. Binary. BinaryFormatter ();
(System. Data. DataSet) BF. Deserialize (FS). WriteXml (FileFullPath + ". TMP ");
FS. Close ();
DeleteFile (FileFullPath );
File. Move (FileFullPath + ". TMP", FileFullPath );
Return true;
}
Catch
{

Return false;
}
}

 

 

 

 

}

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.