0

如何为控制台应用程序提供自动转义的输入字符串?

我的意思是在我的代码中,我可以做到

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

如何为 myFolder 提供值而无需通过命令行手动转义?

如果可能的话,我想避免像这样调用这个应用程序:

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

编辑:如果可能的话,我宁愿像这样调用应用程序

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

谢谢你。

编辑:

这实际上是针对调用 Sharepoint Web 服务的控制台应用程序。我试过了

  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); 
4

2 回答 2

3

\如果字符串不是逐字字符串(以 开头),则在字符串中转义的要求@是 C# 功能。当您从控制台启动应用程序时,您处于 C# 之外,并且控制台不会将其视为\特殊字符,因此C:\test> myapplication.exe "C:\temp\january"可以正常工作。

编辑:我原来的帖子"C:\temp\january\"上面有;但是,Windows 命令行似乎也可以\作为转义字符处理 - 但只有在 a 前面时",该命令才会传递C:\temp\january"给应用程序。感谢@zimdanen 指出这一点。

请注意,C# 中引号之间的任何内容都是字符串的表示;实际的字符串可能不同——例如,\\ 表示单个\. 如果您使用其他方式将字符串获取到程序中,例如命令行参数或通过从文件中读取,则字符串不需要遵循 C# 的字符串文字规则。命令行有不同的表示规则,其中 a\表示它自己。

于 2013-04-10T17:19:26.490 回答
0

“前缀“@”允许使用关键字作为标识符,这在与其他编程语言交互时很有用。字符 @ 实际上不是标识符的一部分,因此标识符在其他语言中可能被视为普通标识符,没有前缀。带有@ 前缀的标识符称为逐字标识符。允许对不是关键字的标识符使用 @ 前缀,但出于风格问题,强烈建议不要使用。”</p>

  1. 您可以将 c# 的保留字之一与 @ 符号一起使用

前任:-

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

2.特别是在使用文件路径时的字符串之前

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

代替

string filepath = "D:\\SENTHIL-DATA\\myprofile.txt";
  1. 对于多行文本

    string ThreeIdiots = @"Senthil Kumar、Norton Stanley 和 Pavan Rao!";

    MessageBox.Show(ThreeIdiots);
    

代替

string ThreeIdiots = @"Senthil Kumar,\n   Norton Stanley,and Pavan Rao!";
于 2013-04-10T17:19:16.640 回答