C# WINFORM使用html css里十六进制颜色格式:
BackColor = ColorTranslator.FromHtml("#626262");
C# WINFORM使用html css里十六进制颜色格式:
BackColor = ColorTranslator.FromHtml("#626262");
C#使用正则判断一个字符串是否为IP地址:
private boolean isIp(String ip) {
String rex =
"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
Pattern pattern = Pattern.compile(rex);
Matcher matcher = pattern.matcher(ip);
return matcher.find();
}
C# 只要超出一点就进位 无条件进位:
就如下面的数字进位方式:
5.99 – 6
6.00 – 6
6.01 – 7
int x = (int)(Math.Ceiling(6.01)); // 结果 x=7
无条件舍去应该用floor
,应该是这个名字,具体要查一下。
C#使用phantomjs采集页面HTML。
code.js
phantom.outputEncoding="gb2312"
system = require('system')
address = system.args[1];//获得命令行第二个参数 接下来会用到
//console.log('Loading a web page');
var page = require('webpage').create();
var url = address;
//console.log(url);
page.open(url, function (status) {
//Page is loaded!
if (status !== 'success') {
console.log('Unable to post!');
} else {
//console.log(page.content);
//var title = page.evaluate(function() {
// return document.title;//示范下如何使用页面的jsapi去操作页面的
// });
//console.log(title);
//console.log(encodeURIComponent(page.content));
console.log(page.content);
}
phantom.exit();
});
C#代码
public string getAjaxCotnent(String url)
{
var b = System.Reflection.Assembly.GetEntryAssembly().Location;
var path = Path.GetDirectoryName(b); // 末尾不带斜杠
ProcessStartInfo start = new ProcessStartInfo(path + "//phantomjs.exe");
start.Arguments = "code.js" + " " + url;//设置命令参数
StringBuilder sb = new StringBuilder();
start.CreateNoWindow = false;//不显示dos命令行窗口
start.RedirectStandardOutput = true;//
start.RedirectStandardInput = true;//
start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
Process p = Process.Start(start);
// string encoding = p.StandardOutput.CurrentEncoding.ToString();
StreamReader reader = p.StandardOutput;//截取输出流
string line = reader.ReadLine();//每次读取一行
sb.AppendLine(line);
while (!reader.EndOfStream)
{
line = reader.ReadLine();
sb.AppendLine(line);
}
p.WaitForExit();//等待程序执行完退出进程
p.Close();//关闭进程
reader.Close();//关闭流
string strRet = System.Web.HttpUtility.UrlDecode(sb.ToString());
return strRet;
}
调用
var url = "http://po.baidu.com/feed/share?context=%7B%22nid%22%3A%22news_3493960622142783493%22%2C%22sourceFrom%22%3A%22bjh%22%7D";
var detailHtml = getAjaxCotnent(url);
C# 获取程序编译时间
var compileTime = System.IO.File.GetLastWriteTime(System.Reflection.Assembly.GetEntryAssembly().Location);
C#检查系统是否支持HttpListener
// 检查系统是否支持
if (!HttpListener.IsSupported)
{
throw new System.InvalidOperationException("必须为 Windows XP SP2 或 Server 2003 以上系统!");
}
C# 操作INI文件的类
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
namespace server
{
class IniFile // revision 11
{
string Path;
string EXE = Assembly.GetExecutingAssembly().GetName().Name;
[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);
public IniFile(string IniPath = null)
{
Path = new FileInfo(IniPath ?? EXE + ".ini").FullName.ToString();
}
public string Read(string Key, string Section = null)
{
var RetVal = new StringBuilder(255);
GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path);
return RetVal.ToString();
}
public void Write(string Key, string Value, string Section = null)
{
WritePrivateProfileString(Section ?? EXE, Key, Value, Path);
}
public void DeleteKey(string Key, string Section = null)
{
Write(Key, null, Section ?? EXE);
}
public void DeleteSection(string Section = null)
{
Write(null, null, Section ?? EXE);
}
public bool KeyExists(string Key, string Section = null)
{
return Read(Key, Section).Length > 0;
}
}
}
C# 格式化时间的格式,如下:
yyyy-MM-dd HH:mm:ss
记忆方法:只有MH(马航)是大写,其它小写。
C# 随机生成手机号码
private string [] telStarts = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153,180,181,182,183,185,186,176,187,188,189,177,178" .Split(',' );
/// <summary>
/// 随机生成电话号码
/// </summary>
/// <returns></returns>
public string getRandomTel()
{
Random ran = new Random();
int n = ran.Next(10, 1000);
int index = ran.Next(0, telStarts.Length - 1);
string first = telStarts[index];
string second = (ran.Next(100, 888) + 10000).ToString().Substring(1);
string thrid = (ran.Next(1, 9100) + 10000).ToString().Substring(1);
return first + second + thrid;
}
C#文件重命名
DirectoryInfo dir = new DirectoryInfo(img); // 创建DirectoryInfo类的实例
var path = Path .GetDirectoryName(img); // 获取原图片文件的路径
dir.MoveTo(path + $"\\{ret.id}.jpg" ); // 进行改名