csharp_learn c#

csharp learn

JSON

C#解析JSON字符串总结 https://www.cnblogs.com/nc923/p/11418583.html

c#解析json字符串处理(最清晰易懂的方法)
https://blog.csdn.net/sajiazaici/article/details/77647625

  • json序列化: 手动生成json字符串。
public class Employees
{
    public string firstName { get; set; }
    public string lastName { get; set; }
}

Employees e1 = new Employees();
e1.firstName = "Ma";
e1.lastName = "Yun";

string str = JsonConvert.SerializeObject(e1);
Console.WriteLine(str);

json反序列化: 获取json内的数据。

public class RootObject
    {
        public string companyID { get; set; }
        public List<Employees> employees { get; set; }
        public List<Manager> manager { get; set; }
    }

 string jsonText = " {\"companyID\":\"15\",\"employees\":[{\"firstName\":\"Bill\",\"lastName\":\"Gates\"},{\"firstName\":\"George\",\"lastName\":\"Bush\"}],\"manager\":[{\"salary\":\"6000\",\"age\":\"23\"},{\"salary\":\"8000\",\"age\":\"26\"}]} ";

Console.WriteLine(jsonText);
Employees rb = JsonConvert.DeserializeObject<Employees>(jsonText);

-- 反序列化的时候遇到问题:
“未将对象引用设置到对象的实例”

  • 获取字符串中的 json字符串
    方法:

    JObject jo = JObject.Parse(jsonText);

字符串

获取字符串子串
string.Substring


项目例子

GIT开源项目地址:https://code.csdn.net/yeqi3000/supernetdebuger

C#通信编程交流群:83868794

线程

线程挂起 ms
Thread.Sleep(2000);

C# 往线程里传参数的方法总结
https://www.cnblogs.com/Liyuting/p/9087216.html

其他

C# 函数返回多个值的方法
https://www.cnblogs.com/masonmei/p/11546377.html

自定义堆栈,实现括号匹配
https://blog.csdn.net/lxy344x/article/details/53613805

String.IndexOf 方法
定义
命名空间:
System
程序集:
mscorlib.dll
报告指定 Unicode 字符或字符串在此实例中的第一个匹配项的从零开始的索引。 如果未在此实例中找到该字符或字符串,则此方法返回 -1。


c# textbox的滚动条总是指向最底端的简单解决方法
更新时间:2017年11月26日 16:20:52 投稿:mrr 我要评论
这篇文章主要介绍了c# textbox的滚动条总是指向最底端的简单解决方法,需要的朋友可以参考下
当我第一次添加滚动条时候,我发现滚动条总是跑向上方,经过研究

解决方案如下:

•this.textBox1.Focus();
•获取焦点
•this.textBox1.Select(this.textBox1.textLength,0);
•选择到最后一行文本
•this.textBox1.ScrollToCaret();
•滑轮滚动到光标处
ps:下面看下c# textBox滚动条一直在最下的解决方法

this.textBox1.Focus();//获取焦点
this.textBox1.Select(this.textBox1.TextLength, 0);//光标定位到文本最后
this.textBox1.ScrollToCaret();//滚动到光标处


C#怎么怎么添加一个现有窗体到工程中

有个一个程序要用到以前的工程里的一个窗体,现在想把它添加到现在的工程里面,可是通过添加现有项,窗体的三个文件Form1.cs、Form1.Designer.cs、Form1.resx文件是并列显示在工程,没有从属关系,添加的窗体里都是空白的,不能正常显示

-----添加的时候只选Form1.cs,不选另外两个就好了。


.NET 中的各种时间格式

DateTime.Now.ToString();            // 2008-9-4 20:02:10
DateTime.Now.ToLocalTime().ToString();        // 2008-9-4 20:12:12

//获取日期
DateTime.Now.ToLongDateString().ToString();    // 2008年9月4日
DateTime.Now.ToShortDateString().ToString();    // 2008-9-4
DateTime.Now.ToString("yyyy-MM-dd");        // 2008-09-04
DateTime.Now.Date.ToString();            // 2008-9-4 0:00:00

//获取时间
DateTime.Now.ToLongTimeString().ToString();    // 20:16:16
DateTime.Now.ToShortTimeString().ToString();    // 20:16
DateTime.Now.ToString("hh:mm:ss");         // 08:05:57
DateTime.Now.TimeOfDay.ToString();         // 20:33:50.7187500

//其他
DateTime.ToFileTime().ToString();        // 128650040212500000
DateTime.Now.ToFileTimeUtc().ToString();    // 128650040772968750
DateTime.Now.ToOADate().ToString();        // 39695.8461709606
DateTime.Now.ToUniversalTime().ToString();    // 2008-9-4 12:19:14

DateTime.Now.Year.ToString();          获取年份    // 2008
DateTime.Now.Month.ToString();      获取月份    // 9
DateTime.Now.DayOfWeek.ToString();  获取星期    // Thursday
DateTime.Now.DayOfYear.ToString();  获取第几天    // 248
DateTime.Now.Hour.ToString();          获取小时    // 20
DateTime.Now.Minute.ToString();     获取分钟    // 31
DateTime.Now.Second.ToString();     获取秒数    // 45

//n为一个数,可以数整数,也可以事小数
dt.AddYears(n).ToString();    //时间加n年
dt.AddDays(n).ToString();    //加n天
dt.AddHours(n).ToString();    //加n小时
dt.AddMonths(n).ToString();    //加n个月
dt.AddSeconds(n).ToString();    //加n秒
dt.AddMinutes(n).ToString();    //加n分

System.DateTime.Now.ToString("yyyy年MM月dd日 dddd HH:mm");
输出格式为:
2006年07月31日 星期一 09:26
这样比较简单


显示 十六进制 tmp中的 0x00
static void Main(string[] args)

{
    byte[] tmp = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

    string str1 = BitConverter.ToString(tmp);
    Console.WriteLine(str1);
}
/*

直接显示为
00-00-30-12-44-77-77-14
*/

https://bbs.csdn.net/topics/392158678?page=1

或者

byte[] bufData = { 0x00, 0x56, 0x01, 0x00, 0xAD, 0x2F, 0xF1, 0x00, 0x67, 0xA3, 0x40, 0x00};

  Console.WriteLine("replace 0x00 to ?:{0} \r\n", Encoding.Default.GetString(bufData).Replace('\0','0'));

判断字符串中是否包含中文

        /// <summary>
        /// 判断字符串中是否包含中文
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>判断结果</returns>
        private static bool HasChineseChar(string str)
        {
            return Regex.IsMatch(str, @"[\u4e00-\u9fa5]");
        }

弹窗提示,确认
doc

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions)
在指定对象的前面显示具有指定文本、标题、按钮、图标、默认按钮和选项的消息框。

public static System.Windows.Forms.DialogResult Show (System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options);

参数
owner
IWin32Window
将拥有模式对话框的 IWin32Window 的一个实现。
text
String
要在消息框中显示的文本。
caption
String
要在消息框的标题栏中显示的文本。
buttons
MessageBoxButtons
MessageBoxButtons 值之一,可指定在消息框中显示哪些按钮。
icon
MessageBoxIcon
MessageBoxIcon 值之一,它指定在消息框中显示哪个图标。
defaultButton
MessageBoxDefaultButton
MessageBoxDefaultButton 值之一,可指定消息框的默认按钮。
options
MessageBoxOptions
MessageBoxOptions 值之一,可指定将对消息框使用哪些显示和关联选项。 若要使用默认值,请传入 0。
返回
DialogResult
DialogResult 值之一。
例外
InvalidEnumArgumentException
buttons 不是 MessageBoxButtons 的成员。
-或- icon 不是 MessageBoxIcon 的成员。
-或- defaultButton 不是 MessageBoxDefaultButton 的成员。
InvalidOperationException
尝试在运行模式不是用户交互模式的进程中显示 MessageBox。 这是由 UserInteractive 属性指定的。
ArgumentException
options 同时指定了 DefaultDesktopOnly 和 ServiceNotification。
-或- options 指定了 DefaultDesktopOnly 或 ServiceNotification 并在 owner 参数中指定一个值。 仅当调用不接受 owner 参数的该方法版本时,才使用这两个选项。
-或- buttons 指定了无效的 MessageBoxButtons 组合。
示例
下面的代码示例演示如何使用 Show的此重载支持的选项显示 MessageBox。 在验证字符串变量 ServerName为空后,该示例将显示一个 MessageBox,为用户提供取消操作的选项。 如果 Show 方法的返回值的计算结果为 Yes,则关闭 MessageBox 的窗体。
C#

复制

private void validateUserEntry2()
{

    // Checks the value of the text.

    if(serverName.Text.Length == 0)
    {

        // Initializes the variables to pass to the MessageBox.Show method.

        string message = "You did not enter a server name. Cancel this operation?";
        string caption = "No Server Name Specified";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;

        // Displays the MessageBox.

        result = MessageBox.Show(this, message, caption, buttons,
            MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 
            MessageBoxOptions.RightAlign);

        if(result == DialogResult.Yes)
        {

            // Closes the parent form.

            this.Close();
        }
    }
}

注解
您可以使用 owner 参数指定实现 IWin32Window 接口的特定对象,该对象将用作对话框的顶级窗口和所有者。 消息框是模式对话框,这意味着除了模式窗体上的对象之外,不会出现任何输入(键盘或鼠标单击)。 在输入到另一种形式之前,程序必须隐藏或关闭模式窗体(通常是为了响应某些用户操作)。
消息框上最多可以有三个按钮。


Remove(Int32, Int32)
返回指定数量字符在当前这个实例起始点在已删除的指定的位置的新字符串。

public string Remove (int startIndex, int count);

参数
startIndex Int32

开始删除字符的从零开始的位置。
count Int32
要删除的字符数。
返回
String
一个新字符串,除所删除的字符之外,该字符串与此实例等效。
原文地址-微软官方文档

猜你喜欢

转载自www.cnblogs.com/dysonnnn/p/12666744.html
今日推荐