10

安装 ClickOnce 应用程序时,程序会在安装后运行。运行就可以安装吗?

我知道我可以使用设置和部署项目并创建安装程序,但我更喜欢使用 ClickOnce。

4

3 回答 3

3

我猜你可以伪造它。引入一个“IsInstalled”布尔属性,默认为 false。然后在 Program.cs 中,将 Main() 方法更改为如下所示:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    if (!Properties.Settings.Default.IsInstalled)
    {
        Properties.Settings.Default.IsInstalled = true;
        Properties.Settings.Default.Save();

        MessageBox.Show("Install Complete");
        return;
    }

    Application.Run(new Form1());
}

因此,现在当应用程序首次安装时,它会检查该属性并向用户显示一条消息,然后退出。

如果您想变得棘手,那么您可以查看解析部署的激活 URI 并具有一个 URI 参数,该参数指定程序是在首次安装时运行还是静默关闭。

于 2009-02-04T23:14:07.287 回答
0

您可以通过在Mage中编辑应用程序清单来做到这一点。有一个复选框可以在安装后停止应用程序运行。

如果您不习惯手动或使用 Mage 编辑清单,那么您可以使用内置部署类来检查这是否是应用程序第一次运行。

using System.Deployment.Application

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    if (ApplicationDeployment.CurrentDeployment.IsFirstRun)
    {
        MessageBox.Show("Install Complete");
        return;
    }

    Application.Run(new Form1());
}
于 2009-03-02T10:29:28.417 回答
0

在尝试了所有建议的解决方案但仍然遇到相同的问题后,我摆弄了一段时间,并将几个解决方案组合成一个真正有效的解决方案。

仅设置“isInstalled”属性的问题是升级后会保留该值,因此每次安装新版本时,它都会再次运行应用程序。但是使用应用程序清单文件和 Mage 只是为了解决这个小问题,工作量太大而且太复杂。

所以我所做的是获取应用程序运行版本的当前构建#,将其保存到一个属性中,然后每次都根据运行版本检查该属性。这是有效的,因为每次发布都会增加版本号。

1) 更改您的程序集版本以在 AssemblyInfo.cs 中使用通配符:

[assembly: AssemblyVersion("1.0.*")]

2) 如果在构建时引发“确定性”错误,请打开您的 .csproj 文件并在 PropertyGroup 部分中将确定性设置为false

<Deterministic>false</Deterministic>

3)添加这个防呆功能来获取正在运行的汇编版本:

private Version GetRunningVersion()
{
    try
    {
        return System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
    }
    catch
    {
        return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
    }
}

4) 在项目的属性中,打开设置选项卡,并添加一个名为lastVersion (String, User) 的设置。将值留空。

5) 添加此属性以用于确定这是否是应用程序安装后第一次运行。

private bool isFirstRun
{
    get { return Properties.Settings.Default.lastVersion != GetRunningVersion().ToString(); }
}

6)然后在您的代码中,在检查 isFirstRun后添加:

if (isFirstRun)
{
    Properties.Settings.Default.lastVersion = GetRunningVersion().ToString();
    Properties.Settings.Default.Save();
}
于 2020-04-02T21:12:26.670 回答