c#手册

字符、字符串 操作

判断字符串是否为空或者不存在

string.IsNullOrEmpty(str);

分割字符串(使用子串分割)

private void button4_Click(object sender, EventArgs e)
        {
            string str = "1ab2ab3";
            string[] lst = str.Split(new string[] { "ab" }, StringSplitOptions.RemoveEmptyEntries);
            MessageBox.Show(lst[0] + "---" + lst[1] + "---" + lst[2]);
        }


遍历文件夹,判断是否有文件名包含指定名称的文件

//遍历文件夹,判断是否有文件名包含指定名称的文件
        private bool TraverseFolderJudgeFile(string pDirName, string pFileName)
        {
            if (!Directory.Exists(pDirName)) return false;
            DirectoryInfo df = new DirectoryInfo(pDirName);//@"C:\new"
            FileInfo[] fileinfos = df.GetFiles();
            List<string> fileLst = new List<string>();
            foreach (FileInfo tmp in fileinfos)
            {
                fileLst.Add(tmp.FullName);
                if (tmp.FullName.Contains(pFileName)) return true;
            }

            return false;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            MessageBox.Show(TraverseFolderJudgeFile(@"C:\new", "rar1").ToString());
        }


获取程序本地目录




json操作

LitJson

使用前准备:

①添加LitJson引用

②using LitJson;




实体转json格式字符串

实体:
namespace JsonTest
{
    class TimeEntity
    {
        public int hour { get; set; }
        public int minute { get; set; }
        public int secind { get; set; }
    }

    class LitJsonTestEntity
    {
        public int year { get;set;}
        public int month { get; set; }
        public int day { get; set; }
        public List<TimeEntity >timeEntitys { get; set; }
    }
}
方法:
        /// <summary>
        /// 实体转json格式字符串
        /// </summary>
        /// <param name="pLitJsonTestEntity"></param>
        /// <returns></returns>
        private string Entity2Str(LitJsonTestEntity pLitJsonTestEntity)
        {
            if (pLitJsonTestEntity == null) return null;
            return JsonMapper.ToJson(pLitJsonTestEntity);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = Entity2Str(litJsonTestEntity);
            MessageBox.Show(str);
        }




json格式字符串 转 json对象

/// <summary>
        /// json格式字符串 转 json对象
        /// </summary>
        /// <param name="pJsonStr"></param>
        /// <returns></returns>
        private JsonData Str2JsonObj(string pJsonStr)
        {
            if (string.IsNullOrEmpty(pJsonStr)) return null;
            return JsonMapper.ToObject(pJsonStr);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string str = "{\"year\":2015,\"month\":1,\"day\":1,\"timeEntitys\":[{\"hour\":1,\"minute\":1,\"secind\":1},{\"hour\":2,\"minute\":2,\"secind\":2}]}";
            JsonData jsd = Str2JsonObj(str);
            string tmp = "year = " + jsd["year"].ToString() + "\n" +
                               "month = " + jsd["month"].ToString() + "\n" +
                               "hour1 = " + jsd["timeEntitys"][0]["hour"].ToString() + "\n" +
                               "hour2 = " + jsd["timeEntitys"][1]["hour"].ToString();
            MessageBox.Show(tmp);
        }


Json对象 转 json格式字符串

/// <summary>
        /// Json对象 转 json格式字符串
        /// </summary>
        /// <param name="pJsd"></param>
        /// <returns></returns>
        private string JsonObj2Str(JsonData pJsd)
        {
            if (pJsd == null) return null;
            return JsonMapper.ToJson(pJsd);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            JsonData jsd = new JsonData();
            JsonData jsdc1 = new JsonData();
            JsonData jsdc2 = new JsonData();


            jsdc1["child1"] = "child1";
            jsdc2["child2"] = "child2";

            jsd["key"] = "key";
            jsd["array"] = new JsonData();
            jsd["array"].Add(jsdc1);
            jsd["array"].Add(jsdc2);

            string str = JsonObj2Str(jsd);
            MessageBox.Show(str);
        }





功能模块

限制应用同时只能启动一个

bool ret;
            System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
            if (!ret)
            {
                MsgBox.Show("该程序已经运行");
                System.Environment.Exit(0);//退出程序
            }


复制文件

将源目录中所有的文件复制到目标目录中(不包含子目录)

<span style="white-space:pre">	</span>private void CopyFiles(string pSourcePath, string pDestPath)
        {
            if (!Directory.Exists(pSourcePath))
            {
                MessageBox.Show("源目录不存在");
                return;
            }

            if (!Directory.Exists(pDestPath))
            {
                Directory.CreateDirectory(pDestPath);
            }

            DirectoryInfo TheFolder = new DirectoryInfo(pSourcePath);
            foreach (FileInfo NextFile in TheFolder.GetFiles())
            {
                string tmpFileName = pDestPath + "\\" + Path.GetFileName(NextFile.FullName);
                File.Copy(NextFile.FullName, tmpFileName, true);
            }

            MessageBox.Show("复制完毕");

            return;
        }



WinForm原生控件的使用

ComboBox的使用

①实现ComboBox元素要绑定的实体结构
class ComboBoxEntity
    {
        public string text { get; set; }
        public string data { get; set; }
    }
②实现ComboBox和数据的桥梁结构(此处我们叫做:适配器)
 public class ComboBoxItem
    {
        public string Text { get; set; }
        public object Value { get; set; }
        public override string ToString()
        {
            return this.Text;
        }
    }
③添加元素
 private void FrmComboBoxTest_Load(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;//设置下拉列表框只能选择不能编辑
            comboBox1.Items.Clear();
            for (int i = 0; i < 10; i++)
            {
                ComboBoxEntity comboBoxEntity = new ComboBoxEntity();
                comboBoxEntity.text = "text" + i.ToString();
                comboBoxEntity.data = "data" + i.ToString();

                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Text = comboBoxEntity.text;
                cbi.Value = comboBoxEntity;

                comboBox1.Items.Add(cbi);
            }

            comboBox1.SelectedIndex = 0;
        }
④实现SelectedIndexChanged事件
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ComboBox1FirstUse)
            {
                ComboBox1FirstUse = false;
                return;
            }

            string text = ((ComboBoxItem)comboBox1.SelectedItem).Text;
            object value = ((ComboBoxItem)comboBox1.SelectedItem).Value;
            ComboBoxEntity tmp = (ComboBoxEntity)value;

            MessageBox.Show(tmp.data);
        }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值