2009年1月19日星期一

MSI(Microsoft Installer)二三事

最近跟MSI(Microsoft Installer)打了不少交道,下面是关于MSI的一点儿知识:

1. The Windows Installer (previously known as Microsoft Installer[1]) is an engine for the installation, maintenance, and removal of software on modern Microsoft Windows systems. -- Wikipedia
2. msi: Windows系统下的安装文件的格式
    msiexec:在Windows下执行安装的程序
    msiserver:运行msiexec会打开msiserver服务;这个服务会在安装完毕10分钟之后自动关闭
3. 使用msiexec的方法:
    msiexec /?                    查看msiexec的使用方法
    msiexec /i SoftwareInstaller.msi 安装SoftwareInstaller.msi
    msiexec /i SoftwareInstaller.msi /lv log.txt 安装SoftwareInstaller.msi,并将安装过程的日志保存在log.txt
    msiexec /i SoftwareInstaller.msi /quiet       以默认的参数静默安装,即安装过程没有任何提示
4. 结束未完成的安装的过程的方法:
    net stop msiserver
    第三四条命令可以在自动化安装脚本(.bat/.cmd)中用到
5. 在系统的隐藏文件夹%WinDir%\Installer下找到本机缓存的安装文件(.msi)。估计控制面板中的“添加删除程序”就是使用这里的.msi来做uninstallation的。在注册表的HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall下可以查看到反安装时所需要的各种信息
    

2009年1月15日星期四

用C#从注册表中读取%ProgramFiles%的值

%ProgramFiles%是Windows上软件的默认安装路径,常见的值是C:\Program Files或者D:\Program Files。这个值是在注册表中有记录的。如果你的C#程序想读取这个值,可以用下面的代码:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingMicrosoft.Win32;
namespaceConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
RegistryKey folders = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\");
if (folders == null)
Console.WriteLine("null");
else
{
string defaultInstallPath = folders.GetValue("ProgramFilesDir") as string;
Console.WriteLine(defaultInstallPath);
}
}
}
}

注意不能写成@"\SOFTWARE\Microsoft\Windows\CurrentVersion\"。