写程序时候引用dll是再正常不过的事情了,如果引用的不是系统自带的dll,部署到其他机器就必须带着dll一起,只有.exe文件是用不了的。
对于有些用户来说,让他准备dll什么的根本不现实,只要软件运行不了就是我们的锅。那么能不能像一些绿色软件一样,做成一个自解压的exe,自带dll呢?
答案是可以的,实现方法如下:
1、把编译好的exe和依赖dll一起塞进压缩包
比如说上面这个,我们把编译好的engin.exe和依赖的dll全放到一起,确认engin.exe可以正常运行,然后把它们打包成一个zip文件:
2、将压缩包作为内置资源塞进安装程序
新建一个控制台程序SelfUnzip,右键点击项目属性,翻到资源那页,将前一步的压缩包添加为资源引用
成功的话资源管理器里面可以看到压缩包,现在使用Properties.Resources.ScriptedCAD就可以引用到压缩包。
引用ICSharpCode.SharpZipLib.dll和System.IO之后就可以用C#代码进行zip文件的压缩与解压。下面这段代码可以将压缩包中的内容全部解压到安装程序所在的目录。
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace SelfUnzip
{
class Program
{
static void Main(string[] args)
{
#region 解压文件
Stream readStream = new MemoryStream(Properties.Resources.ScriptedCAD);
ZipInputStream zipInputStream = new ZipInputStream(readStream);
ZipEntry zipEntryFromZippedFile = zipInputStream.GetNextEntry();
while (zipEntryFromZippedFile != null)
{
if (zipEntryFromZippedFile.IsFile)
{
FileInfo fInfo = new FileInfo(zipEntryFromZippedFile.Name);
if (!fInfo.Directory.Exists) fInfo.Directory.Create();
FileStream file = fInfo.Create();
byte[] bufferFromZip = new byte[zipInputStream.Length];
zipInputStream.Read(bufferFromZip, 0, bufferFromZip.Length);
file.Write(bufferFromZip, 0, bufferFromZip.Length);
file.Close();
}
zipEntryFromZippedFile = zipInputStream.GetNextEntry();
}
zipInputStream.Close();
#endregion
}
}
}
看起来好像问题解决了?是不是忘了什么?
点一下编译,然后去看看生成的文件吧
锵锵!这是什么!
ICSharpCode.SharpZipLib不是系统自带的dll!自解压程序还要自备zip的库才行。
3、自备ICSharpCode.SharpZipLib
只要SelfUnzip的main()当中用到了ICSharpCode,那么编译后的exe在启动时必定会检查是否有ICSharpCode.SharpZipLib.dll。因此只好把解压的工作从main()中独立出来,让main()只负责准备ICSharpCode.SharpZipLib.dll,另外准备一个ziphelper来完成解压工作。
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace SelfUnzip
{
public class Ziphelper
{
public Ziphelper()
{
}
public void Run()
{
#region 解压文件
Stream readStream = new MemoryStream(Properties.Resources.ScriptedCAD);
ZipInputStream zipInputStream = new ZipInputStream(readStream);
ZipEntry zipEntryFromZippedFile = zipInputStream.GetNextEntry();
while (zipEntryFromZippedFile != null)
{
if (zipEntryFromZippedFile.IsFile)
{
FileInfo fInfo = new FileInfo(zipEntryFromZippedFile.Name);
if (!fInfo.Directory.Exists) fInfo.Directory.Create();
FileStream file = fInfo.Create();
byte[] bufferFromZip = new byte[zipInputStream.Length];
zipInputStream.Read(bufferFromZip, 0, bufferFromZip.Length);
file.Write(bufferFromZip, 0, bufferFromZip.Length);
file.Close();
}
zipEntryFromZippedFile = zipInputStream.GetNextEntry();
}
zipInputStream.Close();
#endregion
}
}
}
同样的把ICSharpCode.SharpZipLib.dll作为内置资源塞进来
然后把主程序代码改写
using System.IO;
namespace SelfUnzip
{
class Program
{
static void Main(string[] args)
{
#region 准备ICSharpCode
string SharpDll = "ICSharpCode.SharpZipLib.dll";
FileStream fs = new FileStream(SharpDll, FileMode.Create, FileAccess.Write);
byte[] buffer = Properties.Resources.ICSharpCode_SharpZipLib;
fs.Write(buffer, 0, buffer.Length);
fs.Close();
#endregion
#region 解压压缩包
Ziphelper zip = new Ziphelper();
zip.Run();
#endregion
}
}
}
经过测试,这样生成的exe是一个自带dll的自解压包,非常适合玩不来电脑的用户使用。



