# Region reading CSV files
/// <Summary>
/// Read the CVS file
/// </Summary>
/// <Param name = "path"> file path </param>
/// <Param name = "name"> file name </param>
/// <Returns> datatable </returns>
Public static datatable readcvs (string filepath, string filename)
{
// String cvsdir = filepath; // CVS path to be read
Datatable dt = new datatable ();
If (filename. Trim (). toupper (). endswith ("CSV") // determine the extension to be read
{
String connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source ="
+ Filepath + "; extended properties = 'text; HDR = no; FMt = delimited'"; // read with columns
String commandtext = "select * from [" + filename + "]"; // SQL statement
Oledbconnection olconn = new oledbconnection (connstr );
Olconn. open ();
Oledbdataadapter ODP = new oledbdataadapter (commandtext, olconn );
ODP. Fill (DT );
Olconn. Close ();
ODP. Dispose ();
Olconn. Dispose ();
}
Return DT;
}
# Endregion
# Region reading XLS files
/// <Summary>
/// Read the Excel file
/// </Summary>
/// <Param name = "filepath"> file path </param>
/// <Param name = "FILENAME"> file name </param>
/// <Returns> datatable </returns>
Public static datatable readexcel (string filepath, string filename)
{
Datatable dt = new datatable ();
If (filename. Trim (). toupper (). endswith ("xls "))
{
String connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source ="
+ Filepath + "; extended properties = 'excel 8.0; HDR = no; IMEX = 1 '";
String commandtext = "select * from [" + filename + "]";
Oledbconnection olconn = new oledbconnection (connstr );
Olconn. open ();
Oledbdataadapter ODP = new oledbdataadapter (commandtext, olconn );
ODP. Fill (DT );
Olconn. Close ();
ODP. Dispose ();
Olconn. Dispose ();
}
Return DT;
}
# Endregion
# Region read TXT files
/// <Summary>
/// Read the TXT text file
/// </Summary>
/// <Param name = "filepath"> file path </param>
/// <Param name = "FILENAME"> file name </param>
/// <Returns> Text Information </returns>
Public static string readtxt (string filepath, string filename)
{
Stringbuilder sb = new stringbuilder ("");
// Streamreader sr = new streamreader (filepath + filename );;
Streamreader sr = new streamreader (filepath + filename, encoding. getencoding ("gb2312 "));
String line;
While (line = Sr. Readline ())! = NULL)
{
SB. appendline (line );
}
Sr. Close ();
Sr. Dispose ();
Return sb. tostring ();
}
# Endregion
# Endregion
# Delete a region File
/// <Summary>
/// Delete a file
/// </Summary>
/// <Param name = "filepath"> file path </param>
/// <Param name = "FILENAME"> file name </param>
Public static void deletefile (string filepath, string filename)
{
String destinationfile = filepath + filename;
// Delete an object if the object exists
If (file. exists (destinationfile ))
{
Fileinfo Fi = new fileinfo (destinationfile );
If (Fi. Attributes. tostring (). indexof ("readonly ")! =-1)
Fi. Attributes = fileattributes. normal;
File. Delete (destinationfile );
}
}
# Endregion
///
// copy a file
///
/// file path
// path to which the file is to be copied
private bool copyfile (string fromfilepath, string tofilepath)
{< br> try
{< br> If (file. exists (fromfilepath)
{< br> If (file. exists (tofilepath)
{< br> file. delete (tofilepath);
}< br> file. move (fromfilepath, tofilepath);
return true;
}< br> return false;
}< br> catch
{< br> return false;
}< BR >}
# Region reading CSV files
/// <Summary>
/// Read the CVS file
/// </Summary>
/// <Param name = "path"> file path </param>
/// <Param name = "name"> file name </param>
/// <Returns> datatable </returns>
Public static datatable readcvs (string filepath, string filename)
{
// String cvsdir = filepath; // CVS path to be read
Datatable dt = new datatable ();
If (filename. Trim (). toupper (). endswith ("CSV") // determine the extension to be read
{
String connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source ="
+ Filepath + "; extended properties = 'text; HDR = no; FMt = delimited'"; // read with columns
String commandtext = "select * from [" + filename + "]"; // SQL statement
Oledbconnection olconn = new oledbconnection (connstr );
Olconn. open ();
Oledbdataadapter ODP = new oledbdataadapter (commandtext, olconn );
ODP. Fill (DT );
Olconn. Close ();
ODP. Dispose ();
Olconn. Dispose ();
}
Return DT;
}
# Endregion
# Region reading XLS files
/// <Summary>
/// Read the Excel file
/// </Summary>
/// <Param name = "filepath"> file path </param>
/// <Param name = "FILENAME"> file name </param>
/// <Returns> datatable </returns>
Public static datatable readexcel (string filepath, string filename)
{
Datatable dt = new datatable ();
If (filename. Trim (). toupper (). endswith ("xls "))
{
String connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source ="
+ Filepath + "; extended properties = 'excel 8.0; HDR = no; IMEX = 1 '";
String commandtext = "select * from [" + filename + "]";
Oledbconnection olconn = new oledbconnection (connstr );
Olconn. open ();
Oledbdataadapter ODP = new oledbdataadapter (commandtext, olconn );
ODP. Fill (DT );
Olconn. Close ();
ODP. Dispose ();
Olconn. Dispose ();
}
Return DT;
}
# Endregion
# Region read TXT files
/// <Summary>
/// Read the TXT text file
/// </Summary>
/// <Param name = "filepath"> file path </param>
/// <Param name = "FILENAME"> file name </param>
/// <Returns> Text Information </returns>
Public static string readtxt (string filepath, string filename)
{
Stringbuilder sb = new stringbuilder ("");
// Streamreader sr = new streamreader (filepath + filename );;
Streamreader sr = new streamreader (filepath + filename, encoding. getencoding ("gb2312 "));
String line;
While (line = Sr. Readline ())! = NULL)
{
SB. appendline (line );
}
Sr. Close ();
Sr. Dispose ();
Return sb. tostring ();
}
# Endregion
# Endregion
# Delete a region File
/// <Summary>
/// Delete a file
/// </Summary>
/// <Param name = "filepath"> file path </param>
/// <Param name = "FILENAME"> file name </param>
Public static void deletefile (string filepath, string filename)
{
String destinationfile = filepath + filename;
// Delete an object if the object exists
If (file. exists (destinationfile ))
{
Fileinfo Fi = new fileinfo (destinationfile );
If (Fi. Attributes. tostring (). indexof ("readonly ")! =-1)
Fi. Attributes = fileattributes. normal;
File. Delete (destinationfile );
}
}
# Endregion
///
// copy a file
///
/// file path
// path to which the file is to be copied
private bool copyfile (string fromfilepath, string tofilepath)
{< br> try
{< br> If (file. exists (fromfilepath)
{< br> If (file. exists (tofilepath)
{< br> file. delete (tofilepath);
}< br> file. move (fromfilepath, tofilepath);
return true;
}< br> return false;
}< br> catch
{< br> return false;
}< BR >}