1854

Escaping command line arguments in C# for Urls and Local and Network Paths

Question:

How do I provide an input string with automatic escaping to a console application?

I mean inside my code, I can do

public static void main(string[] args) { string myURL; myFolder = @"C:\temp\january\"; //just for testing myFolder = args[0]; // I want to do this eventually }

How can I provide values to myFolder without me having to escape it manually via command line?

If possible, I want to avoid calling this app like this:

C:\test> myapplication.exe "C:\\temp\\january\\"

EDIT: instead I'd prefer calling the app like this if possible

C:\test> myapplication.exe @"C:\temp\january\"

Thank you.

EDIT:

This is actually for a console application that calls Sharepoint Web services. I tried

string SourceFileFullPath, SourceFileName, DestinationFolder, DestinationFullPath; //This part didn't work. Got Microsoft.SharePoint.SoapServer.SoapServerException //SourceFileFullPath = args[0]; // C:\temp\xyz.pdf //SourceFileName = args[1]; // xyz.pdf //DestinationFolder = args[2]; // "http://myserver/ClientX/Performance" Reports //This worked. SourceFileFullPath = @"C:\temp\TestDoc2.txt"; SourceFileName = @"TestDoc2.txt"; DestinationFolder = @"http://myserver/ClientX/Performance Reports"; DestinationFullPath = string.Format("{0}/{1}", DestinationFolder, SourceFileName);

Answer1:

The requirement to escape \ inside a string if it is not a verbatim string (one that starts with @) is a C# feature. When you start your application from a console, you are outside of C#, and the console does not consider \ to be a special character, so C:\test> myapplication.exe "C:\temp\january" will work.

Edit: My original post had "C:\temp\january\" above; however, the Windows command line seems to also handle \ as an escape character - but only when in front of a ", so that command would pass C:\temp\january" to the application. Thanks to @zimdanen for pointing this out.

Please note that whatever you put between quotes in C# is a <em>representation</em> of a string; the actual string may be different - for instance, \\ <em>represents</em> a single \. If you use other means to get strings into the program, such as the command line arguments or by reading from a file, the strings do not need to follow C#'s rules for string literals. The command line has different rules for representation, in which a \ represents itself.

Answer2:

“The prefix “@” enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.”

<ol><li>You can use one of the reserved words of c# with the @ symbol</li> </ol>

ex:-

string @int = "senthil kumar"; string @class ="MCA";

2.Before a string specially when using the file paths

string filepath = @"D:\SENTHIL-DATA\myprofile.txt";

instead of

string filepath = "D:\\SENTHIL-DATA\\myprofile.txt"; <ol><li>

For a Multi lined text

string ThreeIdiots = @"Senthil Kumar, Norton Stanley, and Pavan Rao!";

MessageBox.Show(ThreeIdiots); </li> </ol>

instead of

string ThreeIdiots = @"Senthil Kumar,\n Norton Stanley,and Pavan Rao!";

Recommend

  • remove user from group Mac Os X (El Capitan)
  • visual studio does not copy assembly into gac
  • Process SOAP Headers in PHP
  • Text classification extract tags from text
  • How to add a service to the type descriptor context of a property grid in .Net?
  • Setting a Sharepoint Site Theme through a Web Service?
  • How to specify Data type in WSDL when exposed from CXF
  • Found com.google.android.gms:play-services-places:9.2.1, but version 9.0.2 is needed for the google-
  • Duplicated password validation messages ONLY IF new password is 1 char long
  • Angular Ui-router can't access $stateParams inside my controller
  • ASP.NET MVC - Detect Time Spent on Page
  • SharePoint REST query SP.UserProfiles.PeopleManager
  • How to get google-services.json from Developer console?
  • Deployments not visible in Kubernetes Dashboard
  • How to resolve docker host names (/etc/hosts) in containers
  • Replace and retrieve placeholder value
  • It is possible use the same sql azure instance from two different cloud service of two different sub
  • Zend Framework bassed projects
  • How to display the images in listview
  • auth.provider is not set to 'password' when user signs-in with email and password
  • Are Richfaces and Primefaces compatible with each other?
  • Request response issues in biztalk
  • Get the number 18437736874454810627
  • powershell Get-Counter -ComputerName parameter on Windows 7
  • Xamarin MonoAndroid Azure mobile service InsertAsync
  • Is there a way to dynamically embed PDF Files in a JSP pulled from the file system?
  • Creating PDF from TIFF image using iText
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • Private IP address in reserved subnet range
  • How To Customize ASP.NET Chart Databound To SqlDataSource
  • Clear fused location provider's location for testing
  • azure media services - The request body is too large and exceeds the maximum permissible limit
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • bootstrap to use multiple ng-app
  • Comma separated Values
  • How to get icons for entities from eclipse?
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy
  • How to load view controller without button in storyboard?