将文件从程序集中复原

1、场景:

发到客户那的程序中使用的一个C++的库需要被替换,而该库在使用了前使用了md5进行检验防止其它伪装的库将其替换,因而替换时要算目标库的

md5,并把使用该库的另一个库也换掉。

2、涉及技术:

做一个离线补丁包去升级程序,并将库文件集成到其中,程序运行时再将其释放出来。

3、解决方法:

将目标库当成资源添加到工程中,并在需要时调用它写入文件

 1         private bool GetFileFromAssembly(String fileName, String targetFilePath)
 2         {
 3             byte[] bs = null;
 4             String fileNameWithoutExtension = fileName.Substring(0, fileName.Length - 4);
 5             MemoryStream ms = null;
 6             FileStream fs = null;
 7 
 8             try
 9             {
10                 bs = (byte[])Properties.Resources.ResourceManager.GetObject(fileNameWithoutExtension);
11                 ms = new MemoryStream(bs);
12                 fs = new FileStream(targetFilePath, FileMode.Create);
13                 ms.WriteTo(fs);
14             }
15             catch (System.Exception ex)
16             {
17                 return false;
18             }
19             finally
20             {
21                 ms.Close();
22                 fs.Close();
23             }
24             return true;
25         }

4、相关程序:

 

posted on 2014-12-25 19:19  siszqsqx  阅读(240)  评论(0编辑  收藏  举报

导航