/// <summary> /// Intercepts logs from the console and provides the ability to flush them to a file in the event of a crash. /// </summary> /// <param name="logger">The logger to intercept logs from.</param> /// <param name="outputDir">The directory to which the log is output to.</param> public LogWriter(Logger logger, string outputDir) { var executableName = Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]); var universalDateTime = DateTime.UtcNow.ToString("yyyy-MM-dd HH.mm.ss"); Directory.CreateDirectory(outputDir); _logItems = new List <string>(MaxBufferLength + 1); _logger = logger; _logger.OnPrintMessage += OnPrintMessage; // Add integer to file path in case there is another instance of same process launched at the same time. string GetLogFilePath() { for (int x = 0; x < short.MaxValue; x++) { var filePath = x == 0 ? Path.Combine(outputDir, $"{universalDateTime} ~ {executableName}.txt") : Path.Combine(outputDir, $"{universalDateTime} ~ {executableName} [{x}].txt"); if (!File.Exists(filePath)) { return(filePath); } } return(default);
private static void PopulateCommandLineArgs() { string[] args = Environment.GetCommandLineArgs(); for (int index = 1; index < args.Length; index += 2) { _commandLineArguments.Add(args[index], args[index + 1]); } }
public static string GetCommandLineArg(string name) { foreach (var argument in Environment.GetCommandLineArgs()) { var parts = argument.Split(CmdArgumentDelimiters, 2); if (parts.Length == 2 && parts[0].Equals(name)) { return(parts[1]); } } return(null); }
static void Main() { string s = ""; s += "\n当前程序名:\t" + Env.GetCommandLineArgs()[0]; s += "\n当前目录:\t" + Env.CurrentDirectory; s += "\nWin操作系统:\t" + (Env.OSVersion.Platform == PlatformID.Win32NT); s += "\n环境变量Temp:\t" + Env.GetEnvironmentVariable("temp"); Console.WriteLine(s); }
/// <summary> /// Adds the command line arguments starting with <paramref name="prefixes"/> into the <see cref="Configuration"/>. /// When a configuration item exists, the value is updated. /// </summary> /// <remarks> ///The command line arguments have the syntax <c>key=value</c> (for example <c>Key1=Option1</c>or /// <c>prefixkey=value</c> (for example <c>-p:Key1=Option1</c> where <c>-p:</c> is the prefix). /// When the prefixes are not provided, all command line arguments are added to the configuration. /// When the prefixes are provided,the prefix is not a part of the key. /// <para> /// The implementation of binding treats the <see cref="bool"/> values the special way - if there is no value, but existing key, /// the value is converted to true, allowing to use the parameters like flags. For example having the argument <c>-p:SkipStep1</c> /// and prefix <c>-p:</c>, the binding a bool property <c>SkipStep1</c> will set the value of the property to <c>true</c> /// (doesn't change the configuration item itself). Of course, it's still possible to use the syntax <c>-p:SkipStep1=true</c> or /// <c>-p:SkipStep1=false</c> even in this case. /// </para> /// </remarks> /// <param name="prefixes">Optional list of prefixes marking the command line arguments to be the configuration items</param> /// <returns>The current <see cref="ConfigurationBuilder"/></returns> public ConfigurationBuilder AddCommandLineArguments(params string[] prefixes) { foreach (var argRaw in Environment.GetCommandLineArgs()) { if (prefixes != null && !prefixes.Any(p => argRaw.StartsWith(p))) { continue; } var arg = argRaw; if (prefixes != null) { arg = argRaw.Substring(prefixes.First(p => arg.StartsWith(p)).Length); } var argSplit = arg.Split(new[] { '=' }, 2); var argKey = argSplit[0]; var argValue = argSplit.Length > 1 ? argSplit[1] : null; Instance.AddOrUpdateItem(argKey, argValue); } return(this); }
public IReadOnlyList <string> GetCommandLineArgs() { return(Env.GetCommandLineArgs()); }
public static bool GetCommandLineFlag(string name) { return(Array.IndexOf(Environment.GetCommandLineArgs(), name) >= 0); }
static CommandLine() { var args = Environment.GetCommandLineArgs(); Arguments = new CommandLineArguments(args.Skip(1)); }
static bool HasOption(string option) { return(Env.GetCommandLineArgs().Contains(option)); }
public static string[] GetCommandLineArgs() { return(Env.GetCommandLineArgs()); }