相关疑难解决方法(0)

在c#中转义命令行参数

精简版:

它是足够用引号括参数和逃生\"

代码版本

我想string[] args使用ProcessInfo.Arguments 将命令行参数传递给另一个进程.

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = Application.ExecutablePath;
info.UseShellExecute = true;
info.Verb = "runas"; // Provides Run as Administrator
info.Arguments = EscapeCommandLineArguments(args);
Process.Start(info);
Run Code Online (Sandbox Code Playgroud)

问题是我将参数作为数组获取,并且必须将它们合并为单个字符串.可以设计一个参数来欺骗我的程序.

my.exe "C:\Documents and Settings\MyPath \" --kill-all-humans \" except fry"
Run Code Online (Sandbox Code Playgroud)

根据这个答案,我创建了以下函数来逃避单个参数,但我可能错过了一些东西.

private static string EscapeCommandLineArguments(string[] args)
{
    string arguments = "";
    foreach (string arg in args)
    {
        arguments += " \"" +
            arg.Replace ("\\", "\\\\").Replace("\"", "\\\"") +
            "\"";
    }
    return arguments;
}
Run Code Online (Sandbox Code Playgroud)

这是否足够好或者是否有任何框架功能?

.net c# command-line-arguments

72
推荐指数
4
解决办法
3万
查看次数

在C#中传递命令行参数

我正在尝试将命令行参数传递给C#应用程序,但我有问题传递这样的东西

"C:\Documents and Settings\All Users\Start Menu\Programs\App name"
Run Code Online (Sandbox Code Playgroud)

即使我加入" "了这个论点.

这是我的代码:

    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)
            {
            }
        }
        }
Run Code Online (Sandbox Code Playgroud)

这就是我要传递的内容:

C:\WINDOWS\Uninstaller.exe  "C:\Program Files\Application name\"  "username"  "C:\Documents and Settings\All Users\Start Menu\Programs\application name"
Run Code Online (Sandbox Code Playgroud)

问题是我可以正确地获得第一个和第二个args,但是它得到的最后一个args C:\Documents.

有帮助吗?

c# command-line-arguments

18
推荐指数
2
解决办法
7万
查看次数

如何转义包含空格的路径

要将带空格的路径传递给.NET控制台应用程序,您应该将其转义.可能没有逃脱,但用双引号括起来:

myapp.exe --path C:\Program Files\MyApp`
Run Code Online (Sandbox Code Playgroud)

new string[] { "--path", "C:\Program", "Files\MyApp" }
Run Code Online (Sandbox Code Playgroud)

myapp.exe --path "C:\Program Files\MyApp"
Run Code Online (Sandbox Code Playgroud)

new string[] { "--path", "C:\Program Files\MyApp" }
Run Code Online (Sandbox Code Playgroud)

它工作正常,你可以很容易地解析它.

我想扩展一个带有附加参数的参数集,并使用生成的参数集启动一个新进程:

new ProcessStartInfo(
    Assembly.GetEntryAssembly().Location,
    String.Join(" ", Enumerable.Concat(args, new[] { "--flag" })))
Run Code Online (Sandbox Code Playgroud)

这成为myapp.exe --path C:\Program Files\MyApp --flag路径逃脱的地方.

如何使用通用解决方案解决它?(不搜索每个参数的值,需要转义并手动引用)

c# escaping console-application command-line-arguments

10
推荐指数
2
解决办法
1万
查看次数

Process.Start的转义字符串

如何将未知字符串转义为传递给Process.Start作为参数?

我目前正在逃避基本的引号和反斜杠,但最近我的输入开始包含http://www.fileformat.info/info/unicode/char/ff02/index.htm(全宽引号)等内容.

所以我的问题是,我需要逃避什么才能安全地传递一个字符串作为Process.Start的参数?

编辑: 所以我需要澄清一下.我真正想要的是一个必须在cmd.exe的带引号的字符串("foo")中转义的所有字符的列表.我最初处理双引号字符以及反斜杠字符,但我最终有一些输入包含一个全宽带引号(如上所述),它也需要转义.所以问题是,对于使用Process.Start传递给cmd.exe的带引号的字符串参数,我还需要转义什么?

c# cmd escaping

6
推荐指数
1
解决办法
4974
查看次数