• C#中的几个实用的代码


    640?wx_fmt=jpeg

    创建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+" "); });

                }

                else

                {

                    textBox.Text += DateTime.Now + "   " + strText+" ";

                }

            }

    关闭窗口,退出所有进程

            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);


  • 相关阅读:
    AJAX异步传输——以php文件传输为例
    js控制json生成菜单——自制菜单(一)
    vs2010中关于HTML控件与服务器控件分别和js函数混合使用的问题
    SQL数据库连接到服务器出错——无法连接到XXX
    PHP错误:Namespace declaration statement has to be the very first statement in the script
    【LeetCode】19. Remove Nth Node From End of List
    【LeetCode】14. Longest Common Prefix
    【LeetCode】38. Count and Say
    【LeetCode】242. Valid Anagram
    【LeetCode】387. First Unique Character in a String
  • 原文地址:https://www.cnblogs.com/hgmyz/p/12351810.html
Copyright © 2020-2023  润新知