目录
2.vs中,生成dll报错时,修改属性中本地复制改成false
3.ClickOnce发布包安装不了,32位,64位。设置IE的安全问题。
19. winform中查询打开的模态窗体和在线程上访问控件
1.在计算机的刷新按钮中增加菜单,如图所示。
创建一个 reg文件,把以下代码写入
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\aa]
@="右键刷新中的名称"
"icon"="C:\\Users\\LiFan\\Desktop\\img\\logo.ico"
[HKEY_CLASSES_ROOT\Directory\Background\shell\aa\command]
@="D:\\Program Files (x86)\\JisuPdf\\JisuPdf.exe"
第一个icon是右键的图标路径
第二个是启动exe的路径
执行后,在注册表中是这样的。
2.vs中,生成dll报错时,修改属性中本地复制改成false
3.ClickOnce发布包安装不了,32位,64位。设置IE的安全问题。
4.“/”应用程序中的服务器错误。
查看 IIS 的应用程序池,把启用 32 位应用程序 改成 True
5.安装完成postgresql数据库,启动步骤
先进入 d:
再进入 cd D:\postgresql\bin
再执行 pg_ctl start -D "D:\postgresql\data"
6.启动mysql
启动:net start mysql
停止:net stop mysql
7.winform只能启动一个exe实例
建立一个winform项目
在Program.cs文件中写入
使用using
using AutoUpdaterDotNET;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsApp1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
const string mutexName = "WinFormsApp1";
//是否允许创建新客户端实例
bool createdNew;
//创建 Mutex 实例,传入上面定义的互斥为止标识名称
System.Threading.Mutex mutex = new System.Threading.Mutex(true, mutexName, out createdNew);
if (createdNew)
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
MessageBox.Show("程序已经在运行", "提示信息");
return;
}
}
}
}
8.WPF只能启动一个exe实例
建立一个wpf项目
把启动项修改一下,修改App.xaml文件
使用using
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.cs中增加代码
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
//互斥的唯一标识名称
const string mutexName = "WpfApp1";
//WPF程序启动的入口
private void Application_Startup(object sender, StartupEventArgs e)
{
//是否允许创建新客户端实例
bool createdNew;
//创建 Mutex 实例,传入上面定义的互斥为止标识名称
System.Threading.Mutex mutex = new System.Threading.Mutex(true, mutexName, out createdNew);
if (createdNew)
{
MainWindow win = new MainWindow();
win.ShowDialog();
//Application.Current.Shutdown();
}
else
{
MessageBox.Show("程序已经在运行", "提示信息");
Environment.Exit(0);
}
}
}
}
拓展:
还可以使用Process
Process[] ps = Process.GetProcesses();
var process = ps.Where(x => x.ProcessName == Process.GetCurrentProcess().ProcessName);
if (process.Count() > 1)
{
MessageBox.Show("程序已经启动,请勿重复启动");
Environment.Exit(0);
}
9.对字符串进行加密和解密
1.界面
2.后台代码
3.加密和解密方法
/// <summary>
/// 加密
/// </summary>
/// <param name="strSource"></param>
/// <returns></returns>
public string Md5Encrypt(string strSource)
{
//把字符串放到byte数组中
byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource);
//建立加密对象的密钥和偏移量
byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 36 };//定义偏移量
byte[] key = { 55, 103, 246, 79, 36, 99, 167, 6 };//定义密钥
//实例DES加密类
DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider();
mobjCryptoService.Key = iv;
mobjCryptoService.IV = key;
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
//实例MemoryStream流加密密文件
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
string strOut = System.Convert.ToBase64String(ms.ToArray());
return strOut;
}
/// <summary>
/// 解密
/// </summary>
/// <param name="Source"></param>
/// <returns></returns>
public string Md5Decrypt(string Source)
{
//JHLOG.Instance().Writelog("Source:" + Source);
//将解密字符串转换成字节数组
byte[] bytIn = System.Convert.FromBase64String(Source);
//给出解密的密钥和偏移量,密钥和偏移量必须与加密时的密钥和偏移量相同
byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 36 };//定义偏移量
byte[] key = { 55, 103, 246, 79, 36, 99, 167, 6 };//定义密钥
DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider();
mobjCryptoService.Key = iv;
mobjCryptoService.IV = key;
//实例流进行解密
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader strd = new StreamReader(cs, Encoding.Default);
return strd.ReadToEnd();
}
注意:加密和解密中的iv和key可以随意修改,并且要一样。
4.效果
输入123点击button2按钮
结果
点击 button3按钮
10.流、字符串转数组
static void binaryRW(string fileName)//二进制文件输入输出
{
using (BinaryWriter bw = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
bw.Write(10);
bw.Write("hello binary!");
bw.Write(true);
}
using (BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open)))
{
int i1 = br.ReadInt32();
string s = br.ReadString();
bool b = br.ReadBoolean();
Console.WriteLine("{0},{1},{2}", i1, s, b);
}
}
调用
binaryRW(@"d:\abc.dat");
二进制流的写入和读取
StringBuilder sb1 = new StringBuilder();
sb1.Append("我爱你,你爱我");
byte[] dataArray = Encoding.Unicode.GetBytes(sb1.ToString());
BinaryWriter binWriter = new BinaryWriter(new MemoryStream());
binWriter.Write(dataArray); //写入流
BinaryReader binReader = new BinaryReader(binWriter.BaseStream);
binReader.BaseStream.Position = 0;
byte[] verifyArray = binReader.ReadBytes((int)binReader.BaseStream.Length+1); //读取流 多加一个,没事
string sb = Encoding.Unicode.GetString(verifyArray, 0, verifyArray.Length);//流转化字符串
网络调用的时候,获取公共的网络流
//获取网络流
NetworkStream networkStream = client.GetStream();
//将网络流作为二进制读写对象
br = new BinaryReader(networkStream);
bw = new BinaryWriter(networkStream);
字符串转数组
StringBuilder sb1 = new StringBuilder();
sb1.Append("我爱你,你爱我");
byte[] data = Encoding.Unicode.GetBytes(sb1.ToString()); //字符串转数组
string sb = string.Empty;
if (data != null && data.Length > 0)
{
sb = Encoding.Unicode.GetString(data, 0, data.Length); //数组转字符串
}
11.获取本机IP的方法
第一种
public static string GetLocalIP()
{
try
{
string HostName = Dns.GetHostName(); //得到主机名
IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
for (int i = 0; i < IpEntry.AddressList.Length; i++)
{
//从IP地址列表中筛选出IPv4类型的IP地址
//AddressFamily.InterNetwork表示此IP为IPv4,
//AddressFamily.InterNetworkV6表示此地址为IPv6类型
if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
{
string ip = "";
ip = IpEntry.AddressList[i].ToString();
return IpEntry.AddressList[i].ToString();
}
}
return "";
}
catch (Exception ex)
{
return ex.Message;
}
}
第二种
public static string GetIp()
{
var ip = Dns.GetHostEntry(Dns.GetHostName());
return ip.AddressList.FirstOrDefault(p => p.AddressFamily.ToString() == "InterNetwork")?.ToString();
}
12.图片互相换字节流
字节数组转换图片
private Image ByteToImage(byte[] btImage)
{
if (btImage.Length == 0)
return null;
System.IO.MemoryStream ms = new System.IO.MemoryStream(btImage, 1, btImage.Length - 1);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
return image;
}
图片转换字节数组
private byte[] ImageToByte(Image picture)
{
MemoryStream ms = new MemoryStream();
if (picture == null)
return new byte[ms.Length];
picture.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] BPicture = new byte[ms.Length];
BPicture = ms.GetBuffer();
return BPicture;
}
13.传递字节流加标志位
传递
//获得字节数组
byte[] data = Encoding.Default.GetBytes(txtMsg.Text);
对字节数组的头部增加一位
byte[] result = new byte[data.Length + 1];
//设置字节数组的头部字节是3:代表字符串
result[0] = 3;//注意这个3,后期拿到的话,根据流解析
//把原始的数据放到最终的字节数组里去
Buffer.BlockCopy(data, 0, result, 1, data.Length);//重要
判断
byte[] data = new byte[1024 * 1024];
if (data[0] == 3)//3表示定义的类型
解析,这里要减去1
System.IO.MemoryStream ms = new System.IO.MemoryStream(btImage, 1, btImage.Length - 1);
14.bat启动
1.cmd命令启动
MoviconNExT.exe D:\movicon\demo\demo.UFProject -start
2.快捷图标启动
"C:\Program Files\Progea\Movicon.NExT 4.1\MoviconNextRT.exe" D:\movicon\demo\demo.UFProject
3.bat启动
@echo off
C:
cd C:\Program Files\Progea\Movicon.NExT 4.1
MoviconNextRT.exe D:\movicon\demo\demo.UFProject
4.bat启动,不弹框
1.bat内容:
@echo off
C:
cd C:\Program Files\Progea\Movicon.NExT 4.1
MoviconNextRT.exe D:\movicon\demo\demo.UFProject
1.vbs内容:
Set ws = CreateObject("Wscript.Shell")
ws.run "cmd /c D:\demo\2\1.bat",vbhide
双击1.vbs
15.cmd快速启动
在
路径上,直接输入cmd,点击回车,能快速进去cmd命令中。
16.集合ForEach用法
List<StudentInfo> list = new List<StudentInfo>();
for (int i = 0; i < 10; i++)
{
StudentInfo entity1 = new StudentInfo();
entity1.Number = i;
entity1.Name = "其他" + i;
entity1.Class = "其他" + i + "其他";
list.Add(entity1);
}
List<string> list1 = new List<string>();
list1.Add("1");
list1.Add("2");
list1.Add("3");
list.ForEach(a => a.Number = a.Number + 1);//Number每一项都加1
list.ForEach(a => a.Name = list1.FindIndex(l1 => l1.ToString() == a.Name).ToString());//每一项集合对比,然后赋值
list.ForEach(a =>
{
//每一项集合对比,然后赋值,默认值的写法
a.Class = list1.FindIndex(l1 => l1.ToString() == a.Name).ToString()=="1"?"123":"456";
});
17.代码混淆器ConfuserEx使用
混淆exe:
混淆dll:
使用vs2022也可以安装
18.加壳
1.首先放入一个要加壳的exe程序
2.设置
3.代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
String projectName = Assembly.GetExecutingAssembly().GetName().Name.ToString();
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(projectName + ".WindowsFormsApp4.exe");
byte[] bs = new byte[stream.Length];
stream.Read(bs, 0, (int)stream.Length);
Assembly asm = Assembly.Load(bs);
MethodInfo info = asm.EntryPoint;
ParameterInfo[] parameters = info.GetParameters();
if ((parameters != null) && (parameters.Length > 0))
info.Invoke(null, (object[])args);
else
info.Invoke(null, null);
}
}
}
19. winform中查询打开的模态窗体和在线程上访问控件
foreach (var form in Application.OpenForms.Cast<Form>().Where(f => !f.Modal))
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() =>
{
form.Enabled = false;//冻结窗体
}
));
}
}