把第三方程序打包在assembly里, 运行时释放并调用, 两种方法
- 使用resx文件
- 创建一个resx文件,如MyResources.resx. 把想要添加的exe, dll啥的放进去, 右击Run Custom Tool, vs自动生成MyResources.Designer.cs.
- 添加代码释放
-
var mgr = new ResourceManager(typeof(MyResources)); //MyResources 类在MyResources.Designer.cs里 bool Extract(string name) { var buff = (byte[])mgr.GetObject(name.Split('.').First()); // 添加进resx的文件名称是没有扩展名的 if (buff == null) return false; using (var file = new FileStream(name, FileMode.Create)) { file.Write(buff, 0, buff.Length); return true; } }
- 运行三方程序
-
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, 添加文件,属性设置Build Action为Content
- 用Application.GetContentStream(Uri)释放指定的文件。
- 剩下的同第一种方法