C#中的几个简单技术点
创建xml对应的对象类
根节点,对应类名
[xmlroot("componentlog ")]
public class componentlog{
}
其他节点,对应属性名
[xmlelement("logcategory")]
public string logcategory { get; set; }
也可以对应集合(如果同一节点有多个的话)
[xmlelement("logcontent")]
public list<logcontent> logcontent { get; set; }
节点里的内容
[xmlattribute("content")]
public string content { get; set; }
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<componentlog>
<logcategory>sign</logcategory>
<logcontent>
<key>1</key>
<contentcaption content="内容1" variablename=""/>
<contentdetail content="内容2" variablename="" />
</logcontent>
<logcontent>
<key>2</key>
<contentcaption content="内容3" variablename=""/>
<contentdetail content="内容4" variablename="" />
</logcontent>
</componentlog>
窗体中打开文件夹
folderbrowserdialog folderbrowser = new folderbrowserdialog();
if (folderbrowser.showdialog() == dialogresult.ok)
{
txtfolderpath.text = folderbrowser.selectedpath;
}
窗体中跨线程调用组件(控件)
/// <param name="textbox">文本框</param>
/// <param name="strtext">要显示的内容</param>
private void showtext(textbox textbox, string strtext)
{
if (this.invokerequired)
{
this.invoke((methodinvoker)delegate () { showtext(textbox, strtext+"\r\n"); });
}
else
{
textbox.text += datetime.now + " " + strtext+"\r\n";
}
}
关闭窗口,退出所有进程
private void form1_formclosed(object sender, formclosedeventargs e)
{
system.environment.exit(0);
}
将文本框的滚动条一直处于最低端
private void txtreceive_textchanged(object sender, eventargs e)
{
txtreceive.selectionstart = txtreceive.text.length;
txtreceive.scrolltocaret();
}
连接字符串
//str1不为空,就将str1和“ ”连接
string journalstring = str1 != string.empty ? string.concat(str1, " ") : string.empty;
获得程序运行目录下指定文件的路径
string xmlpath = path.combine(appdomain.currentdomain.basedirectory, "journallog\\123.xml");
获取指定的编码格式
encoding gb2312 = encoding.getencoding("gb2312");
按照指定编码格式读取文本内容
string strread = file.readalltext(xmlpath,encoding.default);
按照指定编码格式转换已经读取到的文本内容
//sendbyte是字节,将其转换成string
string strsenddata = gb2312.getstring(sendbyte);
或者string strsenddata = encoding.utf8.getstring(sendbyte);
上一篇: ie下警告console未定义