C#中的几个实用的代码

  创建xml对应的对象类
  根节点,对应类名
  [XmlRoot("ComponentLog ")]
  public class ComponentLog{
  }
  其他节点,对应属性名
  [XmlElement("LogCategory")]
  public string logCategory { get; set; }
  也能够对应调集(如果同一节点有多个的话)
  [XmlElement("LogContent")]
  public ListlogContent { get; set; }
  节点里的内容
  [XmlAttribute("Content")]
  public string content { get; set; }
  XML文件:
  Sign
  窗体中打开文件夹
  FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
  if (folderBrowser.ShowDialog() == DialogResult.OK)
  {
  txtFolderPath.Text = folderBrowser.SelectedPath;
  }
  窗体中跨线程调用组件(控件)
  ///文本框
  ///要显示的内容
  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);

posted @ 2020-05-22 15:40  老猿一只  阅读(562)  评论(0编辑  收藏  举报
超越台 网站制作 软件开发