C#实用的代码

  创建xml对应的对象类
  根节点,对应类名
  [XmlRoot("ComponentLog")]
  publicclassComponentLog{
  }
  其他节点,对应属性名
  [XmlElement("LogCategory")]
  publicstringlogCategory{get;set;}
  也可以对应集合(如果同一节点有多个的话)
  [XmlElement("LogContent")]
  publicList<LogContent>logContent{get;set;}
  节点里的内容
  [XmlAttribute("Content")]
  publicstringcontent{get;set;}
  XML文件:
  <?xmlversion="1.0"encoding="utf-8"?>
  <ComponentLog>
  <LogCategory>Sign</LogCategory>
  <LogContent>
  <Key>1</Key>
  <ContentCaptionContent="内容1"VariableName=""/>
  <ContentDetailContent="内容2"VariableName=""/>
  </LogContent>
  <LogContent>
  <Key>2</Key>
  <ContentCaptionContent="内容3"VariableName=""/>
  <ContentDetailContent="内容4"VariableName=""/>
  </LogContent>
  </ComponentLog>
  窗体中打开文件夹
  FolderBrowserDialogfolderBrowser=newFolderBrowserDialog();
  if(folderBrowser.ShowDialog()==DialogResult.OK)
  {
  txtFolderPath.Text=folderBrowser.SelectedPath;
  }
  窗体中跨线程调用组件(控件)
  ///<paramname="textBox">文本框</param>
  ///<paramname="strText">要显示的内容</param>
  privatevoidShowText(TextBoxtextBox,StringstrText)
  {
  if(this.InvokeRequired)
  {
  this.Invoke((MethodInvoker)delegate(){ShowText(textBox,strText+"\r\n");});
  }
  else
  {
  textBox.Text+=DateTime.Now+""+strText+"\r\n";
  }
  }
  关闭窗口,退出所有进程
  privatevoidForm1_FormClosed(objectsender,FormClosedEventArgse)
  {
  System.Environment.Exit(0);
  }
  将文本框的滚动条一直处于最低端
  privatevoidtxtReceive_TextChanged(objectsender,EventArgse)
  {
  txtReceive.SelectionStart=txtReceive.Text.Length;
  txtReceive.ScrollToCaret();
  }
  连接字符串
  //str1不为空,就将str1和“”连接
  stringjournalString=str1!=string.Empty?string.Concat(str1,""):string.Empty;
  获得程序运行目录下指定文件的路径
  stringxmlPath=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"JournalLog\\123.xml");
  获取指定的编码格式
  Encodinggb2312=Encoding.GetEncoding("GB2312");
  按照指定编码格式读取文本内容
  stringstrRead=File.ReadAllText(xmlPath,Encoding.Default);
  按照指定编码格式转换已经读取到的文本内容
  //sendByte是字节,将其转换成string
  stringstrSendData=gb2312.GetString(sendByte);
  或者stringstrSendData=Encoding.UTF8.GetString(sendByte);

posted @ 2020-06-08 16:18  老猿一只  阅读(257)  评论(0编辑  收藏  举报
超越台 网站制作 软件开发