代码类
1.禁止Form窗口调整大小
this.FormBorderStyle = FixedSingle;
2.禁止使用最大化窗口:
MaximuzeBox = False;
3.禁止使用最小化窗口:
MinimizeBox = False;
4.设置ComboBox第一位数
SelectedIndex = 2;
5.三元运算符
checkBox1.Text = checkBox1.Text == "是" ? "否" : "是";
7.选择路径返回String Path
public string SelectPath()
{
string path = string.Empty;
System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
path = fbd.SelectedPath;
}
return path;
}
8.接口的使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CheckUrlStatusRealOK
{
interface SystemAPI
{
//选择路径函数
string SelectPath();
}
class API : SystemAPI
{
/// <summary>
/// 选择一个路径
/// </summary>
/// <returns></returns>
public string SelectPath()
{
string path = string.Empty;
System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
path = fbd.SelectedPath;
}
return path;
}
}
}
9.MessageBox的使用
MessageBox.Show(
"提示内容",
"标题内容"
第一个按钮:{
MessageBoxButtons.YesNoCancel,
}
);
10.TextBox高度
//行为属性里设置:
Multline = True
11.逐行读取TextBox内容
//新建一个array list
string[] UrlList = new string[textBox_UrlList.Lines.Length];
//循环读取url内容
for (int i = 0; i < textBox_UrlList.Lines.Length; i++)
{
UrlList[i] = textBox_UrlList.Lines[i];
}
12.C#生成随机数
Random ran = new Random();
int n = ran.Next(100);
13.C#多线程
public void logtest()
{
Random ran = new Random();
int n = ran.Next(1000);
//....
}
public void main()
{
Thread t1 = new Thread(new ThreadStart(logtest));
t1.IsBackground = true;
t1.Start();
}
14.设置窗口居中
选中窗体,打开右边的“属性”小窗口,找到 StartPosition,并选择其值为 CenterParent(居中相对父窗体),如图1所示
15.C#For循环
for(int x = 0;x <= 10;x++){
//do soming
}
16.C# 类型转换 Int转String
str = "123";
int result=int.Parse(str);
17.C#获取当前时间
//获取日期+时间
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分
18.获取当前路径(目录)
string Path = System.Environment.CurrentDirectory;
19.C++编译DLL+C#调用
extern "C" __declspec(dllexport) char* STD(char * text)
{
return text;
}
[DllImport("Dll1.dll", EntryPoint = "STD", CallingConvention = CallingConvention.Cdecl)]
public static extern string STD(string text);
//Dll1.dll 加载dll的名称
//EntryPoint 函数名称
//CallingConvention 调用方式
//extern string STD(string text); 初始化函数
20.C#按名称结束进程
private void KillProcess(string processName)
{
foreach (var process in Process.GetProcessesByName(processName))
{
try
{
// 杀掉这个进程。
process.Kill();
// 等待进程被杀掉。你也可以在这里加上一个超时时间(毫秒整数)。
process.WaitForExit();
}
catch (Win32Exception ex)
{
// 无法结束进程,可能有很多原因。
// 建议记录这个异常,如果你的程序能够处理这里的某种特定异常了,那么就需要在这里补充处理。
// Log.Error(ex);
}
catch (InvalidOperationException)
{
// 进程已经退出,无法继续退出。既然已经退了,那这里也算是退出成功了。
// 于是这里其实什么代码也不需要执行。
}
}
}
21.C#获得Ping值
private void Form1_Load(object sender, EventArgs e)
{
;
if(int.Parse(test()) < 300)
{
string a = "123";
}
else
{
string a = "123";
}
}
public string test()
{
//远程服务器IP
string ipStr = "103.44.252.155";
//构造Ping实例
Ping pingSender = new Ping();
//Ping 选项设置
PingOptions options = new PingOptions();
options.DontFragment = true;
//测试数据
string data = "test";
byte[] buffer = Encoding.ASCII.GetBytes(data);
//设置超时时间
int timeout = 30000;
//调用同步 send 方法发送数据,将返回结果保存至PingReply实例
PingReply reply = pingSender.Send(ipStr, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
//Console.WriteLine("答复的主机地址:" + reply.Address.ToString() + "\n");
//Console.WriteLine("服务器延迟:" + reply.RoundtripTime + "ms\n");
//Console.WriteLine("生存时间(TTL):" + reply.Options.Ttl + "\n");
//Console.WriteLine("是否控制数据包的分段:" + reply.Options.DontFragment + "\n");
//Console.WriteLine("缓冲区大小:" + reply.Buffer.Length + "\n");
return reply.RoundtripTime.ToString();
}
else
return reply.RoundtripTime.ToString();
//Console.WriteLine(reply.Status.ToString());
//Console.WriteLine("服务器延迟:" + reply.RoundtripTime + "ms\n");
//Console.WriteLine("服务器延迟:>120ms\n");
}
22.C#鼠标悬浮提示
# 事件属性>MouseHover
# 方法函数
private void simpleButton2_MouseHover(object sender, EventArgs e)
{
// 创建the ToolTip
ToolTip toolTip1 = new ToolTip();
// 设置显示样式
toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
toolTip1.InitialDelay = 500;//事件触发多久后出现提示
toolTip1.ReshowDelay = 500;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
toolTip1.ShowAlways = true;//是否显示提示框
// 设置伴随的对象.
toolTip1.SetToolTip(this.simpleButton1, "查询");//设置提示按钮和提示内容
}
23.C#解析Json
using Newtonsoft.Json.Linq;
string response = "json字符串";
JArray res = JArray.Parse(response);
24.C#打开url返回字符串内容
public static string Get(string Url)
{
//System.GC.Collect();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Proxy = null;
request.KeepAlive = false;
request.Method = "GET";
request.ContentType = "application/json; charset=UTF-8";
request.AutomaticDecompression = DecompressionMethods.GZip;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
}
return retString;
}
25.C# 组件Commbox绑定json数据
public void StrArrToCbx(JArray configList, ComboBox combo)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
for (var i = 0; i < configList.Count; i++)
{
var tmpObj = (Newtonsoft.Json.Linq.JObject)configList[i];
var role_id = (string)tmpObj["url"];
var role_name = (string)tmpObj["name"] + (string)tmpObj["url"].ToString().Replace("", "");
DataRow dr = dt.NewRow();
dr["ID"] = role_id;
dr["Name"] = role_name;
dt.Rows.Add(dr);
}
combo.DataSource = dt;
combo.DisplayMember = "Name";
combo.ValueMember = "ID";
}
属性类
1.TextBox
属性名 | 作用 |
---|---|
Text | 文本框对象中显示的文本 |
MaxLength | 在文本框中最多输入的文本的字符个数 |
WordWrap | 文本框中的文本是否自动换行,如果是 True,则自动换行,如果是 False,则不能自动换行 |
PasswordChar | 将文本框中出现的字符使用指定的字符替换,通常会使用“*”字符 |
Multiline | 指定文本框是否为多行文本框,如果为 True,则为多行文本框,如果 为 False,则为单行文本框 |
ReadOnly | 指定文本框中的文本是否可以更改,如果为 True,则不能更改,即只读文本框,如果为 False,则允许更改文本框中的文本 |
Lines | 指定文本框中文本的行数 |
ScrollBars | 指定文本框中是否有滚动条,如果为 True,则有滚动条,如果为 False, 则没有滚动条 |
系统调用类
组件类
其他类
C#函数体注释
///