简体   繁体   English

解析Windows上下文菜单中的Main Args []字符串

[英]parse Main Args[] string from windows context menu

I used the following code to implement the right clicked file that opened by my application. 我使用以下代码来实现由我的应用程序打开的右键单击文件。 My aim is to get that file's path into my program. 我的目标是将该文件的路径放入我的程序中。

I use: 我用:

public static string path = "";

static void Main(string[] args)
{
  if (args.Length > 0)
  {
     path = args[0];      
  }
}

Then I use the variable path , which is the file that opened by the application through the context menu. 然后我使用变量path ,它是应用程序通过上下文菜单打开的文件。

Problem: 问题:

When the file name doesn't contain any spaces, the file path is imported without any problems. 当文件名不包含任何空格时,导入文件路径没有任何问题。 But when the file name contains any spaces, the file name shown without its extension besides it removes letters after the first space in the file name. 但是当文件名包含任何空格时,除了它之外显示的文件名除去了文件名中第一个空格之后的字母。

Example: 例:

  1. fileName.pdffileName.pdf fileName.pdffileName.pdf
  2. fileName blah blah.pdffilename fileName blah blah.pdffilename

The second example shown that the file that contains spaces didn't imported as it should. 第二个示例显示包含空格的文件未按原样导入。

So, if there is any idea of how to parse the files that contains spaces without any problems with its name. 因此,如果有任何想法如何解析包含空格的文件而其名称没有任何问题。

This is because the operating system tries to split out the command line arguments for you, however can get it wrong if you don't put quotes in the right places. 这是因为操作系统会尝试为您拆分命令行参数,但如果您不在正确的位置放置引号,则可能会出错。 By default the following command line 默认情况下,以下命令行

MyConsoleApp.exe FileName blah blah.pdf

Will result in args containing the 3 strings FileName , blah and blah.pdf (split up by the spaces) 将导致args包含3个字符串FileNameblahblah.pdf (由空格分隔)

The most common solution to this problem is to surround the argument with quotes when invoking your application, for example 例如,解决此问题的最常见解决方案是在调用应用程序时用引号括起参数

MyConsoleApp.exe "FileName blah blah.pdf"

This will result in args having length 1 with the first string having the value FileName blah blah.pdf (the OS strips out the extra quotes). 这将导致args长度为1,第一个字符串的值为FileName blah blah.pdf (操作系统删除了额外的引号)。

The alternative is to use the Environment.CommandLine property to obtain the full unparsed command line used to invoke your application, and to manually parse that string. 另一种方法是使用Environment.CommandLine属性获取用于调用应用程序的完整未解析命令行,并手动解析该字符串。 This gives you more flexibility (as its not always possible to identify whether or not an argument was surrounded by quotes when using the args argument passed into Main), however is more effort - you should probably just make sure you use quotes when launching your application. 这为您提供了更大的灵活性(因为在使用传递给Main的args参数时,并不总是能够识别参数是否被引号括起来),但是更省力 - 您应该确保在启动应用程序时使用引号。

Some one posted a perfect answer, but he deleted it before i could upvote it and make it the right answer. 有人发布了一个完美的答案,但是在我提出它之前将其删除并使其成为正确的答案。

the answer was that i have to change the "%1" to "%0" and it worked. 答案是我必须将“%1”更改为“%0”并且它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM