把第三方程序打包在assembly里妥凳, 運(yùn)行時(shí)釋放并調(diào)用啊送, 兩種方法
- 使用resx文件
- 創(chuàng)建一個(gè)resx文件扶踊,如MyResources.resx. 把想要添加的exe干发, dll啥的放進(jìn)去绞愚, 右擊Run Custom Tool叙甸, vs自動(dòng)生成MyResources.Designer.cs.
添加代碼釋放
var mgr = new ResourceManager(typeof(MyResources)); //MyResources 類(lèi)在MyResources.Designer.cs里
bool Extract(string name)
{
var buff = (byte[])mgr.GetObject(name.Split('.').First()); // 添加進(jìn)resx的文件名稱是沒(méi)有擴(kuò)展名的
if (buff == null)
return false;
using (var file = new FileStream(name, FileMode.Create))
{
file.Write(buff, 0, buff.Length);
return true;
}
}
- 運(yùn)行三方程序
async Task Run(string exe, string args)
{
var si = new ProcessStartInfo
{
FileName = Path.GetFullPath(exe),
Arguments = args,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
};
await Task.Run(() =>
{
var p = new Process {StartInfo = si};
p.OutputDataReceived += (sender, e) => Log.Info(e.Data);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
if (p.ExitCode != 0)
Log.Error($"{exe} return errors.");
});
}
- Application.GetContentStream(Uri) (wpf only)
- 工程里建文件夾, 如Resources, 添加文件位衩,屬性設(shè)置Build Action為Content
- 用Application.GetContentStream(Uri)釋放指定的文件裆蒸。
- 剩下的同第一種方法