当应用程序数据文件被删除时,如何防止安装程序运行?

发布于 2024-12-02 20:04:41 字数 580 浏览 1 评论 0原文

我有一个使用 Visual Studio 2005 用 VB.Net 编写的应用程序。该应用程序允许用户创建和保存项目文件。当我分发应用程序时,我会包含一些演示项目文件,并将其安装在公共应用程序数据文件夹中。

XP - C:\Documents and Settings\All Users\Application Data

Vista 和 Windows XP 7 - C:\Program Data

我发现了一个意外行为 - 如果删除了公共应用程序数据文件夹中的任何文件,并且从开始菜单运行该应用程序,则安装过程将启动并尝试恢复丢失的文件(s)。如果 MSI 文件不再存在于其原始位置或已更改,则应用程序将无法运行。我认为这是一个“功能”,但这是我不想要的。谁能告诉我发生了什么事以及如何避免它?

更多详细信息:

  • 我使用 Visual Studio 部署创建了安装包 项目。

  • 如果我直接启动 EXE,则不会发生此行为。我 因此,期望该行为与 开始菜单快捷方式。我注意到该快捷方式不是普通的 快捷方式 - 它没有“目标位置”。

感谢所有建议。

-TC

I have an application written in VB.Net with Visual Studio 2005. The application allows the user to create and save project files. When I distribute the application, I include some demo project files, which I install in the common application data folder.

XP - C:\Documents and Settings\All Users\Application Data

Vista & 7 - C:\Program Data

I have discovered an unexpected behavior -- if any file in the common application data folder is removed, and the application is run from the start menu, then the install procedure will start and attempt to restore the missing file(s). If the MSI file no longer exists at its original location or has been changed, then the application will fail to run. I perceive that this is a "feature", but it is one I don't want. Can anyone tell me what is going on and how I can avoid it?

Some more details:

  • I created the setup package by using a Visual Studio deployment
    project.

  • This behavior will not occur if I launch the EXE directly. I
    expect, therefore, that the behavior has something to do with the
    start menu shortcut. I've noticed that the shortcut isn't a normal
    shortcut -- it doesn't have a "Target Location".

All advice is appreciated.

-TC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坚持沉默 2024-12-09 20:04:41

我了解到这种行为涉及所谓的“按需安装”(又名“自我修复”)。安装包创建的不寻常的快捷方式称为“广告快捷方式”。现在我已经知道了问题的名称,就可以轻松找到有关如何解决该问题的信息。值得注意的是:

这些页面包含大量信息。为了方便其他可能偶然发现这篇文章的人,我将总结他们所说的话:

广告快捷方式是特殊的快捷方式,可以做一些奇特的事情。最值得注意的是,他们在启动目标之前重新安装损坏的应用程序。关于它们是善、恶还是无害存在一些争论。在我看来,他们做了大多数用户意想不到的事情,这让他们变得邪恶。因此,我想为我的应用程序禁用它们。

Visual Studio 安装项目会自动创建 MSI 包,默认情况下会生成广告快捷方式。通过使用 DISABLEADVTSHORTCUTS=1 作为 Setup.exe 的命令行参数来安装 MSI 包时,可以轻松覆盖该默认值。此外,使用 Orca 等实用程序,您可以通过插入 DISABLEADVTSHORTCUTS=1 作为 MSI 的属性来手动更改默认值。但是,如果您希望 Visual Studio 自动创建不创建广告快捷方式的 MSI 包,那就更难了。我是这样做的:

  1. 首先,我使用上面链接之一中 Gary Chang 提供的 DisableAdvt 代码创建了一个 VBS 文件(我在下面重复了该代码)。只需创建一个文本文件,粘贴代码即可。并将其另存为DisableAdvt.vbs。

  2. 然后,为您的安装项目创建一个构建后事件。确切的语法取决于您的文件位置。因为我的DisableAdvt.vbs位于解决方案文件夹的“Tools”子文件夹中,所以我的构建后事件如下所示:

    • “$(ProjectDir)..\Tools\DisableAdvt\DisableAdvt.vbs”“$(BuiltOuputPath)”

就这样我必须这么做。它就像一个魅力。

-TC

一些注意事项:

在 Visual Studio 2005 中,安装项目的构建事件的访问方式与其他类型的项目的访问方式不同。单击解决方案资源管理器中的项目名称,然后在“属性”窗格中查找 PostBuildEvent。

Orca 是一个实用程序,可用于手动将 DISABLEADVTSHORTCUTS 属性插入到 MSI 文件中。按照我的方法,Orca 是不必要的。但是,它对于验证构建事件是否正在进行预期的更改很有用。

在构建事件中,拼写错误的“BuiltOuputPath”是故意的。

这是 Gary Chang 的 DisableAdvt.vbs 代码(请注意,我修正了第 21 行的拼写错误——非常重要!):

Option Explicit

Const msiOpenDatabaseModeTransact = 1
Dim argNum, argCount:argCount = Wscript.Arguments.Count

Dim openMode : openMode =  msiOpenDatabaseModeTransact

' Connect to Windows installer object
On Error Resume Next
Dim installer : Set installer = Nothing
Set installer = Wscript.CreateObject("WindowsInstaller.Installer") :
CheckError

' Open database
Dim databasePath:databasePath = Wscript.Arguments(0)
Dim database : Set database = installer.OpenDatabase(databasePath, openMode) : CheckError

' Process SQL statements
Dim query, view, record, message, rowData, columnCount, delim, column

query = "INSERT INTO Property(Property, Value) VALUES ('DISABLEADVTSHORTCUTS', '1')"  
Set view = database.OpenView(query) : CheckError
view.Execute : CheckError

database.Commit

If Not IsEmpty(message) Then Wscript.Echo message
Wscript.Quit 0

Sub CheckError
  Dim message, errRec
  If Err = 0 Then Exit Sub
  message = Err.Source & " " & Hex(Err) & ": " & Err.Description
  If Not installer Is Nothing Then
    Set errRec = installer.LastErrorRecord
    If Not errRec Is Nothing Then message = message & vbLf & errRec.FormatText
  End If
Fail message
End Sub

Sub Fail(message)
  Wscript.Echo message
  Wscript.Quit 2
End Sub

I have learned that this behavior involves something called "Install-on-Demand" (aka "Self Heal"). The unusual shortcuts created by the setup package are called "Advertised Shortcuts". Now that I have a name for the problem, it is easy to find information on how to fix it. Notably:

Those pages contain a wealth of information. For the convenience of others who may stumble upon this post, I will summarize what they say:

Advertised shortcuts are special shortcuts which do some fancy things. Most notably, they reinstall damaged application before launching their target. There is some debate over whether they are good, evil, or harmless. In my opinion, they do something most users don't expect, and that makes them evil. Therefore, I'd like to disable them for my application.

Visual Studio setup projects automatically create MSI packages which generate advertised shortcuts by default. It is easy to override that default when installing the MSI package by using DISABLEADVTSHORTCUTS=1 as a command-line argument for Setup.exe. Also, with a utility like Orca, you can manually change the default by inserting DISABLEADVTSHORTCUTS=1 as a property of the MSI. However, if you want Visual Studio to automatically create MSI packages which don't create advertised shortcuts, that is harder. I did it this way:

  1. First, I created a VBS file using the DisableAdvt code provided by Gary Chang in one of the links above (I've repeated that code below). Just create a text file, paste in the code. and save it as DisableAdvt.vbs.

  2. Then, create a post-build event for your setup project. The exact syntax will depend on your file locations. Because my DisableAdvt.vbs is in a "Tools" subfolder of the solution folder, my post-build event looks like this:

    • "$(ProjectDir)..\Tools\DisableAdvt\DisableAdvt.vbs" "$(BuiltOuputPath)"

That's all I had to do. It works like a charm.

-TC

Some notes:

In Visual Studio 2005, Build events are accessed differently for setup projects than they are for other types of projects. Click on the project name in the solution explorer, then look for PostBuildEvent in the Properties pane.

Orca is a utility that can be used to manually insert the DISABLEADVTSHORTCUTS property into the MSI file. With my approach, Orca is not necessary. However, it is useful for verifying that the build event is making the expected change.

In the build event, the misspelling "BuiltOuputPath" is intentional.

Here is Gary Chang's DisableAdvt.vbs code (note that I fixed a typo on line 21 -- Very important!):

Option Explicit

Const msiOpenDatabaseModeTransact = 1
Dim argNum, argCount:argCount = Wscript.Arguments.Count

Dim openMode : openMode =  msiOpenDatabaseModeTransact

' Connect to Windows installer object
On Error Resume Next
Dim installer : Set installer = Nothing
Set installer = Wscript.CreateObject("WindowsInstaller.Installer") :
CheckError

' Open database
Dim databasePath:databasePath = Wscript.Arguments(0)
Dim database : Set database = installer.OpenDatabase(databasePath, openMode) : CheckError

' Process SQL statements
Dim query, view, record, message, rowData, columnCount, delim, column

query = "INSERT INTO Property(Property, Value) VALUES ('DISABLEADVTSHORTCUTS', '1')"  
Set view = database.OpenView(query) : CheckError
view.Execute : CheckError

database.Commit

If Not IsEmpty(message) Then Wscript.Echo message
Wscript.Quit 0

Sub CheckError
  Dim message, errRec
  If Err = 0 Then Exit Sub
  message = Err.Source & " " & Hex(Err) & ": " & Err.Description
  If Not installer Is Nothing Then
    Set errRec = installer.LastErrorRecord
    If Not errRec Is Nothing Then message = message & vbLf & errRec.FormatText
  End If
Fail message
End Sub

Sub Fail(message)
  Wscript.Echo message
  Wscript.Quit 2
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文