C#使用正则判断IP地址

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#使用phantomjs采集页面HTML

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# 操作INI文件的类

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# 随机生成手机号码

在现代软件开发中,随机生成手机号码是一项常见的需求,无论是用于测试、数据填充还是其他应用场景。本文将介绍如何使用 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 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;
}

代码解析

  1. ​可用的手机号码段:
    • telStarts 数组包含了当前中国大陆主要的手机号码段,每三个数字代表一个运营商和号段的组合,例如 134、135 等。这些前缀确保生成的号码符合运营商的分配规则。
  2. ​生成过程:
    • ​随机选择前缀:使用 Random 类从 telStarts 数组中随机选择一个号码前缀,保证了号码前3位的合法性和多样性。
    • ​生成中间四位:ran.Next(100, 888) + 10000 生成一个五位数,通过 Substring(1) 获取后四位。为了进一步提高号码的随机性和有效性,可以调整生成范围,避免生成不常用的数字组合。
    • ​生成末四位:类似地,ran.Next(1, 9100) + 10000 生成一个五位数,通过 Substring(1) 获取后四位。
    • ​拼接号码:将前3位、中间4位和末4位拼接起来,形成一个完整的11位手机号码。

C#文件重命名

C#文件重命名

DirectoryInfo dir = new DirectoryInfo(img); // 创建DirectoryInfo类的实例
var path = Path .GetDirectoryName(img); // 获取原图片文件的路径
dir.MoveTo(path + $"\\{ret.id}.jpg" ); // 进行改名