using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
namespace wsControls.IO
{
public class wsFile
{
public const char tab = (char)178;
public const string space = "\t";
public const char eof = (char)13;
public const byte CR = 13;
public const string DateFmt = "yyyy-MM-dd HH:mm:ss.ff";
public const int MaxHeaderLength = 1024;
#region [ Text ] AppendText, WriteFile
public static bool AppendText(string filename, string text)
{
try
{
System.IO.StreamWriter file = File.AppendText(filename);
file.WriteLine(text);
file.Flush();
file.Close();
file.Dispose();
return true;
}
catch { return false; }
}
public static bool WriteFile(string filename, string text)
{
try
{
System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
file.WriteLine(text);
file.Close();
file.Flush();
file.Dispose();
return true;
}
catch { return false; }
}
#endregion
public static bool WriteBuffer(string filename, byte[] buffer)
{
try
{
BinaryWriter bw = new BinaryWriter(System.IO.File.Open(filename, FileMode.Create), new UTF8Encoding());
bw.Write(buffer);
bw.Close();
return true;
}
catch { return false; }
}
#region [ Write BXF ] CreateBXF, AppendBXF, CloseBXF
public static bool CreateBXF(string filename, out BinaryWriter bw)
{
bw = null;
try
{
bw = new BinaryWriter(System.IO.File.Open(filename, FileMode.Create), new UTF8Encoding());
return true;
}
catch { return false; }
}
public static bool AppendBXF(BinaryWriter bw, string text)
{
try
{
bw.Write(text);
return true;
}
catch { return false; }
}
public static bool AppendBXF(BinaryWriter bw, decimal data)
{
try
{
bw.Write(data);
return true;
}
catch { return false; }
}
public static bool AppendBXF(BinaryWriter bw, string text, decimal data)
{
try
{
bw.Write(text);
bw.Write(data);
return true;
}
catch { return false; }
}
public static bool AppendBXF(BinaryWriter bw, DateTime datetime, decimal data)
{
try
{
bw.Write(datetime.ToString(DateFmt));
bw.Write(data);
return true;
}
catch { return false; }
}
public static bool AppendBXF(BinaryWriter bw, string[] text, decimal[] data)
{
try
{
for (int i = 0; i < text.Length; i++)
{
bw.Write(text[i]);
bw.Write(data[i]);
}
return true;
}
catch { return false; }
}
public static bool AppendBXF(BinaryWriter bw, float[] data)
{
try
{
bw.Write(data.Length.ToString());
for (int i = 0; i < data.Length; i++)
bw.Write(data[i]);
return true;
}
catch { return false; }
}
public static bool AppendBXF(BinaryWriter bw, string text, float[] data)
{
try
{
bw.Write(text);
bw.Write(data.Length.ToString());
for (int i = 0; i < data.Length; i++)
bw.Write(data[i]);
return true;
}
catch { return false; }
}
public static bool AppendBXF(BinaryWriter bw, DateTime datetime, float[] data)
{
try
{
bw.Write(datetime.ToString(DateFmt));
bw.Write(data.Length.ToString());
for (int i = 0; i < data.Length; i++)
bw.Write(data[i]);
return true;
}
catch { return false; }
}
public static bool AppendBXF(BinaryWriter bw, DateTime datetime, decimal[] data)
{
try
{
bw.Write(datetime.ToString(DateFmt));
bw.Write(data.Length.ToString());
for (int i = 0; i < data.Length; i++)
bw.Write(data[i]);
return true;
}
catch { return false; }
}
public static bool CloseBXF(BinaryWriter bw)
{
try
{
bw.Flush();
bw.Close();
bw.Dispose();
return true;
}
catch { return false; }
}
#endregion
#region [ Read BXF ] OpenBXF, ReadBXF, CloseBXF
public static bool OpenBXF(string filename, out FileStream fs, out BinaryReader br)
{
fs = null;
br = null;
try
{
fs = new FileStream(filename, FileMode.Open);
br = new BinaryReader(fs, new UTF8Encoding());
return true;
}
catch { return false; }
}
public static bool ReadBXF(BinaryReader br, out string text)
{
try
{
text = br.ReadString();
return true;
}
catch
{
text = string.Empty;
return false;
}
}
public static bool ReadBXF(BinaryReader br, out decimal dec)
{
try
{
dec = br.ReadDecimal();
return true;
}
catch
{
dec = 0;
return false;
}
}
public static bool ReadBXF(BinaryReader br,out string text, out decimal dec)
{
try
{
text = br.ReadString();
dec = br.ReadDecimal();
return true;
}
catch
{
text = string.Empty;
dec = 0;
return false;
}
}
public static bool ReadBXF(BinaryReader br, out DateTime datetime, out decimal dec)
{
try
{
datetime = Convert.ToDateTime(br.ReadString());
dec = br.ReadDecimal();
return true;
}
catch
{
datetime = new DateTime();
dec = 0;
return false;
}
}
public static bool ReadBXF(BinaryReader br, out float[] data)
{
try
{
string len = br.ReadString();
data = new float[Convert.ToInt32(len)];
for (int i = 0; i < data.Length; i++)
data[i] = br.ReadSingle();
return true;
}
catch
{
data = null;
return false;
}
}
public static bool ReadBXF(BinaryReader br, out string text, out float[] data)
{
try
{
text = br.ReadString();
string len = br.ReadString();
data = new float[Convert.ToInt32(len)];
for (int i = 0; i < data.Length; i++)
data[i] = br.ReadSingle();
return true;
}
catch
{
text = string.Empty;
data = null;
return false;
}
}
public static bool ReadBXF(BinaryReader br, out DateTime datetime, out float[] data)
{
try
{
datetime = Convert.ToDateTime(br.ReadString());
string len = br.ReadString();
data = new float[Convert.ToInt32(len)];
for (int i = 0; i < data.Length; i++)
data[i] = br.ReadSingle();
return true;
}
catch
{
datetime = new DateTime();
data = null;
return false;
}
}
public static bool ReadBXF(BinaryReader br, out DateTime datetime, out decimal[] data)
{
try
{
datetime = Convert.ToDateTime(br.ReadString());
string len = br.ReadString();
data = new decimal[Convert.ToInt32(len)];
for (int i = 0; i < data.Length; i++)
data[i] = br.ReadDecimal();
return true;
}
catch
{
datetime = new DateTime();
data = null;
return false;
}
}
public static bool CloseBXF(FileStream fs, BinaryReader br)
{
try
{
br.Close();
fs.Close();
return true;
}
catch { return false; }
}
#endregion
public static bool WriteTXT(string filename, string content)
{
try
{
System.IO.StreamWriter sfile = new System.IO.StreamWriter(filename);
sfile.WriteLine(content);
sfile.Close();
sfile.Dispose();
return true;
}
catch { return false; }
}
}
}
wsFile
最新推荐文章于 2024-08-27 18:49:12 发布