Excluding command line arguments in C # for URLs and local and network paths

How to provide an auto-escaped input string 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 myFolder values ​​if I don't have to manually escape from it via the command line?

If possible, I want to avoid calling this application as follows:

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

EDIT: instead, I would rather call the application like this if possible

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

Thanks.

EDIT:

This is true for a console application that invokes 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); 
0
source share
2 answers

The requirement to avoid \ inside a string if it is not a string (that starts with @ ) is a C # function. When you launch the application from the console, you are outside of C # and the console does not consider \ special character, therefore C:\test> myapplication.exe "C:\temp\january" will work.

Edit: my original post had "C:\temp\january\" above; However, on the Windows command line, it seems to also handle \ as an escape character - but only when before " , so the command will go through C:\temp\january" in the application. Thanks @zimdanen for this.

Note that everything you specify between quotation marks in C # is a string representation; the actual string may be different - for example, \\ represents one \ . If you use other methods to get strings in a program, for example, command line arguments or reading from a file, strings should not follow C # rules for string literals. The command line has different presentation rules in which a \ represents itself.

+3
source

The "@" prefix allows you to use keywords as identifiers, which is useful when interacting with other programming languages. The @ symbol is not actually part of the identifier, so the identifier can be displayed in other languages ​​as a regular identifier without a prefix. An identifier with the prefix @ is called "shorthand identifier. The use of the @ prefix for identifiers that are not keywords is permitted, but is greatly discouraged as a matter of style."

  • You can use one of the C # reserved words with the @ symbol

eg: -

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

2. Before the line specifically when using file paths

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

instead

 string filepath = "D:\\SENTHIL-DATA\\myprofile.txt"; 
  • For multi-line text

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

     MessageBox.Show(ThreeIdiots); 

instead

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

Source: https://habr.com/ru/post/1266619/


All Articles