3

I am trying to add some command line arguments to my application using 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);
        }
    }
}

However, when I try calling this from CMD:

test -s sitename -l listname

I am getting this error:

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)

I have installed the CommandLineParser package and I can see it in my references list. When I navigate to this folder: \\file\IT\SK\Visual Studio Projects\ConsoleApplication1\packages\CommandLineParser.1.9.71\lib\net40 I can see that there is a CommanLine.dll and a CommandLine.xml.

Can someone please explain to me what is going on here?


Update

I am able to run this with command lines in Visual Studio if I DISABLE ClickOnce Security settings, and it works fine. However, when I publish the application, this is automatically selected and the problem persists.

When debugging with command line arguments and ClickOnce Security enabled, args[] is null ...

SteveC
  • 15,808
  • 23
  • 102
  • 173
Bassie
  • 9,529
  • 8
  • 68
  • 159
  • Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Jan 22 '16 at 12:44

2 Answers2

1

Is the dll also in the bin output folder? If not, check the properties of the reference and check that "copy local" is true.

pCarsten
  • 68
  • 5
  • Hi pCastern, thanks for your answer. I found CommandLine.dll in the following folder: `\\file\IT\SK\Visual Studio Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug` but there is no dll in the bin folder itself - is this correct? Copy Local is set to True in the properties for CommandLine as well – Bassie Jan 22 '16 at 11:14
  • 1
    @Bassie, having the dll in the Debug folder seems correct. You can debug the program from within visual studio, setting command line parameters in application properties -> Debug -> Command-line arguments. Also, apologies for posting an answer that ought to have been a comment (I'm still learning about this site). Hope this will not prevent other people from asking. – pCarsten Jan 22 '16 at 11:33
  • Hi pCastern, no problem I have made that mistake many times. When I debug as you suggested (by adding the command line arguments `-s sitesadfname -l listnamesad` in properties), I can successfully F11 through my program and everything runs as expected! straight after doing this, I built the EXE and tried again, but still seeing the same error - this seems really strange – Bassie Jan 22 '16 at 12:01
  • However, this debugging only seems to work if I disable `ClickOnce security settings` - if this is enabled, then `args = null` – Bassie Jan 22 '16 at 12:04
  • 1
    Given that information, I think @lobiZoli 's observation below is correct. – pCarsten Jan 22 '16 at 12:19
1

It's a security feature. Your executable resides on the network and is not trusted to access other network resources. I bet it works if you copy everything to a local folder.

lobiZoli
  • 199
  • 9
  • Thanks lobiZoli - when you say copy everything to a local folder, can you please explain what exactly needs to be copied? And do these files just need to exist in the same folder as the EXE? Thanks again – Bassie Jan 22 '16 at 12:31
  • 1
    Your path suggests that your application is on a network drive. This could be do to a domain policy, meaning that your desktop and documents folder is actually not on your harddrive. If you copy CommandLine.dll together with your own executable to your local harddrive (ex. to c:\temp) and try to run it from there. – lobiZoli Jan 22 '16 at 12:42
  • lobiZoli, your suggestion worked. Saving the dll in the same file as the final, usable EXE allows the program to run! Is there a way around this (including the main package in the actual program)? Most dlls/libraries that I use don't need to saved in the same file as the exe! – Bassie Jan 22 '16 at 13:34
  • 1
    @Bassie, you can add the referenced assemby to the global assembly cache. Then it doesn't need to be in the same folder. You need to assign a strong key to the assembly "CommanLine" and then run "gacutil /i CommandLine.dll" from a Visual Studio Command Prompt. To remove it, run "gacutil /u CommandLine" (without the dll extension. – lobiZoli Jan 22 '16 at 13:44