c# 执行exe

以下是代码:

Process process = new Process();
string exePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,  "a.exe");

string s = "";
foreach (var tmpArg in args)
{
	s += tmpArg.ToString() + " ";
}
s = s.Trim();
if (string.IsNullOrWhiteSpace(s))
{
	throw new Exception("缺少参数");
}

ProcessStartInfo psi = new ProcessStartInfo(exePath , s)
{
	CreateNoWindow = true,
	UseShellExecute = false,
};
process.StartInfo = psi;

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

检索 COM 类工厂中 CLSID 为 {28E68F9A-8D75-11D1-8DC3-3C302A000000} 的组件失败,原因是出现以下错误: 80040154 没有注册类

根据自己是32位还是64位来选择Regsvr32,以下是用代码注册的DLL

// Assembly assembly = Assembly.LoadFile(dllPath);
// 获取CLSID
// Guid guid = new Guid(((GuidAttribute)Attribute.GetCustomAttribute(assembly, typeof(GuidAttribute))).Value);
using (Process process = new Process())
{
	string regPath = System.Environment.GetFolderPath(Environment.SpecialFolder.System);
	if (Environment.Is64BitOperatingSystem)
	{
		regPath = System.Environment.GetFolderPath(Environment.SpecialFolder.SystemX86);
	}
	regPath = Path.Combine(regPath, "regsvr32");

	string reg32DllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Lib", "OPCDAAuto.dll"); // 要注册的dll路径
	ProcessStartInfo startInfo = new ProcessStartInfo(reg32DllPath)
	{
		WindowStyle = ProcessWindowStyle.Hidden,
		FileName = regPath,
		Arguments = $" {reg32DllPath} /s ",
		Verb = "runas"
	};
	process.StartInfo = startInfo;
	process.Start();
	process.WaitForExit();
}
Console.WriteLine(reg32DllPath + "注册成功");

c# dapper mysql like

const string sql = "SELECT * from user_profile WHERE FirstName LIKE @name;";
var result = connection.Query<Profile>(sql, new {name = "%"+name+"%"});

A generic error occurred in GDI+

  1. 因为 .net GDI+ 是对底层 的封装。 所以可以尝试用 Marshal.GetLastWin32Error();函数获得底层错误代码
try{
   image.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
} 
catch (Exception exp)
            {
                var code = Marshal.GetLastWin32Error();
                throw new Exception(exp.Message + ":" + code.ToString(), exp);
            }

2. 查看错误代码含义

ERROR_PATH_NOT_FOUND

3 (0x3)

The system cannot find the path specified.

原因:save的目录不存在。解决:手动或通过程序建save的目录。

ERROR_ACCESS_DENIED

5 (0x5)

Access is denied.

原因: save的目录没有权限

解决: 查看目录的权限

ERROR_SHARING_VIOLATION32 (0x20)

The process cannot access the file because it is being used by another process.

原因:

  从一个文件构造的Bitmap 对象或一个 Image 对象, 在该对象的生存期内该文件处于锁定状态。 因此, 在没有释放这个Image或Bitmap对象前,无法更改图像并将其保存回原文件。

  解决方法:

解决:

构造一个新的Image对象,然后把原来的Image对象中的图象通过Graphics的DrawImage()方法,拷贝到新Image对象中,最后通过Dispose()方法释放原来的Image对象:

  Image image = new Bitmap ( openFileDialog1 . FileName );

  //新建第二个Image类型的变量newImage,这里是根据程序需要设置自己设置。

  Image newImage = new Bitmap ( 800 , 600 );

  //将第一个bmp拷贝到bmp2中

  Graphics draw = Graphics . FromImage ( newImage);

  draw . DrawImage ( image , 0 , 0 );

  //释放第一个Image对象

  image.Dispose();

 3.其他问题

a.  Image 对应的Stream被Dispose了。

b.  保存成png时报错。

转载自` https://www.cnblogs.com/guangfengli/p/5920104.html `

.Net获取配置信息

做开发时,需要读取配置文件信息。.Net项目已经有配置文件,比如:Web.config,App.config,要读取这些配置文件里的信息,需要引用`System.Configuration`

以appSettings为例,使用`System.Configuration.ConfigurationManager.AppSettings[keyName] ` 即可获取配置信息