C# .NET 执行bat或exe文件

代码如下:新建进程,运行cmd,参数为bat文件的内容。
注意如果是IIS部署,且批处理中需要管理员权限,则还需要将该网站的应用程序池–高级设置–标识设置为:LocalSystem

string filePath = Server.MapPath("/bat.bat");
if (File.Exists(filePath))
{
    Process process = new Process();
    var startInfo = new ProcessStartInfo
    {
        FileName = "cmd.exe",       //这里执行的是bat文件,所以要在cmd中执行
        // Use /K to have the new cmd window stay after the batch file is done
        Arguments = $"/C \"{filePath}\"",
        Verb = "runas",             //管理员身份运行
        RedirectStandardOutput = true,      //重定向输出为了前台展示cmd内容
        RedirectStandardError = true,
        UseShellExecute = false
    };
    process.StartInfo = startInfo;

    process.Start();
    process.WaitForExit();

    //获取输出结果
    string output = process.StandardOutput.ReadToEnd();
    this.txtResult.Text = "已执行。\n" + output;
}

.NET C# PDFSharp使用

// Open an existing document. Providing an unrequired password is ignored.
PdfSharp.Pdf.PdfDocument document = PdfReader.Open(pdfFilePath);
PdfSecuritySettings securitySettings = document.SecuritySettings;
// Setting one of the passwords automatically sets the security level to 
// PdfDocumentSecurityLevel.Encrypted128Bit.
//securitySettings.UserPassword = "user";
securitySettings.OwnerPassword = GGGuid.ToString();
// Don't use 40 bit encryption unless needed for compatibility reasons
//securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted40Bit;
// Restrict some rights.
//除打印权限其他均关闭
securitySettings.PermitAccessibilityExtractContent = false;
securitySettings.PermitAnnotations = false;
securitySettings.PermitAssembleDocument = false;
securitySettings.PermitExtractContent = false;
securitySettings.PermitFormsFill = false;
securitySettings.PermitFullQualityPrint = true;
securitySettings.PermitModifyDocument = false;
securitySettings.PermitPrint = true;

//水印
for (int i = 0; i < document.Pages.Count; i++)
{
	string watermark = "水印文字";        //水印文字
	double emSize = 42;
	XFont xFont = new XFont("黑体", emSize);
	var page = document.Pages[i];

	var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
	var size = gfx.MeasureString(watermark, xFont);
	gfx.TranslateTransform(page.Width / 2, page.Height / 2);
	gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
	gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);
	var format = new XStringFormat();
	format.Alignment = XStringAlignment.Near;
	format.LineAlignment = XLineAlignment.Near;
	XBrush brush = new XSolidBrush(XColor.FromArgb(16, 0, 0, 0));
	//循环以实现全屏水印
	for (int y = 0; y < 10; y++)
	{
		for (int x = 0; x < 10; x++)
		{
			gfx.DrawString(watermark, xFont, brush, new XPoint(x * 180 - 300, y * 120), format);
		}
	}
}

document.Save(pdfFilePath);

Javascript 判断微信内置浏览器浏览

在页面中添加如下代码,检测如果是微信打开的网页,则要求用户打开其他浏览器
div元素

<div class="weixin-tip">
<p>
<img src="/images/live_weixin.png" alt="在浏览器打开" />
</p>
</div>

sciprt代码:

    $(window).on("load", function () {
        var winHeight = $(window).height();
        function is_weixin() {
            var ua = navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == "micromessenger") {
                return true;
            } else {
                return false;
            }
        }
        var isWeixin = is_weixin();
        if (isWeixin) {
            $(".weixin-tip").css("height", winHeight);
            $(".weixin-tip").show();
        }
    })

样式代码:

<style type="text/css">
.weixin-tip a {
text-decoration: none;
}

.weixin-tip img {
max-width: 100%;
height: auto;
}

.weixin-tip {
display: none;
position: fixed;
left: 0;
top: 0;
bottom: 0;
background: rgba(0,0,0,0.8);
filter: alpha(opacity=80);
height: 100%;
width: 100%;
z-index: 100;
}

.weixin-tip p {
text-align: center;
margin-top: 10%;
padding: 0 5%;
}
</style>

C# 多线程For循环

使用Parallel.For方法替代基础的for循环,基础代码如下

Parallel.For(0, recordDT.Rows.Count, new ParallelOptions(), (x) => {
    recordDT.Rows[x]
    ......code
}

如果在循环内需要使用到List等原生集合序列元素,请替代为:ConcurrentStack,否则List会缺失项目。

ConcurrentStack<string> strList = new ConcurrentStack<string>();

mysql 忘记密码 重置密码

如果MYSQL不是正常通过apt install mysql 安装的,或者是xammp之类集成的数据库,这时候mysql密码忘记了,可以按照如下操作:

1、查看并修改/etc/my.cnf文件,将mysql.sock指向正确的mysql路径。(标红处)

[client]
port=3306
socket=/opt/lampp/var/mysql/mysql.sock
#socket=/var/lib/mysql/mysql.sock
default-character-set=latin1

[mysqld]
#datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock
datadir=/opt/lampp/var/mysql
socket=/opt/lampp/var/mysql/mysql.sock
user=mysql
set-variable = max_connections=10000
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

default-storage-engine=InnoDB
character-set-server=latin1

skip-grant-tables

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

2、重启环境通过如下命令登录mysql,提示输入密码时直接回车。

mysqld_safe --skip-grant-tables & mysql -p

3、创建用户或更改密码,注意先刷新权限,直接更新密码会报错

flush privileges;
grant all privileges on *.*  to  'root'@'%'  identified by '123456'  with grant option;