当前位置:科技动态 > vs2010打包.bat程序_vs2010 setup 打包 安装 BAT批处理实现自动安装软件功能

vs2010打包.bat程序_vs2010 setup 打包 安装 BAT批处理实现自动安装软件功能

  • 发布:2023-09-19 22:18

废话不多说,直接搞起,打开vs2010,建立一个安装项目,如下图 点击setup1项目,然后点击右侧的属性 会出现如下界面,你可以在里面设置公司名称、产品名称、安装语言等: 设置好后,便可以添加待安装的文件,右击项目->视图->文件系统,会出现文件系统,右击“应用系统文件夹 ”添加待打包的文件, 给打包程序建立快捷方式:如下图: 在这里可以给程序建立一个卸载快捷键,右击“应用程序文件夹”添加文件“msiexec.exe”并建立快捷键,如下图: 右击“用户的程序菜单”,添加一个文件夹,并将先前建立快捷键移动到此文件夹内,此文件夹在安装包安装后会出现在开始菜单,再按照之前的方法建立一个程序启动快捷键,移动至“用户桌面”目录下;这是需要给这些快捷键添加图标,具体操作如下图: 除此之外,需要给卸载快捷键加一个额外的操作,进入卸载快捷键属性窗口,在”arguement”中添加“/x {FF5D1DDF-C30B-452A-83F7-9626E7BCB436}”其中“{FF5D1DDF-C30B-452A-83F7-9626E7BCB436}”为setup1属性窗口中的productcode: 这时,待打包的文件及对其的操作便进行好了,我们还需对setup1做一些操作,右击setup1,选择属性,设置系统必备(注:这里根据个人需求去添加): 如果还需要设置启动条件,这里以设置.net4.0为启动条件为例,右击setup1,选择“视图->启动条件”弹出启动条件界面,右击“启动条件”可以添加启动条件,这里选择.net启动条件,右击属性,设置version为.net4.0。这时,启动条件便设置好。 这时简单的安装包便制作好了,如果你想在安装过程中或卸载过程中做一些处理,则需要进行如下操作: 1、在解决方案下新建一个类库项目,并在类库项目中添加一个安装程序类,具体命名可以自己定:具体操作可见下面代码:

using System;

using System.Collections;

using System.Collections.Generic;

using System.ComponentModel;

using System.Configuration.Install;

using System.Linq;

using www.sychzs.cn32;

using www.sychzs.cn;

using System.Diagnostics;

namespace HisignInstaller

{

[RunInstaller(true)]

public partial class HisignInstaller : System.Configuration.Install.Installer

{

///

/// 标志是否首次添加注册表

///

private bool isFirstAdd = true;

string sourcePath = "empty";

string path = string.Empty;

public string zippath = string.Empty;

public string presetConfigFile = string.Empty;

public HisignInstaller()

{

InitializeComponent();

}

protected override void OnAfterInstall(IDictionary savedState)

{

try

{

//是在安装部署项目里自定义操作的属性中通过设置CustomActionData属性为/Path="[TARGETDIR]\"得到的,就是用户选择的路径。

path = Context.Parameters["Path"];

path = path.TrimEnd('\'');

if (path.EndsWith("\\\\"))

{

path = path.Replace("\\\\", "\\");

}

int a = path.IndexOf("\'");

// int b = path.LastIndexOf("\"");

path = path.Substring(a + 1, path.Length - a - 1);

//MSI目录,CustomActionData属性为/SourcePath="[SOURCEDIR]\"

sourcePath = Context.Parameters["SourcePath"].TrimEnd('\'');

sourcePath = sourcePath.TrimEnd('\\') + "\\";

int sourcea = sourcePath.IndexOf("\'");

//int sourceb = sourcePath.LastIndexOf("\"");

sourcePath = sourcePath.Substring(sourcea + 1, sourcePath.Length - sourcea - 1);

string[] presetConfigFileArr = Directory.GetFiles(sourcePath, "Configuation*.*", SearchOption.TopDirectoryOnly);

string configFile = Path.Combine(path, "HisignVideo\\Client\\Configuation.xml");

if (presetConfigFileArr != null && presetConfigFileArr.Length > 0) //存在预置配置文件,直接拷贝

{

try

{

presetConfigFile = presetConfigFileArr[0];

File.Copy(presetConfigFile, configFile, true);//将安装包同级目录拷贝到安装目录

}

catch (Exception ex)

{

}

}

string appPath = Path.Combine(path, "HisignVideo\\Client\\ConDemoWPF.exe");

string appkey = "Hisign\\ConDemoWPF";

string _key = "AppPath";

AddDecodersReg(appPath, appkey, _key);//写注册表

string ZipPath = Path.Combine(path, "HisignVideo\\www.sychzs.cn");

zippath = Path.Combine(path, "HisignVideo");

bool IsSuccess = UnZipCommonMethod(ZipPath);

if (IsSuccess)

{

string filePath = Path.Combine(path, "HisignVideo\\decoders");

string DestPath = GetChromeConfigurePath();

if (!Directory.Exists(DestPath))

{

Directory.CreateDirectory(DestPath);

}

try

{

CopyFiles(filePath, DestPath, true, true);

string appkeyDecode = "Visystem\\SoftCodec";

string _keyDecode = "PlugPath";

DestPath = DestPath + "\\";

AddDecodersReg(DestPath, appkeyDecode, _keyDecode);

string BatFile = Path.Combine(path, "HisignVideo\\reg_decoder.bat");

if (File.Exists(BatFile))

{

RunBat(BatFile);

}

ChangeFileAttributes(filePath);

Directory.Delete(filePath, true);

}

catch (Exception ex)

{

}

}

}

catch (Exception ex)

{

}

finally

{

}

}

///

/// 更改文件属性

///

/// 文件夹路径

private void ChangeFileAttributes(string dirPath)

{

try

{

DirectoryInfo thisDic = new DirectoryInfo(dirPath);

FileInfo[] fileInfo = thisDic.GetFiles();

foreach (FileInfo NextFile in fileInfo) //遍历文件

{

File.SetAttributes(NextFile.FullName, FileAttributes.Normal);

}

foreach (DirectoryInfo NextDic in thisDic.GetDirectories())

{

ChangeFileAttributes(NextDic.FullName);

}

}

catch (Exception ex)

{

}

}

///

/// 解压文件

///

///

bool UnZipCommonMethod(string fileName)

{

try

{

//presetConfigFile = presetConfigFile.Substring(0, presetConfigFile.LastIndexOf("\\"));

string unZipExe = Path.Combine(path, "HisignVideo\\7z.exe");

ZipFile zipFile = new ZipFile(unZipExe);

string outPut = string.Empty;

bool isSucess = zipFile.UnCompress(fileName, zippath, out outPut);

return isSucess;

}

catch (Exception ex)

{

return false;

}

}

//判断操作系统是否为WindowsXP

public static bool IsWindowsXP

{

get

{

return (Environment.OSVersion.Platform == www.sychzs.cn32NT)

&& (Environment.OSVersion.Version.Major == 5)

&& ((Environment.OSVersion.Version.Minor == 1)

|| (Environment.OSVersion.Version.Minor == 2));

}

}

///

/// 获取浏览器配置文件根目录

///

///

public static string GetChromeConfigurePath()

{

string filePath = string.Empty;

if (IsWindowsXP)

{

filePath = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Application Data\\decoders");

}

else

{

filePath = Path.Combine(Environment.GetEnvironmentVariable("AppData"), "decoders");

}

return filePath;

}

///

/// 运行bat文件

///

///

public static void RunBat(string batPath)

{

Process pro = new Process();

FileInfo file = new FileInfo(batPath);

pro.StartInfo.WorkingDirectory = file.Directory.FullName;

pro.StartInfo.FileName = batPath;

pro.StartInfo.UseShellExecute = false;

pro.StartInfo.CreateNoWindow = true;

pro.StartInfo.Verb = "runas";

pro.Start();

pro.WaitForExit();

}

///

/// 复制指定目录的所有文件

///

/// 原始目录

/// 目标目录

/// 如果为true,覆盖同名文件,否则不覆盖

/// 如果为true,包含子目录,否则不包含

public static void CopyFiles(string sourceDir, string targetDir, bool isOverWrite, bool isCopySubDir)

{

try

{

//复制当前目录文件

foreach (string sourceFileName in Directory.GetFiles(sourceDir))

{

string targetFileName = Path.Combine(targetDir, Path.GetFileName(sourceFileName));

if (File.Exists(targetFileName))

{

if (isOverWrite)

{

File.SetAttributes(targetFileName, FileAttributes.Normal);

File.Copy(sourceFileName, targetFileName, isOverWrite);

}

}

else

{

File.Copy(sourceFileName, targetFileName, isOverWrite);

}

}

//复制子目录

if (isCopySubDir)

{

foreach (string sourceSubDir in Directory.GetDirectories(sourceDir))

{

string targetSubDir = Path.Combine(targetDir, sourceSubDir.Substring(sourceSubDir.LastIndexOf("\\") + 1));

if (!Directory.Exists(targetSubDir))

Directory.CreateDirectory(targetSubDir);

CopyFiles(sourceSubDir, targetSubDir, isOverWrite, true);

}

}

}

catch (Exception ex)

{

}

}

///

/// 将安装目录写入注册表

///

/// 安装根目录

private void AddDecodersReg(string basePath, string keypath, string key)

{

try

{

bool is64 = www.sychzs.cn64BitOperatingSystem;

if (is64)

{

Reg64Decoders(basePath, keypath, key);

}

else

{

Reg32Decoders(basePath, keypath, key);

}

}

catch (Exception ex)

{

}

}

///

/// 64位系统修改注册表

///

///

private void Reg64Decoders(string basePath, string pcontent, string Key)

{

string decodersPath = basePath;

string pathHead = "SOFTWARE\\WOW6432Node";

//string pathContent = "Hisign\\ConDemoWPF";

string pathContent = pcontent;

// string keyVal = "AppPath";

string keyVal = Key;

RegistryKey key = null;

RegistryKey software = null;

RegistryKey softCodec = null;

try

{

key = Registry.LocalMachine;

software = key.OpenSubKey(pathHead, true);

if (software == null)

{

key.CreateSubKey(pathHead);

software = key.OpenSubKey(pathHead);

}

softCodec = software.OpenSubKey(pathContent, true);

if (softCodec == null)

{

software.CreateSubKey(pathContent);

softCodec = software.OpenSubKey(pathContent);

}

softCodec.SetValue(keyVal, decodersPath, RegistryValueKind.String);

softCodec.Close();

software.Close();

key.Close();

}

catch (Exception ex)

{

//64位的首次添加可能失败,需在添加一次

if (isFirstAdd)

{

isFirstAdd = false;

if (softCodec != null)

{

softCodec.Close();

}

if (software != null)

{

software.Close();

}

if (key != null)

{

key.Close();

}

Reg64Decoders(basePath, pcontent, Key);

return;

}

if (softCodec != null)

{

softCodec.Close();

}

if (software != null)

{

software.Close();

}

if (key != null)

{

key.Close();

}

}

}

///

/// 32位系统修改注册表

///

///

private void Reg32Decoders(string basePath, string pcontent, string Key)

{

string decodersPath = basePath;

// string pathReg = "SOFTWARE\\Hisign\\ConDemoWPF";

string pathReg = "SOFTWARE\\" + pcontent;

// string keyVal = "AppPath";

string keyVal = Key;

RegistryKey key = null;

RegistryKey software = null;

try

{

key = Registry.LocalMachine;

software = key.OpenSubKey(pathReg, true);

if (software == null)

{

key.CreateSubKey(pathReg);

software = key.OpenSubKey(pathReg, true);

}

software.SetValue(keyVal, decodersPath, RegistryValueKind.String);

software.Close();

key.Close();

}

catch (Exception ex)

{

if (software != null)

{

software.Close();

}

if (key != null)

{

key.Close();

}

}

}

}

}

这段代码是我在安装包安装过程中做的一些操作,如拷贝与安装包同级目录下的文件到安装目录下,将程序的装路径写入注册表等, 如果你想在卸载时也做一些处理,也可以建立一类库并添加一个安装程序类,卸载处理的代码如下:

using System;

using System.Collections;

using System.Collections.Generic;

using System.ComponentModel;

using System.Configuration.Install;

using System.Linq;

using www.sychzs.cn32;

using www.sychzs.cn;

using System.Diagnostics;

using System.ServiceProcess;

namespace HisignUnInstaller

{

[RunInstaller(true)]

public partial class HisignUnInstaller : System.Configuration.Install.Installer

{

object pathValue = new object();

public HisignUnInstaller()

{

InitializeComponent();

}

protected override void OnBeforeUninstall(IDictionary savedState)

{

try

{

Process[] _process = Process.GetProcesses();

foreach (Process ps in _process)

{

if (ps.ProcessName.Equals("ConDemoService"))

{

ps.Kill();

}

}

using (ServiceController sc = new ServiceController("ConAnalysisService"))

{

if (sc != null)

{

if (sc.Status.Equals(ServiceControllerStatus.Running))

{

sc.Stop();

}

}

}

}

catch (Exception ex)

{

}

base.OnBeforeUninstall(savedState);

}

protected override void OnAfterUninstall(System.Collections.IDictionary savedState)

{

base.OnBeforeUninstall(savedState);

try

{

bool is64 = www.sychzs.cn64BitOperatingSystem;

if (is64)

{

pathValue = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Hisign\\ConDemoWPF", "AppPath", null);

}

else

{

pathValue = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Hisign\\ConDemoWPF", "AppPath", null);

}

if (pathValue != null)

{

string pathReg = "Hisign\\ConDemoWPF";

DeleteDecodersReg(pathReg);

}

}

catch (Exception ex)

{

}

finally

{

}

base.OnAfterUninstall(savedState);

}

///

/// 将安装目录从注册表中删除

///

/// 安装根目录

private void DeleteDecodersReg(string basePath)

{

bool is64 = www.sychzs.cn64BitOperatingSystem;

if (is64)

{

Delete64Decoders(basePath);

}

else

{

Delete32Decoders(basePath);

}

}

///

/// 32位系统删除注册表

///

private void Delete32Decoders(string pathReg)

{

try

{

string keyVal = "AppPath";

RegistryKey key = null;

RegistryKey software = null;

RegistryKey softhisignkey = null;

RegistryKey softsmallkey = null;

string softpath = "SOFTWARE";

string[] _pathReg = pathReg.Split('\\');

key = Registry.LocalMachine;

software = key.OpenSubKey(softpath, true);

if (software == null)

{

return;

}

softhisignkey = software.OpenSubKey(_pathReg[0], true);

if (softhisignkey == null)

{

return;

}

softsmallkey = softhisignkey.OpenSubKey(_pathReg[1], true);

if (softsmallkey == null)

{

return;

}

if (!IsRegeditKeyExist(softsmallkey, keyVal))

{

return;

}

softsmallkey.DeleteValue(keyVal);

softsmallkey.Close();

softhisignkey.Close();

software.Close();

}

catch (Exception ex)

{

}

}

///

/// 64位系统删除注册表

///

///

private void Delete64Decoders(string pathReg)

{

string keyVal = "AppPath";

RegistryKey key = null;

RegistryKey software = null;

pathReg = "SOFTWARE\\WOW6432Node\\" + pathReg;

key = Registry.LocalMachine;

software = key.OpenSubKey(pathReg, true);

if (software == null)

return;

//IsRegeditItemExist(software)

if (!IsRegeditKeyExist(software, keyVal))

return;

software.DeleteValue(keyVal);

software.Close();

}

///

/// 判断键值是否存在

///

///

///

///

private bool IsRegeditKeyExist(RegistryKey RegBoot, string RegKeyName)

{

string[] subkeyNames;

try

{

subkeyNames = RegBoot.GetValueNames();

foreach (string keyName in subkeyNames)

{

if (keyName.Equals(RegKeyName)) //判断键值的名称

{

return true;

}

}

return false;

}

catch (Exception ex)

{

return false;

}

}

}

}

这里只是去删除注册表,关闭某些服务等。我在这只是给出一些实例,大家可以根据自己的需求去写里面的代码。当然大家不要以为做了这些在程序包安装或卸载时就可以执行大家想做的处理了,这里还需要额外的操作,具体如下: 1、右击“setup1”选择“添加->项目输出”,会弹出一界面,选中建的类库,作为“主输出”点击确定 2、右击“setup1”选择“视图->自定义操作”,会弹出自定义操作界面,如果你想在安装时做一些处理,右击“安装”,选择“添加自定义”,如下图: 在查找范围中选择“应用程序文件夹”选中“主输出来自Installer1(活动)”点击确定,添加自定义成功,右击此自定义操作,选择属窗口,在“CustomActiondata”中添加“/Path=”’[TARGETDIR]\’” /SourcePath=”’[SOURCEDIR]\’””。最后点击生成,即完成打包所有操作。

?

CLS@echo offECHO.ECHO 安装 Diskeeper 7.0.428ECHO 请稍等...start /wait %systemdrive%\install\Applications\diskeeper\Setup.exe /s /v/qn

ECHO.ECHO 更新 Diskeeper 7.0.428 到 7.0.430ECHO 请稍等...start /wait %systemdrive%\install\Applications\diskeeper\us_dk70_wup_build430.exe /s /v/qn

ECHO.ECHO 安装 Flashget 1.4ECHO 请稍等...start /wait %systemdrive%\install\Applications\flashget\fgf140.exe /S

ECHO.ECHO 安装 Symantec Antivirus Corporate 8.1

ECHO 请稍等...start /wait %systemdrive%\install\Applications\sav\sav810b821.exe /qnECHO.EXIT

用记事本输入,保存为*.bat。具体路径自己更改,每空一行就是单独的一个。如果在dos下,变量只用%,作成批处理,就要用%%。特别说明:“安装Windows 补丁”这个批处理有点不妥,因为Windows 补丁有两种类型,一种是简单的可执行文件,即没有图标的,另一种是有图标的,所以改用另一种批处理(/r参数表示包括子文件夹。"_sfx_cab_exe_path"是特征字符串,关于特征字符串地获得:可以用记事本打开两种不同的补丁,找出两文本不同的字符,可能用fc命令也可以。

for /r %%f in (*.exe) do @((@findstr _sfx_cab_exe_path "%%f" >nul && @start /wait %%f /u /q /z) || @start /wait %%f /q)

qchain.exe

ECHO 正在安装 Windows 补丁,请稍等...

cd e:\hotfix

for %a in (*.exe) do start /wait %a -z -q

qchain.exe

ECHO.

ECHO 正在安装 MSN Messenger 6.0,请稍等...

start /wait e:\software\messenger\MsnMsgs.msi /QB

ECHO.

ECHO 正在安装 DirectX 9.0b,请稍等...

start /wait e:\software\dx9\dxsetup.exe /install /silent

ECHO.

ECHO 正在安装 Windows Media Player 9,请稍等...

start /wait e:\software\WMP9XP.exe /Q:A /R:N

ECHO.

ECHO 正在安装 Movie Maker 2.0,请稍等...

start /wait e:\software\wmm2\mm20.msi /qn

ECHO.

ECHO 正在安装 .NET Framework v1.1,请稍等...

start /wait e:\software\NetFramework\netfx.msi /QB

ECHO.

ECHO 正在安装 Microsoft JavaVM,请稍等...

start /wait e:\software\msjavwu.exe /Q:A /R:N

ECHO.

ECHO 正在安装 ISOBuster,请稍等……

regedit /s e:\software\isobuster.reg

start /wait e:\software\IsoBuster.exe /verysilent

ECHO.

ECHO 正在安装AD-aware 6,请稍等……

start /wait e:\software\Adaware.exe /s

ECHO.

ECHO 正在安装Winamp,请稍等……

start /wait e:\software\winamp.exe /S

ECHO.

ECHO 正在安装Adobe Reader 6,请稍等……

start /wait e:\software\AdbeRdr60.exe -p"-s /v\"/qn\""

ECHO.

ECHO 正在安装Nero Burning ROM 6,请稍等……

Regedit /s e:\software\nero.reg

start /wait e:\software\Nero6009.exe /silent /noreboot

ECHO.

ECHO 正在安装WinRAR,请稍等……

start /wait e:\software\wrar320.exe /S

ECHO.

ECHO 正在安装 Flashget,请稍等……

start /wait e:\software\flashget.exe /S

ECHO.

ECHO 正在安装 Symantec Antivirus Corporate 8.1,请稍等……

start /wait e:\software\nav81chs.exe /qn

ECHO.

ECHO 正在安装 ZoneAlarm Free,请稍等……

start /wait e:\software\ZoneAlarm.exe /s /noreboot

?

选择Splash属性-->配置属性-->常规,选择

选择C/C++-->代码生成,选择

然后重新编译这个程序。

下面开始打包自己写的Splash程序

新建一个项目,选择??安装项目

写上你程序的名字,这个是生成安装程序的名字,点击确定

右击

添加-->文件,选择刚刚编译出的程序的可执行文件,我的是Splash.exe,并把这个程序所有相关的库选择进来。

相关文章