zoukankan      html  css  js  c++  java
  • C# 命令行参数分割

    [DllImport("shell32.dll", SetLastError = true)]
    private static extern IntPtr CommandLineToArgvW([MarshalAs(UnmanagedType.LPWStr)] string lpCmdLine, out int pNumArgs);
    public static string[] commandLineToArgs(string commandLine)
            {
                int argc;
                var argv = CommandLineToArgvW(commandLine, out argc);
                if (argv == IntPtr.Zero)
                    throw new System.ComponentModel.Win32Exception();
                try
                {
                    var args = new string[argc];
                    for (var i = 0; i < args.Length; i++)
                    {
                        var p = Marshal.ReadIntPtr(argv, i * IntPtr.Size);
                        args[i] = Marshal.PtrToStringUni(p);
                    }
    
                    return args;
                }
                finally
                {
                    Marshal.FreeHGlobal(argv);
                }
            }
    private static string[] GetCommandLine(this Process process)
            {
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id))
                using (ManagementObjectCollection objects = searcher.Get())
                {
                    var commandLine = objects.Cast<ManagementBaseObject>().SingleOrDefault()?["CommandLine"]?.ToString();
                    if (string.IsNullOrWhiteSpace(commandLine))
                        return new string[] { };
    
                    return commandLineToArgs(commandLine);
                }
            }
  • 相关阅读:
    自我介绍 Self Introduction
    HDU1864 最大报销额
    HDU2955 Robberies
    Sicily 1509. Rails
    Sicily 1031. Campus
    Sicily 1090. Highways
    Sicily 1034. Forest
    Sicily 1800. Sequence
    Sicily 1150. 简单魔板
    CodeVS4919 线段树练习4
  • 原文地址:https://www.cnblogs.com/nanfei/p/14005863.html
Copyright © 2011-2022 走看看