以静默模式自动启动 ClickOnce 应用程序

发布于 2024-07-27 16:23:35 字数 497 浏览 5 评论 0原文

我有一个使用 ClickOnce 进行部署的应用程序,并且我已设法在以下时间启动该应用程序:用户登录。

我的问题是我需要应用程序以隐藏方式启动(我不希望用户必须将其最小化,直到他们需要它 - 我希望它位于系统托盘中)。 在使用 ClickOnce 之前,我只是检查参数以查看是否传入了“/silent”。在 ClickOnce 应用程序中似乎无法执行此操作(您可以检查是否传入了 URI 查询字符串,但因为这是从 .appref-ms 快捷方式运行似乎无法获取 /silent 参数)

我如何获取 /silent 参数,或者如何判断是否应用程序是由于用户登录而启动的(而不是用户从快捷方式启动应用程序)?

重要的是,无论提出什么解决方案都不需要管理员权限,但使用的语言并不重要,重要的是我可以将其移植到 .NET。

I have an application that uses ClickOnce to deploy, and I have managed to get the application starting when a user logs in.

My issues are that I need the application to start up hidden (I don't want a user to have to minimize it until they require it - I want it to sit in the system tray). Prior to using ClickOnce I simply checked the arguments to see if "/silent" was passed in. There appears to be no way to do this in a ClickOnce application (you can check if a URI query string is passed in, but because this is run from a .appref-ms shortcut there appears to be no way to get the /silent argument)

How can I get the /silent argument, or how can I tell if the application has started as a result of the user logging on (rather than the user starting the application from a shortcut)?

It is important that whatever solution proposed doesn't require administrator permissions, but the language used isn't as important as I can probably port it to .NET.

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

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

发布评论

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

评论(2

浮光之海 2024-08-03 16:23:35

您可以这样做:

在您的 Main 方法中:

if ((args.Length > 0 && args[0].ToLower() == "minimized") ||
    (AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null && AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Length > 0 &&
    AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0] == "minimized"))//ClickOnce arguments
{

    //My code to start minimized. My system tray is always visible
    main.WindowState = FormWindowState.Minimized;
    main.Hide();
    main.ShowInTaskbar = false;
}
else {

    //Code to start normally
    main.WindowState = FormWindowState.Normal;
    main.ShowInTaskbar = true;
    main.Show();
}

然后您只需通过 ClickOnce 应用程序传递“最小化”参数即可将其最小化。

为了自动启动我的 ClickOnce 应用程序,我创建了一个如下所示的快捷方式:

CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + @"\LVH Tools\MyMiniTools.appref-ms", Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\MyMiniTools", "minimized")

“MyMiniTools”是应用程序的名称,“LVH Tools”是发布者名称。

CreateShortcut:

    public void CreateShortcut(string destinationPath, string shortcutPath, string arguments = "")
    {
        IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();

        shortcutPath = Path.ChangeExtension(shortcutPath, "lnk");

        IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);

        shortcut.TargetPath = destinationPath;
        shortcut.IconLocation = destinationPath;
        shortcut.Arguments = arguments;

        shortcut.Description = Path.GetFileNameWithoutExtension(destinationPath);

        shortcut.Save();
    }

使用 ClickOnce 启用自动启动的另一种方法在 ClickOnce 应用程序自动启动和彻底卸载或自定义 ClickOnce 安装的方式

You can do it this way:

In your Main-method:

if ((args.Length > 0 && args[0].ToLower() == "minimized") ||
    (AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null && AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Length > 0 &&
    AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0] == "minimized"))//ClickOnce arguments
{

    //My code to start minimized. My system tray is always visible
    main.WindowState = FormWindowState.Minimized;
    main.Hide();
    main.ShowInTaskbar = false;
}
else {

    //Code to start normally
    main.WindowState = FormWindowState.Normal;
    main.ShowInTaskbar = true;
    main.Show();
}

Then you can just pass the argument 'minimized' with the ClickOnce-application to start it minimized.

To start my ClickOnce application automatically, I make a shortcut like this:

CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + @"\LVH Tools\MyMiniTools.appref-ms", Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\MyMiniTools", "minimized")

'MyMiniTools' is the name of the application, and 'LVH Tools' is the Publisher name.

CreateShortcut:

    public void CreateShortcut(string destinationPath, string shortcutPath, string arguments = "")
    {
        IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();

        shortcutPath = Path.ChangeExtension(shortcutPath, "lnk");

        IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);

        shortcut.TargetPath = destinationPath;
        shortcut.IconLocation = destinationPath;
        shortcut.Arguments = arguments;

        shortcut.Description = Path.GetFileNameWithoutExtension(destinationPath);

        shortcut.Save();
    }

Another method to enable autostart with ClickOnce is explained in ClickOnce application autostart and clean uninstall or the way to customize ClickOnce installation.

寂寞清仓 2024-08-03 16:23:35

我认为没有一种非常干净的方法可以做到这一点,因为命令参数不起作用,并且您不能使用查询字符串参数。

但是,请尝试使用 ClickOnce 部署来部署另一个小型可执行文件。 它将负责设置“启动”标志(在配置文件、注册表等中),然后启动实际的应用程序。 您的应用程序将检查该标志以确定是否应以静默方式启动,然后重置该标志。 然后,您只需让小型可执行文件从 Windows 启动,而不是您的主应用程序。

当然我没有尝试过这些。

I don't think there's a very clean way to do this since command arguments don't work, and you can't use query string arguments.

However, try deploying another small executable with your ClickOnce deployment. It would be responsible for setting a "startup" flag (in a configuration file, registry, whatever) and then launching your actual application. Your application would check the flag to determine if it should launch silently and then reset the flag. Then you would just have the small executable start with Windows rather than your main application.

Of course I didn't try any of this out.

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