using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GeoSigma;
using ScintillaNET;
using WeifenLuo.WinFormsUI.Docking;
namespace LibTest
{
public partial class FormTest : Form
{
private string dockDefaultPath = "";
private string dockPanelPath = "";
private FrmLayers frmLayers;
private FrmProperty frmProperty;
private FrmOutput frmOutput;
private FrmDoc frmDoc;
private readonly ToolStripRenderer _toolStripProfessionalRenderer = new ToolStripProfessionalRenderer();
private Scintilla scintilla;
private DockPanel dockPanel;
public FormTest()
{
InitializeComponent();
//AutoScaleMode = AutoScaleMode.Dpi;
////dockPanel = this.dockPanelTest1.MainPanel;
////dockPanel = dockPanel1;
//vsToolStripExtender1.DefaultRenderer = _toolStripProfessionalRenderer;
//SetSchema();
//this.scintilla = new Scintilla();
//this.Controls.Add(this.scintilla);
//this.scintilla.BorderStyle = BorderStyle.None;
//this.scintilla.Width = 300;
//this.scintilla.Height = 20;
//scintilla.Location = new Point(200, 200);
//this.scintilla.VScrollBar = false;
//this.scintilla.HScrollBar = false;
//TestClass test = new TestClass();
}
private void FormTest_Load(object sender, EventArgs e)
{
//if (dockPanel != null)
//{
// this.dockPanel.DocumentStyle = DocumentStyle.DockingMdi;
// this.dockDefaultPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockDefault.config");
// this.dockPanelPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
// //this.InitDockPanel();
// this.tssStatus.Text = "就绪";
//}
}
private void button1_Click(object sender, EventArgs e)
{
//FrmSigmaColor frmColor = new FrmSigmaColor();
//frmColor.SelectedColor = ((Button)sender).BackColor;
//if (frmColor.ShowDialog() == DialogResult.OK)
//{
// this.button1.BackColor = frmColor.SelectedColor;
//}
string strFile = @"C:\temp\TestWeb\ceshi501.dfd";
Encoding encoding = EncodingType.GetType(strFile);
//Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);// 使用了gb2312 需要定义
// Encoding encoding = Encoding.GetEncoding("GB2312");
string strContent = File.ReadAllText(strFile, Encoding.Default);
encoding = Encoding.Default;
string strContent2 = ReadFileAsString(strFile, encoding);
}
public string ReadFileAsString(string filePath, Encoding encoding)
{
if (!File.Exists(filePath))
{
return string.Empty;
}
using (StreamReader reader = new StreamReader(filePath, encoding))
{
// fileInfo.OpenText();
return reader.ReadToEnd();
}
}
public Encoding GetEncoding(string filePath)
{
if (filePath == null)
{
throw new ArgumentNullException("filePath");
}
Encoding encoding1 = Encoding.Default;
if (File.Exists(filePath))
{
try
{
using (FileStream stream1 = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
if (stream1.Length > 0)
{
using (StreamReader reader1 = new StreamReader(stream1, true))
{
char[] chArray1 = new char[1];
reader1.Read(chArray1, 0, 1);
encoding1 = reader1.CurrentEncoding;
reader1.BaseStream.Position = 0;
if (encoding1 == Encoding.UTF8)
{
byte[] buffer1 = encoding1.GetPreamble();
if (stream1.Length >= buffer1.Length)
{
byte[] buffer2 = new byte[buffer1.Length];
stream1.Read(buffer2, 0, buffer2.Length);
for (int num1 = 0; num1 < buffer2.Length; num1++)
{
if (buffer2[num1] != buffer1[num1])
{
encoding1 = Encoding.Default;
break;
}
}
}
else
{
encoding1 = Encoding.Default;
}
}
}
}
}
}
catch (Exception exception1)
{
throw;
}
if (encoding1 == null)
{
encoding1 = Encoding.UTF8;
}
}
return encoding1;
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
//richTextBox1.SelectedRtf
}
}
public class EncodingType
{
///
/// 给定文件的路径,读取文件的二进制数据,判断文件的编码类型
///
/// 文件路径
/// 文件的编码类型
public static System.Text.Encoding GetType(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
Encoding r = GetType(fs);
fs.Close();
return r;
}
///
/// 通过给定的文件流,判断文件的编码类型
///
/// 文件流
/// 文件的编码类型
public static System.Text.Encoding GetType(FileStream fs)
{
byte[] unicode = new byte[] { 0xFF, 0xFE, 0x41 };
byte[] unicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
byte[] uTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; // 带BOM
BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
int i;
int.TryParse(fs.Length.ToString(), out i);
byte[] ss = r.ReadBytes(i);
Encoding reVal = GetType(ss);
r.Close();
return reVal;
}
///
/// 获得格式编码
///
/// 数据内容
/// 编码
public static System.Text.Encoding GetType(byte[] data)
{
Encoding reVal = Encoding.Default;
if (IsUTF8Bytes(data) || (data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF))
{
reVal = Encoding.UTF8;
}
else if (data[0] == 0xFE && data[1] == 0xFF && data[2] == 0x00)
{
reVal = Encoding.BigEndianUnicode;
}
else if (data[0] == 0xFF && data[1] == 0xFE && data[2] == 0x41)
{
reVal = Encoding.Unicode;
}
return reVal;
}
///
/// 判断是否是不带 BOM 的 UTF8 格式
///
/// 数据
/// 是否UTF8
private static bool IsUTF8Bytes(byte[] data)
{
int charByteCounter = 1;
// 计算当前正分析的字符应还有的字节数
byte curByte; // 当前分析的字节.
for (int i = 0; i < data.Length; i++)
{
curByte = data[i];
if (charByteCounter == 1)
{
if (curByte >= 0x80)
{
// 判断当前
while (((curByte <<= 1) & 0x80) != 0)
{
charByteCounter++;
}
// 标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X
if (charByteCounter == 1 || charByteCounter > 6)
{
return false;
}
}
}
else
{
// 若是UTF-8 此时第一位必须为1
if ((curByte & 0xC0) != 0x80)
{
return false;
}
charByteCounter--;
}
}
if (charByteCounter > 1)
{
throw new Exception("非预期的byte格式");
}
return true;
}
}
public class TestClass
{
public Color feildColor;
public Color FeildColor
{
get
{
return feildColor;
}
set
{
feildColor = value;
}
}
}
}