.net 如何在启动时运行一次单击一次部署的应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/401816/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:54:44  来源:igfitidea点击:

How can I make a Click-once deployed app run at startup?

.netclickoncestartup

提问by Eric Haskins

How can I make a Click-once deployed app run a startup?

如何让一次点击部署的应用程序运行启动?

The best option I found by searching was to set the Publisher on the app to Startup, so the Start menu shortcut would be placed in the Startup folder, but that seems like a huge hack and I would like there to be a Start menu icon people can find.

我通过搜索找到的最佳选择是将应用程序上的发布者设置为启动,因此开始菜单快捷方式将放置在启动文件夹中,但这似乎是一个巨大的黑客,我希望有一个开始菜单图标可以找到。

What options do I have?

我有哪些选择?

回答by Don Scott

I feel that adding your application to the startup folder is unprofessional. I strongly recommend using a startup registry key to launch your application.

我觉得将你的应用程序添加到启动文件夹是不专业的。我强烈建议使用启动注册表项来启动您的应用程序。

Contrary to what a lot of the material on this topic says, it is extremely simple to setup a key to launch a click once application and does not require setting up additional shortcuts. You simply use the shortcut created on install:

与该主题的许多材料所说的相反,设置一个键来启动单击一次应用程序非常简单,并且不需要设置额外的快捷方式。您只需使用安装时创建的快捷方式:

// The path to the key where Windows looks for startup applications
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey(
                    @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);

//Path to launch shortcut
string startPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs) 
                   + @"\YourPublisher\YourSuite\YourProduct.appref-ms";

rkApp.SetValue("YourProduct", startPath);

回答by DiscDev

After reading all the comments on this thread and the johnnycoder blog post mentioned above, I came up with a solution that:

在阅读了关于这个线程的所有评论和上面提到johnnycoder 博客文章后,我想出了一个解决方案:

  1. Adds your ClickOnce app to the Startup folder
  2. Removes the Startup item automatically when the ClickOnce app is uninstalled (after a reboot or log out/log in)
  3. Was tested and works on Windows XP, Windows 7, Windows Server 2000/2003, Windows 8
  1. 将您的 ClickOnce 应用程序添加到启动文件夹
  2. 卸载 ClickOnce 应用程序时(重新启动或注销/登录后)自动删除启动项
  3. 经过测试,可在 Windows XP、Windows 7、Windows Server 2000/2003、Windows 8 上运行

My Solution

我的解决方案

Basically, your app will be writing a .batfile to the Startup folder that launches the ClickOnce app for you. The .batfile is smart enough to detect if the app has been uninstalled and will delete itself if the ClickOnce app cannot be found.

基本上,您的应用程序会将.bat文件写入启动文件夹,为您启动 ClickOnce 应用程序。该.bat文件足够智能,可以检测应用程序是否已被卸载,如果找不到 ClickOnce 应用程序,它将自行删除。

Step 1

第1步

Get the batch file working. Replace PUBLISHER_NAME and APPLICATION_NAME with the right values. You can find them by installing your ClickOnce app, then following the path to it on your file system:

让批处理文件工作。用正确的值替换 PUBLISHER_NAME 和 APPLICATION_NAME。您可以通过安装 ClickOnce 应用程序,然后按照文件系统上的路径找到它们:

@echo off

IF EXIST "%appdata%\Microsoft\Windows\Start Menu\Programs\PUBLISHER_NAME\APPLICATION_NAME.appref-ms" (
"%appdata%\Microsoft\Windows\Start Menu\Programs\PUBLISHER_NAME\APPLICATION_NAME.appref-ms"
) ELSE (start /b "" cmd /c del "%~f0"&exit /b)

The batch file will check if your ClickOnce app is installed (by seeing if the appref-ms file exists) and launch it if so. Otherwise, the batch file deletes itself, via a method outlined here.

批处理文件将检查您的 ClickOnce 应用程序是否已安装(通过查看 appref-ms 文件是否存在),如果存在则启动它。否则,批处理文件会通过此处概述的方法自行删除。

Now that you have the batch file, test it out. Drop it in your Startup folder to make sure it launches your app on login.

现在您有了批处理文件,请对其进行测试。将它放在您的启动文件夹中,以确保它在登录时启动您的应用程序。

Step 2

第2步

Now, in the code for your app, you need to write this batch file to the Startup folder. Here is an example using the batch file above in C# (note that there is some escaping, and environment variable voodoo happening):

现在,在您的应用程序代码中,您需要将此批处理文件写入 Startup 文件夹。这是一个在 C# 中使用上述批处理文件的示例(请注意,有一些转义和环境变量 voodoo 发生):

string[] mystrings = new string[] { @"@echo off

IF EXIST ""%appdata%\Microsoft\Windows\Start Menu\Programs\PUBLISHER_NAME\APPLICATION_NAME.appref-ms"" (
""%appdata%\Microsoft\Windows\Start Menu\Programs\PUBLISHER_NAME\APPLICATION_NAME.appref-ms""
) ELSE (start /b """" cmd /c del ""%~f0""&exit /b)"};

string fullPath = "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\StartMyClickOnceApp.bat";

//Expands the %appdata% path and writes the file to the Startup folder
System.IO.File.WriteAllLines(Environment.ExpandEnvironmentVariables(fullPath), mystrings);

There you have it. Comments / improvements welcomed.

你有它。欢迎评论/改进。

EDIT: Fixed quotes in step 2

编辑:第 2 步中的固定引号

回答by DiscDev

All these tricks dont work on Vista unfortunately. Vista blocks these programs on startup for some reason.

不幸的是,所有这些技巧都不适用于 Vista。由于某些原因,Vista 在启动时会阻止这些程序。

As suggested by @thijs, you can easily bypass vista's "security" on this one. See blogpost on how to run clickonce apps on windows startup.

正如@thijs 所建议的,您可以轻松绕过vista 的“安全性”。请参阅有关如何在 Windows 启动时运行 clickonce 应用程序的博文

回答by Ivan Leonenko

Well there're a number of ways to make your application launch on start up, but there's a clean up problem. Even if you use start up registry key and it looks fine, anyway you should clean all you added to thesystem. You can take a look on my article, I faced same clean up problem and used automation and cutom uninstall file to solve issue.

有很多方法可以让您的应用程序在启动时启动,但是有一个清理问题。即使您使用启动注册表项并且它看起来不错,但无论如何您都应该清除添加到系统中的所有内容。你可以看看我的文章,我遇到了同样的清理问题,并使用自动化和 cutom 卸载文件来解决问题。

回答by user3029922

Thanks DiscDev and dbenham. The solution works great. Just wanted to share the updated working code based on the latest by dbenham.

感谢 DiscDev 和 dbenham。该解决方案效果很好。只是想分享基于 dbenham 最新的更新工作代码。

 const string fullPathEnvVar =
            "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\StartMyClickOnceApp.bat";
 //Expands the %appdata% path
 string fullPath = Environment.ExpandEnvironmentVariables(fullPathEnvVar);

if (!File.Exists(fullPath))
        {
            string[] mystrings =
            {
                @"@echo off 
if exist ""%appdata%\Microsoft\Windows\Start Menu\Programs\<PublisherName>\<ApplicationName>.appref-ms"" (
""%appdata%\Microsoft\Windows\Start Menu\Programs\<PublisherName>\<ApplicationName>.appref-ms""
) else (
(goto) 2>nul & del ""%~f0""
)"
            };
            //write the file to the Startup folder
            File.WriteAllLines(fullPath, mystrings);
        }

回答by Fabian V?ppling

First off thanks for the answer Discdev. To get this to work with ? ? ? and other special characters this modification did it for me, using UTF-8 a different code page and no BOM.

首先感谢您的回答 Discdev。让这个工作?? ? 和其他特殊字符,这个修改为我做了,使用 UTF-8 不同的代码页,没有 BOM。

string[] mystrings = new string[] { "chcp 65001", @"IF EXIST ""%appdata%\Microsoft\Windows\Start Menu\Programs\<Publisher>\<App_Name>.appref-ms"" (""%appdata%\Microsoft\Windows\Start Menu\Programs\<Publisher>\<App_Name>.appref-ms"") ELSE (start /b """" cmd /c del ""%~f0""&exit /b)" };
string fullPath = "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\StartErrandDynamicMenu.bat";
System.Text.Encoding utf8WithoutBOM = new System.Text.UTF8Encoding(false);
System.IO.File.WriteAllLines(Environment.ExpandEnvironmentVariables(fullPath), mystrings, utf8WithoutBOM);

回答by JaredPar

In terms of actually getting the application to launch at startup, having a link in the startup folder is your best bet. Or if not the startup folder then the startup reg key.

就实际让应用程序在启动时启动而言,在启动文件夹中有一个链接是最好的选择。或者,如果不是启动文件夹,则是启动注册表项。

A way to work around not having the Icon in it's normal position is to have the application place a link to itself into the startup folder on Application startup. ClickOnce apps will run the first time they are installed. The application can use this startup to place a link in the Startup folder. Now the link will be in both places and you should be golden.

解决图标不在其正常位置的一种方法是让应用程序在应用程序启动时将其自身的链接放置到启动文件夹中。ClickOnce 应用程序将在第一次安装时运行。应用程序可以使用此启动在启动文件夹中放置一个链接。现在链接将在两个地方,你应该是金色的。

There is the issue though that now deleting the ClickOnce app will no longer actually delete it. ClickOnce will not track the manual link added and hence every time someone un-installs your app and reboots it will re-install. I would start considering that program to not be behaving well :(.

但存在一个问题,即现在删除 ClickOnce 应用程序将不再实际删除它。ClickOnce 不会跟踪添加的手动链接,因此每次有人卸载您的应用程序并重新启动时,它都会重新安装。我会开始考虑该程序表现不佳:(。

回答by bhollis

You could add your app to the appropriate "Run" startup registry key at startup. Then even though you can't remove it when your app is deleted, it won't hurt anything and nobody will see the broken reference.

您可以在启动时将您的应用添加到相应的“运行”启动注册表项中。这样,即使您在删除应用程序时无法将其删除,也不会造成任何伤害,也没有人会看到损坏的引用。

回答by sipwiz

In case it helps anyone else still looking for a solution to this a comment way down the bottom http://johnnycoder.com/blog/2009/02/24/clickonce-run-at-startup/suggests setting the ClickOnce Publish options (in VS.Net 2010 that's on the project properties, publish screen and options) with Publisher name set to Startup and Suite name left blank. That does get the program shortcut into the startup folder for me on Windows 7. I don't know what other versions of Windows do.

如果它帮助其他人仍然在寻找解决方案,请在底部http://johnnycoder.com/blog/2009/02/24/clickonce-run-at-startup/建议设置 ClickOnce Publish 选项(在项目属性、发布屏幕和选项上的 VS.Net 2010 中),发布者名称设置为启动,套件名称留空。这确实让我在 Windows 7 上的启动文件夹中的程序快捷方式。我不知道其他版本的 Windows 是做什么的。

回答by Alex Martins

I make this and work for me

我做这个并对我来说有效

 Sub AddToStartup()
    If My.Application.IsNetworkDeployed Then


        Dim str As String = My.Application.Deployment.UpdatedApplicationFullName.ToString

        Dim saida As String = ""

        For i As Integer = 0 To str.Split(",").Length

            If Not saida.Contains("msil") Then
                saida += str.Split(",")(i) & ", "
            End If
        Next

        If saida.Contains("msil") Then
            saida = saida.Substring(0, saida.Length - 2)
        End If

        Dim name As String = My.Application.Info.Title
        Dim file As String = Path.Combine(My.Application.Info.DirectoryPath, name & ".appref-ms")
        My.Computer.FileSystem.WriteAllText(file, saida, False)

        Dim reg As RegistryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
        reg.SetValue(name, file)

    End If


End Sub