在C#中转义命令行参数以获取Urls以及本地和网络路径 [英] Escaping command line arguments in C# for Urls and Local and Network Paths

查看:157
本文介绍了在C#中转义命令行参数以获取Urls以及本地和网络路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
}

如何在不使用myFolder的情况下提供值我必须通过命令行手动转义吗?

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\\" 

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

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

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

谢谢。

编辑:

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

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


推荐答案

逃脱要求 requirement string 如果不是逐字字符串(以 @ 开头的字符串),则为C#功能。从控制台启动应用程序时,您不在C#中,并且控制台不会将 \ 视为特殊字符,因此 C :\test> myapplication.exe C:\temp\january 将起作用。

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.

编辑:我的原始帖子中有上面的 C:\temp\january\ ;但是,Windows命令行似乎也将 \ 作为转义符处理-但仅当在 ,因此该命令会将 C:\temp\january 传递给应用程序。

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.

请注意,在C#中用引号引起来的是字符串的表示;实际的字符串可能有所不同-例如, \\ 代表单个 \ 。如果您使用其他方式(例如,命令行参数或从文件中读取)将字符串放入程序中,则这些字符串不需要遵循C#的字符串文字规则。命令行具有不同的表示规则,其中 \ 表示自身。

Please note that whatever you put between quotes in C# is a representation of a string; the actual string may be different - for instance, \\ represents 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.

这篇关于在C#中转义命令行参数以获取Urls以及本地和网络路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆