Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Console app: Take arguments as verbatim string aka how do I create a verbatim string list?

Console app: Take arguments as verbatim string aka how do I create a verbatim string list?

Scheduled Pinned Locked Moved C#
questionhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bbranded
    wrote on last edited by
    #1

    Hello, I'm attempting to have a console program take a path as an argument. I have the following code executing:

    static void Main(string[] args)
    {
    HandleArgs(args);
    }

    public static void HandleArgs(string[] args)
    {
    foreach (string arg in args)
    {
    if (arg.Contains("/path:"))
    {
    string execpath = arg.Substring(6, arg.Length - 6);
    }
    }
    }

    What i'd like to do is to take the string[] args as it is passed to Main() as a verbatim string. The problem arises when the argument /path: is input as /path:"c:\documents and settings\". The string arg in args is not verbatim, and execpath is then c:\documents and settings\" not c:\documents and settings\ as desired. Another foreseeable situation is the the /path: argument is /path:""c:\documents and settings\", as I am attempting a condition to determine if the last character is a \ How can I take the string into string[] args passed to Main() as a a verbatim string. Thanks, Matt

    P 2 Replies Last reply
    0
    • B bbranded

      Hello, I'm attempting to have a console program take a path as an argument. I have the following code executing:

      static void Main(string[] args)
      {
      HandleArgs(args);
      }

      public static void HandleArgs(string[] args)
      {
      foreach (string arg in args)
      {
      if (arg.Contains("/path:"))
      {
      string execpath = arg.Substring(6, arg.Length - 6);
      }
      }
      }

      What i'd like to do is to take the string[] args as it is passed to Main() as a verbatim string. The problem arises when the argument /path: is input as /path:"c:\documents and settings\". The string arg in args is not verbatim, and execpath is then c:\documents and settings\" not c:\documents and settings\ as desired. Another foreseeable situation is the the /path: argument is /path:""c:\documents and settings\", as I am attempting a condition to determine if the last character is a \ How can I take the string into string[] args passed to Main() as a a verbatim string. Thanks, Matt

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      I'm not sure what you're asking; the term "verbatim string" makes no sense in this context. If you want the command line as it appears on the command line, and not split into a string array, then try System.Environment.CommandLine P.S. If you mean that you are looking at the string in the debugger and seeing \" rather than " -- please remember that the \ is not really there, it's only displayed for your convenience -- write it to the console and see for yourself.

      modified on Thursday, April 1, 2010 12:54 PM

      B 1 Reply Last reply
      0
      • P PIEBALDconsult

        I'm not sure what you're asking; the term "verbatim string" makes no sense in this context. If you want the command line as it appears on the command line, and not split into a string array, then try System.Environment.CommandLine P.S. If you mean that you are looking at the string in the debugger and seeing \" rather than " -- please remember that the \ is not really there, it's only displayed for your convenience -- write it to the console and see for yourself.

        modified on Thursday, April 1, 2010 12:54 PM

        B Offline
        B Offline
        bbranded
        wrote on last edited by
        #3

        The following are the command line arguments: /path:"c:\Documents and Settings\mbrown\Desktop\securable.exe" /save:"c:\Documents and Settings\mbrown\Desktop\" The following code:

        try
        {
        Console.WriteLine(savepath + "~scrublbdy.tmp");
        streamer = File.CreateText(savepath + "~scrublbdy.tmp");
        streamer.WriteLine("All work and no play makes Jack a null toy.");
        streamer.Close();

                            File.Delete(savepath + "~scrublbdy.tmp");
        
                        }
                        catch (Exception err)
                        {
                            Console.WriteLine(err);
                            Environment.Exit(1);
        
                        }
        

        Produces the following error:

        C:\Documents and Settings\mbrown\My Documents\Visual Studio 2008\Projects\securable_buddy\Securable_Buddy\Securable_Budd
        y\bin\Release\Securable_Buddy.exe /path:"c:\Documents and Settings\mbrown\Desktop\securable.exe" /save:"c:\Documents and Settings\"

        c:\Documents and Settings"\~scrublbdy.tmp
        System.ArgumentException: Illegal characters in path.
        at System.IO.Path.CheckInvalidPathChars(String path)
        at System.IO.Path.GetFileName(String path)
        at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileO
        ptions options)
        at System.IO.StreamWriter.CreateFile(String path, Boolean append)
        at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
        at System.IO.StreamWriter..ctor(String path, Boolean append)
        at System.IO.File.CreateText(String path)
        at Securable_Buddy.Program.HandleArgs(String[] args) in C:\Documents and Settings\mbrown\My Documents\Visual Studio 2
        008\Projects\securable_buddy\Securable_Buddy\Securable_Buddy\Program.cs:line 117

        Note c:\Documents and Settings"\~scrublbdy.tmp. The trailing \ is being treated as an escape character in the argument. So the "best" solution I can think of is making the arguments that are passed to the main() function be verbatim strings. Any idea how to modify the incoming args to the Main() function static void Main(string[] args) to make them literal? Thanks, Matt

        D 1 Reply Last reply
        0
        • B bbranded

          The following are the command line arguments: /path:"c:\Documents and Settings\mbrown\Desktop\securable.exe" /save:"c:\Documents and Settings\mbrown\Desktop\" The following code:

          try
          {
          Console.WriteLine(savepath + "~scrublbdy.tmp");
          streamer = File.CreateText(savepath + "~scrublbdy.tmp");
          streamer.WriteLine("All work and no play makes Jack a null toy.");
          streamer.Close();

                              File.Delete(savepath + "~scrublbdy.tmp");
          
                          }
                          catch (Exception err)
                          {
                              Console.WriteLine(err);
                              Environment.Exit(1);
          
                          }
          

          Produces the following error:

          C:\Documents and Settings\mbrown\My Documents\Visual Studio 2008\Projects\securable_buddy\Securable_Buddy\Securable_Budd
          y\bin\Release\Securable_Buddy.exe /path:"c:\Documents and Settings\mbrown\Desktop\securable.exe" /save:"c:\Documents and Settings\"

          c:\Documents and Settings"\~scrublbdy.tmp
          System.ArgumentException: Illegal characters in path.
          at System.IO.Path.CheckInvalidPathChars(String path)
          at System.IO.Path.GetFileName(String path)
          at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileO
          ptions options)
          at System.IO.StreamWriter.CreateFile(String path, Boolean append)
          at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
          at System.IO.StreamWriter..ctor(String path, Boolean append)
          at System.IO.File.CreateText(String path)
          at Securable_Buddy.Program.HandleArgs(String[] args) in C:\Documents and Settings\mbrown\My Documents\Visual Studio 2
          008\Projects\securable_buddy\Securable_Buddy\Securable_Buddy\Program.cs:line 117

          Note c:\Documents and Settings"\~scrublbdy.tmp. The trailing \ is being treated as an escape character in the argument. So the "best" solution I can think of is making the arguments that are passed to the main() function be verbatim strings. Any idea how to modify the incoming args to the Main() function static void Main(string[] args) to make them literal? Thanks, Matt

          D Offline
          D Offline
          Daniel Grunwald
          wrote on last edited by
          #4

          bbranded wrote:

          Any idea how to modify the incoming args to the Main() function static void Main(string[] args) to make them literal?

          You cannot modify them. The .NET command line parser doesn't have any options. The only solution I see is the same as PIEBALDconsult suggested: take Environment.CommandLine and parse it yourself.

          B 1 Reply Last reply
          0
          • D Daniel Grunwald

            bbranded wrote:

            Any idea how to modify the incoming args to the Main() function static void Main(string[] args) to make them literal?

            You cannot modify them. The .NET command line parser doesn't have any options. The only solution I see is the same as PIEBALDconsult suggested: take Environment.CommandLine and parse it yourself.

            B Offline
            B Offline
            bbranded
            wrote on last edited by
            #5

            Thanks! Sounds good.

            1 Reply Last reply
            0
            • B bbranded

              Hello, I'm attempting to have a console program take a path as an argument. I have the following code executing:

              static void Main(string[] args)
              {
              HandleArgs(args);
              }

              public static void HandleArgs(string[] args)
              {
              foreach (string arg in args)
              {
              if (arg.Contains("/path:"))
              {
              string execpath = arg.Substring(6, arg.Length - 6);
              }
              }
              }

              What i'd like to do is to take the string[] args as it is passed to Main() as a verbatim string. The problem arises when the argument /path: is input as /path:"c:\documents and settings\". The string arg in args is not verbatim, and execpath is then c:\documents and settings\" not c:\documents and settings\ as desired. Another foreseeable situation is the the /path: argument is /path:""c:\documents and settings\", as I am attempting a condition to determine if the last character is a \ How can I take the string into string[] args passed to Main() as a a verbatim string. Thanks, Matt

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Or maybe you need a simple Trim ?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups