分类目录归档:C#

C#  INI-Parser 操作ini文件

读取:

var parser = new IniParser.FileIniDataParser();
IniParser.Model.IniData data = parser.ReadFile($"{System.Windows.Forms.Application.StartupPath}\\config.ini");
string bkgmusic = data["system"]["bkgmusic"];

写入:

var parser = new IniParser.FileIniDataParser();
IniParser.Model.IniData data = new IniParser.Model.IniData();
data["system"]["bkgmusic"] = "1";
parser.WriteFile($"{System.Windows.Forms.Application.StartupPath}\\config.ini", data);

C# 执行CMD命令

private static string InvokeCmd(string cmdArgs)
{
    string Tstr = "";
    Process p = new Process();
    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.CreateNoWindow = true;
    p.Start();
    p.StandardInput.WriteLine(cmdArgs);
    p.StandardInput.WriteLine("exit");
    Tstr = p.StandardOutput.ReadToEnd();
    p.Close();
    return Tstr;
}

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

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" ); // 进行改名

C# 每两位插入一个空格

string txt = "12345678" ;
string result = Regex .Replace(txt, @"(\d{2}(?!$))", "$1 " );

每N位插入一个就把2改成N


上面只分操作全是数字的,如果是适应所有的,可以把\d换成 . 号

string txt = "ABCD1E2F4G" ;
string result = Regex .Replace(txt, @"(.{2}(?!$))", "$1 " );

DBConn mysql

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace VideoSpider
{
    public class DBConn
    {
#if DEBUG
        private static   string _connectionString = @"server=61.164.149.180;User ID=sql_zuqiu;Password=FULFQcvuFsqnbQSL;database=zuqiu_bak;";
#else
        private static string _connectionString = @"server=localhost;User=11xs;Password=HPU3aMJVzECQfVXR;database=11xs";
#endif
        public static MySqlConnection OpenConnection( string connstr = "" )
        {

            if (connstr != "" )
            {
                _connectionString = connstr;
            }
            var conn = new MySqlConnection(_connectionString);
            conn.Open();
            return conn;
        }
    }
}