1、选中附件,右键生成操作选择 嵌入的资源,例如:handle.exe
2、FileUtil
1 using System.IO; 2 using System.Reflection; 3 4 namespace ResourceOccupancyHelp 5 { 6 public class FileUtil 7 { 8 /// <summary> 9 /// 从资源文件中抽取资源文件 10 /// </summary> 11 /// <param name="resFileName">资源文件名称(资源文件名称必须包含目录,目录间用“.”隔开,最外层是项目默认命名空间)</param> 12 /// <param name="outputFile">输出文件</param> 13 public static void ExtractResFile(string resFileName, string outputFile) 14 { 15 BufferedStream inStream = null; 16 FileStream outStream = null; 17 try 18 { 19 Assembly asm = Assembly.GetExecutingAssembly(); //读取嵌入式资源 20 inStream = new BufferedStream(asm.GetManifestResourceStream(resFileName)); 21 outStream = new FileStream(outputFile, FileMode.Create, FileAccess.Write); 22 23 byte[] buffer = new byte[1024]; 24 int length; 25 26 while ((length = inStream.Read(buffer, 0, buffer.Length)) > 0) 27 { 28 outStream.Write(buffer, 0, length); 29 } 30 outStream.Flush(); 31 } 32 finally 33 { 34 if (outStream != null) 35 { 36 outStream.Close(); 37 } 38 if (inStream != null) 39 { 40 inStream.Close(); 41 } 42 } 43 } 44 } 45 }
3、调用demo
FileUtil.ExtractResFile("ResourceOccupancyHelp.handle.exe", System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "handle.exe"));
特别注意,生成的exe需要用命名空间最前缀,如果有文件夹的话也需要完整加上。