C#释放嵌入的资源

2020-06-06 · 1 阅读
释放lib目录下的嵌入的资源 for (int i = 0; i < Assembly.GetExecutingAssembly().GetManifestResourceNames().Length; i++) { string name = Assembly.GetExecutingAssembly().GetManifestResourceNames()[i]; if(name.IndexOf("览器.lib.")>0) { string file_name = name.Replace("浏览器.lib.", ""); //判断文件的存在 if (!System.IO.File.Exists(file_name)) { ExtractResFile(name, file_name); } } } //释放资源 private bool ExtractResFile(string resFileName, string outputFile) { BufferedStream inStream = null; FileStream outStream = null; try { Assembly asm = Assembly.GetExecutingAssembly(); //读取嵌入式资源 inStream = new BufferedStream(asm.GetManifestResourceStream(resFileName)); outStream = new FileStream(outputFile, FileMode.Create, FileAccess.Write); byte[] buffer = new byte[1024]; int length; while ((length = inStream.Read(buffer, 0, buffer.Length)) > 0) { outStream.Write(buffer, 0, length); } outStream.Flush(); return true; } catch { return false; } finally { if (outStream != null) outStream.Close(); if (inStream != null) inStream.Close(); } }