File Read and save class

Source: Internet
Author: User

 

Using System;
Using System. IO;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace CommHelper
{
Public class FilesAccess
{
Public FilesAccess ()
{}
 
# Region
/// <Summary>
/// Write the file content to the physical location
/// </Summary>
/// <Param name = "as_filename"> </param>
/// <Param name = "a_FileData"> </param>
/// <Returns> </returns>
Public static string WriteFile (string as_filename, byte [] a_FileData)
{
String sRet = "";
If (as_filename = "")
{
SRet = "specify the path and file name ";
Return sRet;
}
Try
{
System. IO. FileStream objFile = new System. IO. FileStream (as_filename,
System. IO. FileMode. OpenOrCreate, System. IO. FileAccess. ReadWrite, System. IO. FileShare. ReadWrite );
System. IO. BinaryWriter objSW = new BinaryWriter (objFile, System. Text. Encoding. Unicode );
ObjSW. BaseStream. Write (a_FileData, 0, a_FileData.Length );
ObjSW. Close ();
ObjSW = null;
ObjFile. Close ();
ObjFile = null;
}
Catch (Exception ex)
{
SRet = ex. Message;
}
Return sRet;
}
/// <Summary>
/// Write the file content to the physical location
/// </Summary>
/// <Param name = "as_filename"> </param>
/// <Param name = "as_FileData"> </param>
/// <Returns> </returns>
Public static string WriteFile (string as_filename, string as_FileData)
{
String sRet = "";
If (as_filename = "")
{
SRet = "specify the path and file name ";
Return sRet;
}
Try
{
System. IO. FileStream objFile = new System. IO. FileStream (as_filename,
System. IO. FileMode. OpenOrCreate, System. IO. FileAccess. ReadWrite, System. IO. FileShare. ReadWrite );
System. IO. BinaryWriter objSW = new BinaryWriter (objFile, System. Text. Encoding. Unicode );
Byte [] tmpData = System. Text. Encoding. Unicode. GetBytes (as_FileData );
ObjSW. BaseStream. Write (tmpData, 0, tmpData. Length );
ObjSW. Close ();
ObjSW = null;
ObjFile. Close ();
ObjFile = null;
SRet = "";
}
Catch (Exception exp)
{
SRet = exp. Message;
}
Return sRet;
}
/// <Summary>
/// Read the file content in the physical location
/// </Summary>
/// <Param name = "as_filename"> </param>
/// <Returns> </returns>
Public static byte [] ReadFile (string as_filename)
{
Byte [] tmpData = null;
If (as_filename = "")
{
Return tmpData;
}
Byte [] tmpAddData = new byte [1024*1024];
Int intNum = 0;
Int intReadLen = 0;
Long lngLen = 0;
System. IO. FileStream objFile = new System. IO. FileStream (as_filename,
System. IO. FileMode. Open, System. IO. FileAccess. Read, System. IO. FileShare. Read );
System. IO. BinaryReader objSR = new System. IO. BinaryReader (objFile, System. Text. Encoding. Unicode );
TmpData = new byte [objSR. BaseStream. Length];
While (true)
{
IntReadLen = objSR. Read (tmpAddData, 0, tmpAddData. Length );
Array. Copy (tmpAddData, 0, tmpData, lngLen, intReadLen );
LngLen = lngLen + intReadLen;
IntNum = intNum + 1;
If (intReadLen <= 0)
{
Break;
}
}
ObjSR. Close ();
ObjSR = null;
ObjFile. Close ();
ObjFile = null;
Return tmpData;
}
/// <Summary>
/// Read the content of the file in the physical location
/// </Summary>
/// <Param name = "as_filename"> </param>
/// <Param name = "as_FileData"> </param>
/// <Returns> </returns>
Public static string ReadFile (string as_filename, out byte [] as_FileData)
{
String sRet = "";
As_FileData = null;
Byte [] tmpData = null;
If (as_filename = "")
{
SRet = "specify the path and file name ";
Return sRet;
}
Try
{
Byte [] tmpAddData = new byte [1024*1024];
Int intNum = 0;
Int intReadLen = 0;
Long lngLen = 0;
System. IO. FileStream objFile = new System. IO. FileStream (as_filename,
System. IO. FileMode. Open, System. IO. FileAccess. Read, System. IO. FileShare. Read );
System. IO. BinaryReader objSR = new System. IO. BinaryReader (objFile, System. Text. Encoding. Unicode );
TmpData = new byte [objSR. BaseStream. Length];
While (true)
{
IntReadLen = objSR. Read (tmpAddData, 0, tmpAddData. Length );
Array. Copy (tmpAddData, 0, tmpData, lngLen, intReadLen );
LngLen = lngLen + intReadLen;
IntNum = intNum + 1;
If (intReadLen <= 0)
{
Break;
}
}
ObjSR. Close ();
ObjSR = null;
ObjFile. Close ();
ObjFile = null;
As_FileData = tmpData;
}
Catch (Exception ex)
{
SRet = ex. Message. ToString ();
As_FileData = null;
}
Return sRet;
}

/// <Summary>
/// Read the file content from the physical location
/// </Summary>
/// <Param name = "as_filename"> </param>
/// <Param name = "asFileData"> </param>
/// <Returns> </returns>
Public static string ReadFile (string as_filename, out string as_FileData)
{
String sRet = "";
As_FileData = "";
Try
{
If (as_filename = "")
{
SRet = "specify the path and file name ";
Return sRet;
}
Byte [] tmpData;
Byte [] tmpAddData = new byte [1024*1024];
Int intNum = 0;
Int intReadLen = 0;
Long lngLen = 0;
System. IO. FileStream objFile = new System. IO. FileStream (as_filename,
System. IO. FileMode. Open, System. IO. FileAccess. Read, System. IO. FileShare. Read );
System. IO. BinaryReader objSR = new System. IO. BinaryReader (objFile, System. Text. Encoding. Unicode );
TmpData = new byte [objSR. BaseStream. Length];
While (true)
{
IntReadLen = objSR. Read (tmpAddData, 0, tmpAddData. Length );
Array. Copy (tmpAddData, 0, tmpData, lngLen, intReadLen );
LngLen = lngLen + intReadLen;
IntNum = intNum + 1;
If (intReadLen <= 0)
{
Break;
}
}
As_FileData = System. Text. Encoding. Unicode. GetString (tmpData );
ObjSR. Close ();
ObjSR = null;
ObjFile. Close ();
ObjFile = null;
SRet = "";
}
Catch (Exception eRead)
{
SRet = eRead. Message. ToString ();
As_FileData = "";
}
Return sRet;
}
/// <Summary>
/// Write the file content to the physical location
/// </Summary>
/// <Param name = "as_filename"> </param>
/// <Param name = "as_FileData"> </param>
/// <Returns> </returns>
Public static string WriteFile_UTF8 (string as_filename, string as_FileData)
{
String sRet = "";
Try
{
If (as_filename = "")
{
SRet = "specify the path and file name ";
Return sRet;
}
System. IO. FileStream objFile = new System. IO. FileStream (as_filename,
System. IO. FileMode. OpenOrCreate, System. IO. FileAccess. ReadWrite, System. IO. FileShare. ReadWrite );
System. IO. BinaryWriter objSW = new BinaryWriter (objFile, System. Text. Encoding. UTF8 );
Byte [] tmpData = System. Text. Encoding. UTF8.GetBytes (as_FileData );
ObjSW. BaseStream. Write (tmpData, 0, tmpData. Length );
ObjSW. Close ();
ObjSW = null;
ObjFile. Close ();
ObjFile = null;
SRet = "";
}
Catch (Exception exp)
{
SRet = exp. Message;
}
Return sRet;
}

# Endregion
}
}

 

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.