【问题标题】:Unable to Run Program Using CommandLineParser Package无法使用 CommandLineParser 包运行程序
【发布时间】:2016-01-22 11:05:54
【问题描述】:

我正在尝试使用 CommandLineParser 向我的应用程序添加一些命令行参数:

using CommandLine;
using System;


namespace ConsoleApplication1
{
    class Options
    {
        [Option('s', "site", Required = true,
            HelpText = "The site to connect to. Please include http://")]
        public string sitename { get; set; }

        [Option('l', "list", Required = true,
            HelpText = "The list to connect to.")]
        public string listname { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var options = new Options();

            Parser.Default.ParseArguments(args, options);

            Console.WriteLine(options.sitename);
            Console.WriteLine("\n");
            Console.WriteLine(options.listname);
        }
    }
}

但是,当我尝试从 CMD 调用它时:

test -s sitename -l listname

我收到此错误:

Unhandled Exception: System.IO.FieNotFoundEsxcepion: Could not load file or assembly 'CommandLine, Version=1.9.71.2, Culture=neutral, PublicKeytoken=de6f01bd326f8c32', or one of its dependancies. The system cannot find the file specified. at ConsoleApplication1.Program.Main(String[] args)

我已经安装了 CommandLineParser 包,我可以在我的参考列表中看到它。当我导航到这个文件夹时:\\file\IT\SK\Visual Studio Projects\ConsoleApplication1\packages\CommandLineParser.1.9.71\lib\net40 我可以看到有一个CommanLine.dll 和一个CommandLine.xml

谁能给我解释一下这里发生了什么?


更新

如果我禁用 ClickOnce 安全设置,我可以在 Visual Studio 中使用命令行运行它,并且它工作正常。但是,当我发布应用程序时,它会自动选择,并且问题仍然存在。

使用命令行参数进行调试并启用 ClickOnce 安全性时,args[] 为 null ...

【问题讨论】:

标签: c# cmd command-line-parser


【解决方案1】:

这是一项安全功能。您的可执行文件驻留在网络上,不受信任以访问其他网络资源。 我敢打赌,如果您将所有内容都复制到本地文件夹中,它会起作用。

【讨论】:

  • 感谢 lobiZoli - 当您说将所有内容复制到本地文件夹时,您能否解释一下究竟需要复制什么?这些文件是否只需要与 EXE 存在于同一文件夹中?再次感谢
  • 您的路径表明您的应用程序位于网络驱动器上。这可以用于域策略,这意味着您的桌面和文档文件夹实际上不在您的硬盘上。如果您将 CommandLine.dll 与您自己的可执行文件一起复制到本地硬盘驱动器(例如复制到 c:\temp)并尝试从那里运行它。
  • lobiZoli,你的建议奏效了。将 dll 保存在与最终可用的 EXE 相同的文件中,可以让程序运行!有没有办法解决这个问题(包括实际程序中的主包)?我使用的大多数 dll/库不需要与 exe 保存在同一个文件中!
  • @Bassie,您可以将引用的程序集添加到全局程序集缓存中。然后它不需要在同一个文件夹中。您需要为程序集“CommandLine”分配一个强键,然后从 Visual Studio 命令提示符运行“gacutil /i CommandLine.dll”。要删除它,请运行“gacutil /u CommandLine”(不带 dll 扩展名。
【解决方案2】:

dll 是否也在 bin 输出文件夹中?如果不是,请检查引用的属性并检查“本地复制”是否为真。

【讨论】:

  • 您好 pCastern,感谢您的回答。我在以下文件夹中找到了 CommandLine.dll:\\file\IT\SK\Visual Studio Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug 但 bin 文件夹本身没有 dll - 这是正确的吗? Copy Local 在 CommandLine 的属性中也设置为 True
  • @Bassie,在 Debug 文件夹中包含 dll 似乎是正确的。您可以在 Visual Studio 中调试程序,在应用程序属性 -> 调试 -> 命令行参数中设置命令行参数。另外,很抱歉发布应该是评论的答案(我仍在了解这个网站)。希望这不会阻止其他人询问。
  • 嗨 pCastern,没问题,我犯过很多次这个错误。当我按照您的建议进行调试时(通过在属性中添加命令行参数-s sitesadfname -l listnamesad),我可以通过我的程序成功 F11 并且一切都按预期运行!完成此操作后,我构建了 EXE 并再次尝试,但仍然看到相同的错误 - 这看起来很奇怪
  • 然而,这个调试似乎只有在我禁用 ClickOnce security settings 时才有效 - 如果启用,则 args = null
  • 鉴于这些信息,我认为@lobiZoli 下面的观察是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-24
  • 2020-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 2021-04-23
相关资源
最近更新 更多