ClickOnce 应用程序不接受命令行参数

发布于 2024-12-07 11:39:04 字数 304 浏览 0 评论 0原文

我有一个采用命令行参数的 VB.NET 应用程序。

如果我关闭 Visual Studio 的 ClickOnce 安全设置,调试时它可以正常工作。

当我尝试通过 ClickOnce 在计算机上安装应用程序并尝试使用参数运行它时,会出现此问题。当这种情况发生时我会崩溃(哦不!)。

此问题有一个解决方法:将文件从最新版本的发布文件夹移动到计算机的 C: 驱动器,并从 .exe 中删除“.deploy”。从 C: 驱动器运行应用程序,它将很好地处理参数。

有没有比我上面的解决方法更好的方法来让它发挥作用?

谢谢!

I have a VB.NET application that takes command-line arguments.

It works fine when debugging provided I turn off Visual Studio's ClickOnce security setting.

The problem occurs when I try to install the application on a computer via ClickOnce and try to run it with arguments. I get a crash when that happens (oh noes!).

There is a workaround for this issue: move the files from the latest version's publish folder to a computer's C: drive and remove the ".deploy" from the .exe. Run the application from the C: drive and it will handle arguments just fine.

Is there a better way to get this to work than the workaround I have above?

Thanks!

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

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

发布评论

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

评论(1

梦里寻她 2024-12-14 11:39:04

“命令行参数”仅适用于从 URL 运行的 ClickOnce 应用程序。

例如,您应该这样启动应用程序以附加一些运行时参数:

http://myserver/install/MyApplication.application?argument1=value1&参数2=值2

我有以下 C# 代码,用于解析 ClickOnce 激活 URL 和命令行参数:

public static string[] GetArguments()
{
    var commandLineArgs = new List<string>();
    string startupUrl = String.Empty;

    if (ApplicationDeployment.IsNetworkDeployed &&
        ApplicationDeployment.CurrentDeployment.ActivationUri != null)
    {
        // Add the EXE name at the front
        commandLineArgs.Add(Environment.GetCommandLineArgs()[0]);

        // Get the query portion of the URI, also decode out any escaped sequences
        startupUrl = ApplicationDeployment.CurrentDeployment.ActivationUri.ToString();
        var query = ApplicationDeployment.CurrentDeployment.ActivationUri.Query;
        if (!string.IsNullOrEmpty(query) && query.StartsWith("?"))
        {
            // Split by the ampersands, a append a "-" for use with splitting functions
            string[] arguments = query.Substring(1).Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries).Select(a => String.Format("-{0}", HttpUtility.UrlDecode(a))).ToArray();

            // Now add the parsed argument components
            commandLineArgs.AddRange(arguments);
        }
    }
    else
    {
        commandLineArgs = Environment.GetCommandLineArgs().ToList();
    }

    // Also tack on any activation args at the back
    var activationArgs = AppDomain.CurrentDomain.SetupInformation.ActivationArguments;
    if (activationArgs != null && activationArgs.ActivationData.EmptyIfNull().Any())
    {
        commandLineArgs.AddRange(activationArgs.ActivationData.Where(d => d != startupUrl).Select((s, i) => String.Format("-in{1}:\"{0}\"", s, i == 0 ? String.Empty : i.ToString())));
    }

    return commandLineArgs.ToArray();
}

这样我的 main 函数如下所示:

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        var commandLine = GetArguments();
        var args = commandLine.ParseArgs();

        // Run app
    }

"Command-line arguments" only work with a ClickOnce app when it is run from a URL.

For example, this is how you should launch your application in order to attach some run-time arguments:

http://myserver/install/MyApplication.application?argument1=value1&argument2=value2

I have the following C# code that I use to parse ClickOnce activation URL's and command-line arguments alike:

public static string[] GetArguments()
{
    var commandLineArgs = new List<string>();
    string startupUrl = String.Empty;

    if (ApplicationDeployment.IsNetworkDeployed &&
        ApplicationDeployment.CurrentDeployment.ActivationUri != null)
    {
        // Add the EXE name at the front
        commandLineArgs.Add(Environment.GetCommandLineArgs()[0]);

        // Get the query portion of the URI, also decode out any escaped sequences
        startupUrl = ApplicationDeployment.CurrentDeployment.ActivationUri.ToString();
        var query = ApplicationDeployment.CurrentDeployment.ActivationUri.Query;
        if (!string.IsNullOrEmpty(query) && query.StartsWith("?"))
        {
            // Split by the ampersands, a append a "-" for use with splitting functions
            string[] arguments = query.Substring(1).Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries).Select(a => String.Format("-{0}", HttpUtility.UrlDecode(a))).ToArray();

            // Now add the parsed argument components
            commandLineArgs.AddRange(arguments);
        }
    }
    else
    {
        commandLineArgs = Environment.GetCommandLineArgs().ToList();
    }

    // Also tack on any activation args at the back
    var activationArgs = AppDomain.CurrentDomain.SetupInformation.ActivationArguments;
    if (activationArgs != null && activationArgs.ActivationData.EmptyIfNull().Any())
    {
        commandLineArgs.AddRange(activationArgs.ActivationData.Where(d => d != startupUrl).Select((s, i) => String.Format("-in{1}:\"{0}\"", s, i == 0 ? String.Empty : i.ToString())));
    }

    return commandLineArgs.ToArray();
}

Such that my main function looks like:

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        var commandLine = GetArguments();
        var args = commandLine.ParseArgs();

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