c#winforms:确定第一次运行程序

时间:2021-10-09 01:25:26

I need to present a wizard once, the first time my windows forms application is run after install. I could use a user setting like firstrun = false I suppose. But I also need handle the case where the program is uninstalled, then reinstalled. How will that user setting be reset? It's already present in the config file in ...\Users--user--\AppData\Roaming... for that user. I need the wizard to run after any reinstalls so I need to reset that setting. Do I need to do this with a custom installer action?

我需要提交一个向导,第一次我的Windows窗体应用程序在安装后运行。我可以使用像firstrun = false这样的用户设置。但是我还需要处理卸载程序然后重新安装的情况。如何重置该用户设置?它已存在于该用户的... \ Users - user - \ AppData \ Roaming ...的配置文件中。我需要在重新安装后运行向导,所以我需要重置该设置。我是否需要使用自定义安装程序操作执行此操作?

8 个解决方案

#1


2  

It's probably best to have your installer create the FirstRun key in the registry and set it to true (or 1 or whatever), and make sure your uninstaller deletes this key entirely. Then just have your application read this key when it starts, and set it to false (or 0 or whatever).

最好让安装程序在注册表中创建FirstRun密钥并将其设置为true(或1或其他),并确保卸载程序完全删除此密钥。然后让你的应用程序在启动时读取此键,并将其设置为false(或0或其他)。

If the user uninstalls and then reinstalls your application, they'll see the wizard again the first time they run it.

如果用户卸载并重新安装您的应用程序,他们将在第一次运行时再次看到该向导。

#2


2  

The Windows registry seems like the appropriate place for this type of setting. An installer step could reset the key when the user reinstalls, or you could just clear out the registry key on uninstall if you don't want to keep any settings between installations.

Windows注册表似乎是适合此类设置的地方。安装程序步骤可以在用户重新安装时重置密钥,或者如果您不想在安装之间保留任何设置,则可以在卸载时清除注册表项。

#3


2  

A per-user true/false setting will never work properly in the case of multiple Windows users using the same app. The installer is run only once as one of the Windows users and it will not have access to the per-user settings for all other users on that machine.

对于使用相同应用程序的多个Windows用户,每用户true / false设置将永远不会正常工作。安装程序仅作为一个Windows用户运行一次,并且无法访问该计算机上所有其他用户的每用户设置。

You could have a per-machine flag that would be set to true on install. However, if an admin user runs FRW and changes it, no other user will get FRW. If an non-admin user runs FRW, they won't be able to change it and will run FRW on next app run again.

您可以在安装时将每台机器标志设置为true。但是,如果管理员用户运行FRW并更改它,则其他用户不会获得FRW。如果非管理员用户运行FRW,他们将无法更改它并将在下次应用程序运行时再次运行FRW。

What you need is a machine-wide timestamp of the isntallation and a per-user timestamp of when FRW was run. Here's the scenario:

您需要的是机器范围的isntallation时间戳和FRW运行时的每用户时间戳。这是场景:

On install, add a timestamp in the HKLM registry for your app. For each user, when the app is started check the timestamp of the first run wizzard (FRW) in the per-user settings file you mentioned above. If the per-user timestamp is older than the HKLM install stamp, run the FRW for that user and update the per-user settings file.

在安装时,在应用程序的HKLM注册表中添加时间戳。对于每个用户,启动应用程序时,请检查上面提到的每用户设置文件中第一个运行wizzard(FRW)的时间戳。如果每用户时间戳早于HKLM安装戳记,请为该用户运行FRW并更新每用户设置文件。

If the app is uninstalled and then installed again, the installer will update the HKLM timestamp, thus causing the FRW to be run for all users again.

如果卸载该应用程序然后再次安装,则安装程序将更新HKLM时间戳,从而导致再次为所有用户运行FRW。

#4


1  

Use a name-value pair like FirstRun=true in a settings file or a resx file. Read this file on startup, if true, show Wizard and set to false.
Each time you install, the copy of the file should be overwritten and hence you will get FirstRun=true. The wizard will run after every (re)install

在设置文件或resx文件中使用名称 - 值对,如FirstRun = true。在启动时读取此文件,如果为true,则显示向导并设置为false。每次安装时,都应该覆盖该文件的副本,因此您将获得FirstRun = true。每次(重新)安装后,向导都会运行

#5


1  

One could store a list of users that have already run the configuration wizard.

可以存储已经运行配置向导的用户列表。

This list could be stored in a machine level configuration file or in the app directory. When the app is reinstalled this list could be cleared out.

此列表可以存储在计算机级配置文件或app目录中。重新安装应用程序后,可以清除此列表。

Instead of looking at FirstRun, you would just check the current user with the list. If the user is in the list skip the configuration wizard. If the user is not in the list show the configuration wizard.

您只需使用列表检查当前用户,而不是查看FirstRun。如果用户在列表中,请跳过配置向导。如果用户不在列表中,则显示配置向导。

#6


0  

You could create a file in the program directory. The uninstaller won't remove this, since it wasn't added by the installer.

您可以在程序目录中创建一个文件。卸载程序不会删除它,因为安装程序未添加它。

#7


0  

I would suggest changing your program's behaviour and do not reset configuration settings after reinstall. After all, user already made his or her choice, why ask the same question again?

我建议更改程序的行为,重新安装后不要重置配置设置。毕竟,用户已经做出了他或她的选择,为什么再次提出同样的问题呢?

#8


0  

Similar to @Franci Penov's suggestion, I'd do it like this:

类似于@Franci Penov的建议,我会这样做:

  • At install, create a registry value HKLM\Software\YourCompany\YourApp\InstallId using a newly generated GUID.

    在安装时,使用新生成的GUID创建注册表值HKLM \ Software \ YourCompany \ YourApp \ InstallId。

  • On first-run for a user, compare that value to HKCU\Software\YourCompany\YourApp\InstallId.

    在首次运行时,将该值与HKCU \ Software \ YourCompany \ YourApp \ InstallId进行比较。

  • If the HKCU value doesn't exist or they're different, run your first-run logic and then copy HKLM\Software\YourCompany\YourApp\InstallId to HKCU\Software\YourCompany\YourApp\InstallId.

    如果HKCU值不存在或者它们不同,请运行首次运行的逻辑,然后将HKLM \ Software \ YourCompany \ YourApp \ InstallId复制到HKCU \ Software \ YourCompany \ YourApp \ InstallId。

This has the (tiny) advantage of not being susceptible to time changes.

这具有不易受时间变化影响的(微小)优势。

#1


2  

It's probably best to have your installer create the FirstRun key in the registry and set it to true (or 1 or whatever), and make sure your uninstaller deletes this key entirely. Then just have your application read this key when it starts, and set it to false (or 0 or whatever).

最好让安装程序在注册表中创建FirstRun密钥并将其设置为true(或1或其他),并确保卸载程序完全删除此密钥。然后让你的应用程序在启动时读取此键,并将其设置为false(或0或其他)。

If the user uninstalls and then reinstalls your application, they'll see the wizard again the first time they run it.

如果用户卸载并重新安装您的应用程序,他们将在第一次运行时再次看到该向导。

#2


2  

The Windows registry seems like the appropriate place for this type of setting. An installer step could reset the key when the user reinstalls, or you could just clear out the registry key on uninstall if you don't want to keep any settings between installations.

Windows注册表似乎是适合此类设置的地方。安装程序步骤可以在用户重新安装时重置密钥,或者如果您不想在安装之间保留任何设置,则可以在卸载时清除注册表项。

#3


2  

A per-user true/false setting will never work properly in the case of multiple Windows users using the same app. The installer is run only once as one of the Windows users and it will not have access to the per-user settings for all other users on that machine.

对于使用相同应用程序的多个Windows用户,每用户true / false设置将永远不会正常工作。安装程序仅作为一个Windows用户运行一次,并且无法访问该计算机上所有其他用户的每用户设置。

You could have a per-machine flag that would be set to true on install. However, if an admin user runs FRW and changes it, no other user will get FRW. If an non-admin user runs FRW, they won't be able to change it and will run FRW on next app run again.

您可以在安装时将每台机器标志设置为true。但是,如果管理员用户运行FRW并更改它,则其他用户不会获得FRW。如果非管理员用户运行FRW,他们将无法更改它并将在下次应用程序运行时再次运行FRW。

What you need is a machine-wide timestamp of the isntallation and a per-user timestamp of when FRW was run. Here's the scenario:

您需要的是机器范围的isntallation时间戳和FRW运行时的每用户时间戳。这是场景:

On install, add a timestamp in the HKLM registry for your app. For each user, when the app is started check the timestamp of the first run wizzard (FRW) in the per-user settings file you mentioned above. If the per-user timestamp is older than the HKLM install stamp, run the FRW for that user and update the per-user settings file.

在安装时,在应用程序的HKLM注册表中添加时间戳。对于每个用户,启动应用程序时,请检查上面提到的每用户设置文件中第一个运行wizzard(FRW)的时间戳。如果每用户时间戳早于HKLM安装戳记,请为该用户运行FRW并更新每用户设置文件。

If the app is uninstalled and then installed again, the installer will update the HKLM timestamp, thus causing the FRW to be run for all users again.

如果卸载该应用程序然后再次安装,则安装程序将更新HKLM时间戳,从而导致再次为所有用户运行FRW。

#4


1  

Use a name-value pair like FirstRun=true in a settings file or a resx file. Read this file on startup, if true, show Wizard and set to false.
Each time you install, the copy of the file should be overwritten and hence you will get FirstRun=true. The wizard will run after every (re)install

在设置文件或resx文件中使用名称 - 值对,如FirstRun = true。在启动时读取此文件,如果为true,则显示向导并设置为false。每次安装时,都应该覆盖该文件的副本,因此您将获得FirstRun = true。每次(重新)安装后,向导都会运行

#5


1  

One could store a list of users that have already run the configuration wizard.

可以存储已经运行配置向导的用户列表。

This list could be stored in a machine level configuration file or in the app directory. When the app is reinstalled this list could be cleared out.

此列表可以存储在计算机级配置文件或app目录中。重新安装应用程序后,可以清除此列表。

Instead of looking at FirstRun, you would just check the current user with the list. If the user is in the list skip the configuration wizard. If the user is not in the list show the configuration wizard.

您只需使用列表检查当前用户,而不是查看FirstRun。如果用户在列表中,请跳过配置向导。如果用户不在列表中,则显示配置向导。

#6


0  

You could create a file in the program directory. The uninstaller won't remove this, since it wasn't added by the installer.

您可以在程序目录中创建一个文件。卸载程序不会删除它,因为安装程序未添加它。

#7


0  

I would suggest changing your program's behaviour and do not reset configuration settings after reinstall. After all, user already made his or her choice, why ask the same question again?

我建议更改程序的行为,重新安装后不要重置配置设置。毕竟,用户已经做出了他或她的选择,为什么再次提出同样的问题呢?

#8


0  

Similar to @Franci Penov's suggestion, I'd do it like this:

类似于@Franci Penov的建议,我会这样做:

  • At install, create a registry value HKLM\Software\YourCompany\YourApp\InstallId using a newly generated GUID.

    在安装时,使用新生成的GUID创建注册表值HKLM \ Software \ YourCompany \ YourApp \ InstallId。

  • On first-run for a user, compare that value to HKCU\Software\YourCompany\YourApp\InstallId.

    在首次运行时,将该值与HKCU \ Software \ YourCompany \ YourApp \ InstallId进行比较。

  • If the HKCU value doesn't exist or they're different, run your first-run logic and then copy HKLM\Software\YourCompany\YourApp\InstallId to HKCU\Software\YourCompany\YourApp\InstallId.

    如果HKCU值不存在或者它们不同,请运行首次运行的逻辑,然后将HKLM \ Software \ YourCompany \ YourApp \ InstallId复制到HKCU \ Software \ YourCompany \ YourApp \ InstallId。

This has the (tiny) advantage of not being susceptible to time changes.

这具有不易受时间变化影响的(微小)优势。