C#学习笔记汇总——持续更新

C#学习笔记汇总目录列表——持续更新

  1. C#定时器、计时器
  2. C#获取系统时间及格式化时间
  3. C#ListBox使用介绍
  4. C#DataGridView使用
  5. C#获取程序路径以及系统环境变量
  6. C# 读取xml
  7. C#进制转换
  8. C#实现经纬度转换,大地坐标系转为火星坐标系
  9. C#排序算法
  10. C#实现窗口间消息通信
  11. C#实现异步socket通信

1 跨线程安全调用控件

方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
///
/// 确保线程安全
///
///
private delegate void AddItemsCallback(string text, int i);
 
private void AddItems(string text, int i)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.listBoxInstruction.InvokeRequired)
{
AddItemsCallback d = new AddItemsCallback(AddItems);
this.Invoke(d, new object[] { text, i });
}
else
{
if (i == 0)
{ }
else
{ }
}
}

WPF中使用匿名委托安全的跨线程后台更新UI界面

1
2
3
4
this.Dispatcher.BeginInvoke(DispatcherPriority.Background,(Action)(() =>
{
//在这里执行界面数据更新操作
}));

2 根据控件名称获取控件

1
2
Control col = this.GroupBox1.Controls.Find("控件名", true)[0];
Button btn = col as Button;//转为Button

3 文件编码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
///
/// 获取文件的编码格式
///
public class EncodingType
{
///
/// 给定文件的路径,读取文件的二进制数据,判断文件的编码类型
///
/// 文件路径
/// 文件的编码类型
public static System.Text.Encoding GetType(string FILE_NAME)
{
FileStream fs = new FileStream(FILE_NAME, 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
Encoding reVal = Encoding.Default;
 
BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
int i;
int.TryParse(fs.Length.ToString(), out i);
byte[] ss = r.ReadBytes(i);
if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
{
reVal = Encoding.UTF8;
}
else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
{
reVal = Encoding.BigEndianUnicode;
}
else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
{
reVal = Encoding.Unicode;
}
r.Close();
return reVal;
}
 
///
/// 判断是否是不带 BOM 的 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;
}
}

C# 字符串转为变量名,通过字符串给变量赋值

将字符串转为变量名:
1
2
3
4
5
6
string str = "spp";
public string spp = "very good";
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.GetType().GetField(str).GetValue(this).ToString());
}
通过字符串给变量赋值:
1
2
3
4
5
6
7
8
9
10
11
public string gisoracle = "ok";
 
private void button2_Click(object sender, EventArgs e)
{
//通过字符串获得变量值
MessageBox.Show(this.GetType().GetField("gisoracle").GetValue(this).ToString());
//通过给变量赋值
this.GetType().GetField("gisoracle").SetValue(this, "[email protected]");
//新的值
MessageBox.Show(this.GetType().GetField("gisoracle").GetValue(this).ToString());
}

使用正则表达式分割字符串

1
2
string[] str = System.Text.RegularExpressions.Regex.Split(temp, @"[ ]+",
             System.Text.RegularExpressions.RegexOptions.IgnoreCase);

C#中的移位、异或、与、或运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
///
/// 移位运算            
///
int i = 7;
int j = 2;
Console.WriteLine(i >> j);   //输出结果为1
 
///
/// 异或运算
///
int x = 5;
int y = 3;
y ^= x;   //等价与y=y^x
Console.WriteLine(y);        ////输出结果为6
 
///
/// 或运算            
///
int m = 7;
int n = 2;
Console.WriteLine(m | n);   //输出结果为7
 
///
/// 与运算            
///
int p = 7;
int q = 2;
Console.WriteLine(p & q);   //输出结果为2

C#遍历指定文件夹中的所有文件和文件夹

1
2
3
4
5
6
7
DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);
//遍历文件夹
foreach(DirectoryInfo NextFolder in TheFolder.GetDirectories())
   this.listBox1.Items.Add(NextFolder.Name);
//遍历文件
foreach(FileInfo NextFile in TheFolder.GetFiles())
   this.listBox2.Items.Add(NextFile.Name);

获取指定目录包含的文件和子目录:

1
2
3
1. DirectoryInfo.GetFiles();//获取目录中(不包含子目录)的文件,返回类型为FileInfo[],支持通配符查找;
2. DirectoryInfo.GetDirectories();//获取目录(不包含子目录)的子目录,返回类型为DirectoryInfo[],支持通配符查找;
3. DirectoryInfo. GetFileSystemInfos();//获取指定目录下(不包含子目录)的文件和子目录,返回类型为FileSystemInfo[],支持通配符查找;

获取指定文件的基本信息:

1
2
3
4
5
6
7
8
9
FileInfo.Exists:获取指定文件是否存在;
FileInfo.Name,FileInfo.Extensioin:获取文件的名称和扩展名;
FileInfo.FullName:获取文件的全限定名称(完整路径);
FileInfo.Directory:获取文件所在目录,返回类型为DirectoryInfo;
FileInfo.DirectoryName:获取文件所在目录的路径(完整路径);
FileInfo.Length:获取文件的大小(字节数);
FileInfo.IsReadOnly:获取文件是否只读;
FileInfo.Attributes:获取或设置指定文件的属性,返回类型为FileAttributes枚举,可以是多个值的组合
FileInfo.CreationTime、FileInfo.LastAccessTime、FileInfo.LastWriteTime:分别用于获取文件的创建时间、访问时间、修改时间;

 

 

你可能感兴趣的:(C#)