传递命令行参数在C# [英] Passing command-line arguments in C#

查看:324
本文介绍了传递命令行参数在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图命令行参数传递给C#应用程序,但我有过这样的问题。

I'm trying to pass command-line arguments to a C# application, but I have problem passing something like this

"C:\Documents and Settings\All Users\Start Menu\Programs\App name"

即使我添加的说法。

下面是我的code:

    public ObjectModel(String[] args)
    {
        if (args.Length == 0) return; //no command line arg.
        //System.Windows.Forms.MessageBox.Show(args.Length.ToString());
        //System.Windows.Forms.MessageBox.Show(args[0]);
        //System.Windows.Forms.MessageBox.Show(args[1]);
        //System.Windows.Forms.MessageBox.Show(args[2]);
        //System.Windows.Forms.MessageBox.Show(args[3]);
        if (args.Length == 3)
        {
            try
            {
                RemoveInstalledFolder(args[0]);
                RemoveUserAccount(args[1]);
                RemoveShortCutFolder(args[2]);
                RemoveRegistryEntry();
            }
            catch (Exception e)
            {
            }
        }
        }

这是我通过什么:

C:\WINDOWS\Uninstaller.exe  "C:\Program Files\Application name\"  "username"  "C:\Documents and Settings\All Users\Start Menu\Programs\application name"

问题是我能得到第一和第二ARGS正确,但最后一个它得到 C:\\文件

任何帮助吗?

推荐答案

我刚跑了检查和验证的问题。这让我吃惊,但它是第一个参数的最后一个\\。

I just ran a check and verified the problem. It surprised me, but it is the last \ in the first argument.

"C:\Program Files\Application name\" <== remove the last '\'

这需要更多的解释,没有任何人有一个想法?我倾向于把它称为一个错误。

This needs more explanation, does anybody have an idea? I'm inclined to call it a bug.

第2部分,我跑了几个更多的测试和

Part 2, I ran a few more tests and

"X:\\aa aa\\" "X:\\aa aa\" next

变为

X:\\aa aa\
X:\\aa aa" next

一个小谷歌的行动给出了从<一的一些见解href=\"http://weblogs.asp.net/jgalloway/archive/2006/10/05/_5B002E00_NET-Gotcha_5D00_-Commandline-args-ending-in-_5C002200_-are-subject-to-CommandLineToArgvW-whackiness.aspx\">blog由Jon加洛韦,基本规则是:

A little Google action gives some insight from a blog by Jon Galloway, the basic rules are:


  • 反斜杠是转义字符

  • 总是逃避报价

  • 只有逃跑反斜杠时precede的报价。

这篇关于传递命令行参数在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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