windows c# winforms:确定程序的第一次运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/210944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# winforms: determine first run of program
提问by P a u l
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... 的配置文件中。我需要在任何重新安装后运行向导,所以我需要重置该设置。我是否需要使用自定义安装程序操作来执行此操作?
回答by Guy Starbuck
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 注册表似乎是此类设置的合适位置。安装程序步骤可以在用户重新安装时重置密钥,或者如果您不想在安装之间保留任何设置,则可以在卸载时清除注册表项。
回答by MusiGenesis
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.
如果用户卸载然后重新安装您的应用程序,他们将在第一次运行时再次看到该向导。
回答by Franci Penov
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 用户使用同一个应用程序的情况下,每个用户的真/假设置将永远无法正常工作。安装程序仅作为 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:
您需要的是安装的机器范围时间戳和运行 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 注册表中为您的应用添加时间戳。对于每个用户,当应用程序启动时,在您上面提到的每用户设置文件中检查第一次运行向导 (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。
回答by Dustin Townsend
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.
此列表可以存储在机器级配置文件或应用程序目录中。重新安装应用程序时,此列表可能会被清除。
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。如果用户在列表中,请跳过配置向导。如果用户不在列表中,则显示配置向导。
回答by Gishu
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,则显示 Wizard 并设置为 false。
每次安装时,文件的副本都应该被覆盖,因此您将获得 FirstRun=true。向导将在每次(重新)安装后运行
回答by Rory
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.
On first-run for a user, compare that value to 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.
安装时,使用新生成的 GUID 创建注册表值 HKLM\Software\YourCompany\YourApp\InstallId。
在用户首次运行时,将该值与 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.
这具有不易受时间变化影响的(微小)优势。
回答by Jon B
You could create a file in the program directory. The uninstaller won't remove this, since it wasn't added by the installer.
您可以在程序目录中创建一个文件。卸载程序不会删除它,因为它不是由安装程序添加的。
回答by Pavel Chuchuva
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?
我建议更改程序的行为,并且在重新安装后不要重置配置设置。毕竟用户已经做出了选择,为什么还要问同样的问题呢?