110,534
社区成员
发帖
与我相关
我的任务
分享
public static class StreamHelper
{
public static Encoding GetEncoding(Stream stream, Encoding defaultEncoding)
{
Encoding targetEncoding = defaultEncoding;
if (stream == null) return targetEncoding;
if (stream.Length < 3) return targetEncoding;
byte byte1, byte2, byte3;
//保存当前Seek位置
long origPos = stream.Seek(0, SeekOrigin.Begin);
byte1 = Convert.ToByte(stream.ReadByte());
byte2 = Convert.ToByte(stream.ReadByte());
byte3 = Convert.ToByte(stream.ReadByte());
//根据文件流的前3个字节判断Encoding
if (byte1 == 0xFE && byte2 == 0xFF)//UnicodeBe
{
targetEncoding = Encoding.BigEndianUnicode;
}
if (byte1 == 0xFF && byte2 == 0xFE && byte3 != 0xFF)//Unicode
{
targetEncoding = Encoding.Unicode;
}
if (byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF)//UTF8
{
targetEncoding = Encoding.UTF8;
}
//恢复Seek位置
stream.Seek(origPos, SeekOrigin.Begin);
return targetEncoding;
}
}
private void InitComboBox()
{
List<string> ServerNames = new List<string>();
ServerNames = this.LoadName();
this.cmbServer.Items.AddRange(ServerNames.ToArray());
this.cmbCategory.Items.AddRange(new string[] { "Error", "Exception", "Info" });
}
private void SetSavePath(ref string savePath)
{
FolderBrowserDialog FolderPath = new FolderBrowserDialog();
FolderPath.ShowDialog();
savePath = FolderPath.SelectedPath;
}
private void btnDownloadBatch_Click(object sender, EventArgs e)
{
DateTime dt = this.dtpStart.Value;
string savePath = String.Empty;
Server = this.cmbServer.Text;
Path = this.txtPath.Text + @"\Log\" + cmbCategory.Text;
if (!Directory.Exists(Path)) { MessageBox.Show("路径不存在"); return; }
SetSavePath(ref savePath);
//下面那两句防止间隔小于24小时但跨越两天才算一天的情况
DateTime start = new DateTime(this.dtpStart.Value.Year, this.dtpStart.Value.Month, this.dtpStart.Value.Day, 0, 0, 0);
DateTime end = new DateTime(this.dtpEnd.Value.Year, this.dtpEnd.Value.Month, this.dtpEnd.Value.Day, 23, 59, 59);
TimeSpan ts = end - start;
double dDays = ts.TotalDays;//带小数的天数,比如1天12小时结果就是1.5
int DateCount = (int)Math.Ceiling(dDays);//返回有效数就进一的整数
this.progressBar1.Value = 0;
DownLoadBatch(dt, end, DateCount, savePath);
}