C#基本工具代码

控制台Cosole

Console.WriteLine("HelloWorld");
Console.ReadLine(); //等待用户按一个回车
return; //可选,按下回车后关闭

下载Xlsx

public static void TryToDisplayGeneratedFileXlsx(string writeFilePath, string fileName)
{
   HttpContext.Current.Response.Clear();
   HttpContext.Current.Response.Charset = System.Text.Encoding.Default.WebName;
   HttpContext.Current.Response.AddHeader("content-disposition", "attachment;  filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xlsx");
   HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
   HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
   HttpContext.Current.Response.WriteFile(writeFilePath);
   HttpContext.Current.Response.End();
}

 C#多维数组字面语法

参考

CURL之WebRequest

string url = "https://YOUR_COMPANY_HERE.beebole-apps.com/api";
string data = "{"service":"absence.list", "company_id":3}";

WebRequest myReq = WebRequest.Create(url);
myReq.Method = "POST";
myReq.ContentLength = data.Length;
myReq.ContentType = "application/json; charset=UTF-8";

string usernamePassword = "YOUR API TOKEN HERE" + ":" + "x";
UTF8Encoding enc = new UTF8Encoding();
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(enc.GetBytes(usernamePassword)));

using (Stream ds = myReq.GetRequestStream())
{
ds.Write(enc.GetBytes(data), 0, data.Length); 
}

WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
Response.Write(content);

参考1, 参考2(有解释,有共通点)

 请求字符串拼接Dictionary

/// <summary>
/// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
/// </summary>
/// <param name="sArray">需要拼接的数组</param>
/// <returns>拼接完成以后的字符串</returns>
public static string CreateLinkString(Dictionary<string, string> dicArray)
{
    if (dicArray.Count == 0)
    {
        return "";
    }

    StringBuilder prestr = new StringBuilder();
    foreach (KeyValuePair<string, string> temp in dicArray)
    {
        prestr.Append(temp.Key + "=" + temp.Value + "&");
    }
    //去掉最后一个&字符
    int nLen = prestr.Length;
    prestr.Remove(nLen - 1, 1);
    return prestr.ToString();
}

拼接SQL

StringBuilder fieldSetsSb = new StringBuilder();
List<string> fields = new List<string>();
if (companyEntity.PruchFeeCount.HasValue)
{
    fields.Add("PruchFeeCount");
}
else if (companyEntity.UpdateFeeCount.HasValue)
{
    fields.Add("UpdateFeeCount");
}
foreach (string field in fields)
{
    if (fieldSetsSb.Length == 0)
    {
        fieldSetsSb.Append("[").Append(field).Append("]=ISNULL("+ field + ",0)+@").Append(field);    //UPDATE tbiz_Company SET [UpdateFeeCount]=ISNULL(UpdateFeeCount,0)+@UpdateFeeCount WHERE CompanyID = @CompanyID
    }
    else
    {
        fieldSetsSb.Append(",[").Append(field).Append("]=ISNULL(" + field + ",0)+@").Append(field);
    }
}
string fieldSets = fieldSetsSb.ToString();
string sql = @"UPDATE tbiz_Company SET "+ fieldSets + " WHERE CompanyID = @CompanyID";

反射打印变量自身字面值

public static string GetVarName(System.Linq.Expressions.Expression<Func<string, string>> exp)
{
    return ((System.Linq.Expressions.MemberExpression)exp.Body).Member.Name;
}

//调用方法
public static string GetSeachStr(string value)
{
    string result = GetVarName(p => value);
    return value;
}

 正则表达式实例

using System.Text.RegularExpressions;
string
[] keyWordsArray = Regex.Split(postData.keywords, @"[s|,|,]{1,}"); //按全角逗号,半角逗号,空格 切割
string text = strData.ReadLine().Trim();
Match TitleMatch = Regex.Match(text, @"[([^[]]*)]"); //抓取由方括号界定的字符
string titleKey = TitleMatch.Groups[1].Value;
相关参考
 ( ( [ ^  (  ) ] * )  )
最外层的  (... )代表匹配左右括号
里面的 ( [ ^  (  ) ] * ) 代表匹配0个或多个不包含左右括号的任意字符
所以这个式子是匹配一对括号及其内部的所有内容,包括空值
string test = "yw_wq01_22.jpg";
Match numMatch = Regex.Match(test, @"_(d+)."); //获取最大的编号
string num = numMatch.Groups[1].Value; //提取22

IndexOf()  Substring() 切割字符串

string url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri; //AbsoluteUri = "http://localhost:58321/Journal/StaticHtml"
int start = url.IndexOf("//"); //搜索//第一次出现的位置
int end = url.IndexOf("/", start + 2);//跳过// 2个位置,到/第一次出现的位置
string urlHead = url.Substring(0,end + 1);
string reRewriteUrl = urlHead + "index.html";

end

原文地址:https://www.cnblogs.com/zhuji/p/5763690.html